Skip to content

Commit

Permalink
Speed up processing
Browse files Browse the repository at this point in the history
There are a few queries that are repeatedly run (is this product stocked
by attributes?).  This is reduces the execution of that
query and associated queries.  This has been identified to cut the time
involved in querying a product that has two option names with a minimum
of 4 each by approximately a factor of 7.
  • Loading branch information
mc12345678 committed Sep 18, 2018
1 parent 6de704c commit fa2fa96
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 41 deletions.
Expand Up @@ -45,6 +45,11 @@
'checkInstantiated' => true,
'classSession'=>true
);
$autoLoadConfig[200][] = array(
'autoType' => 'objectMethod',
'objectName' => 'pwas_class2',
'methodName' => '__construct'
);
$autoLoadConfig[95][] = array(
'autoType'=>'init_script',
'loadFile'=>'init_sba_copy_to_confirm_messagestack.php'
Expand Down
Expand Up @@ -33,6 +33,11 @@
'checkInstantiated' => true,
'classSession'=>true
);
$autoLoadConfig[200][] = array(
'autoType' => 'objectMethod',
'objectName' => 'pwas_class2',
'methodName' => '__construct'
);
$autoLoadConfig[0][] = array(
'autoType' => 'class',
'loadFile' => 'observers/class.products_with_attributes_stock.php'
Expand Down
38 changes: 22 additions & 16 deletions includes/classes/class.products_with_attributes_class_stock.php
Expand Up @@ -85,7 +85,7 @@ function non_stock_attribute($check_attribute_id/*, $non_stock_id, $non_stock_ty
$sql = $db->bindVars($sql, ':values:', PWAS_NON_STOCK_ATTRIB_OPTION_VALUE, 'string');
$sql = $db->bindVars($sql, ':check_attribute_id:', $check_attribute_id, 'integer');

$non_stock_result = $db->Execute($sql, false, false, 0, true);
$non_stock_result = $db->Execute($sql); //, false, false, 0, true);

$total = $non_stock_result->fields['quantity'];
if (!$total) {
Expand Down Expand Up @@ -288,7 +288,7 @@ function zen_get_customid($products_id, $attributes = null) {
ORDER BY stock_id;';
$customid_query = $db->bindVars($customid_query, ':products_id:', $products_id, 'integer');
$customid_query = $db->bindVars($customid_query, ':stock_attributes:', $stock_attributes, 'passthru');
$customid = $db->Execute($customid_query, false, false, 0, true); //moved to inside this loop as for some reason it has made
$customid = $db->Execute($customid_query); //, false, false, 0, true); //moved to inside this loop as for some reason it has made

}
}
Expand Down Expand Up @@ -497,7 +497,7 @@ function zen_get_sba_attribute_ids($products_id, $attribute_list = array(), $fro
}
$query = $db->bindVars($query, ':products_id:', $products_id, 'integer');

$attributes_new = $db->Execute($query, false, false, 0, true);
$attributes_new = $db->Execute($query);//, false, false, 0, true);

while (!$attributes_new->EOF) {
if (true) { // mc12345678 Here is one place where verification can be performed as to whether a particular attribute should be added.
Expand Down Expand Up @@ -650,6 +650,7 @@ function zen_get_sba_stock_attribute_id($products_id, $attribute_list = array(),

if ($stock_values->EOF) {
$notAccounted = true;
break;
}

//special test to account for qty when all attributes are listed seperetly
Expand Down Expand Up @@ -757,6 +758,7 @@ function zen_sba_attribs_no_text($products_id, $attribute_list, $from = 'product
return $attribute_list;
}

if (!isset($this->_isSBA[(int)$products_id]['sql'])) {
if (PRODUCTS_OPTIONS_SORT_ORDER=='0') {
$options_order_by= ' order by LPAD(popt.products_options_sort_order,11,"0"), popt.products_options_name';
} else {
Expand All @@ -779,6 +781,10 @@ function zen_sba_attribs_no_text($products_id, $attribute_list, $from = 'product
$sql = $db->bindVars($sql, ':products_id:', $products_id, 'integer');
$sql = $db->bindVars($sql, ':languages_id:', $_SESSION['languages_id'], 'integer');
$products_options_names = $db->Execute($sql);
$this->_isSBA[(int)$products_id]['sql'] = $products_options_names;
} else {
$products_options_names = $this->_isSBA[(int)$products_id]['sql'];
}

if ($products_options_names->EOF) {
// @TODO: Log error rather than set session value, unless session value is to be used elsewhere for messaging.
Expand Down Expand Up @@ -838,7 +844,7 @@ function zen_sba_attribs_no_text($products_id, $attribute_list, $from = 'product
while (!$products_options_names->EOF) {
// $_SESSION['prod_optins_names'] = $products_options_names;
// if (!isset($specAttributes[$products_options_names->fields['products_options_id']])) {
if (!array_key_exists($products_options_names->fields['products_options_id'], $specAttributes)) {
if ((!isset($specAttributes[$products_options_names->fields['products_options_id']]) || !array_key_exists($products_options_names->fields['products_options_id'], $specAttributes))) {
// $_SESSION['key_not_exist']++;
// $_SESSION['key_not_exist_spec_' . $_SESSION['key_not_exist']] = $products_options_names->fields['products_options_id'];
// $_SESSION['key_not_exist_spec_type_' . $_SESSION['key_not_exist']] = $products_options_names->fields['products_options_type'];
Expand Down Expand Up @@ -1061,7 +1067,7 @@ function zen_get_sba_attribute_info($products_id, $attribute_list = array(), $fr
$stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
// @TODO: identify if function zen_products_lookup($products_id, 'products_quantity') could be used here or if there
// would be a cache of information left behind that could cause an issue?
$stock_values = $db->Execute($stock_query, false, false, 0, true);
$stock_values = $db->Execute($stock_query); //, false, false, 0, true);
return $stock_values->fields['products_quantity'];
} elseif (!$this->zen_product_is_sba($products_id)) {
return NULL;
Expand Down Expand Up @@ -1128,7 +1134,7 @@ function zen_get_sba_attribute_info($products_id, $attribute_list = array(), $fr

$sql = $db->bindVars($sql, ':products_id:', $products_id, 'integer');
$sql = $db->bindVars($sql, ':languages_id:', $_SESSION['languages_id'], 'integer');
$products_options_names = $db->Execute($sql, false, false, 0, true);
$products_options_names = $db->Execute($sql); //, false, false, 0, true);

if ($products_options_names->EOF) {
// Log error rather than set a session value.
Expand Down Expand Up @@ -1307,7 +1313,7 @@ function zen_get_sba_attribute_info($products_id, $attribute_list = array(), $fr
$stock_query = 'select stock_id, quantity as products_quantity from ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id: and stock_attributes like :stock_attributes:';
$stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
$stock_query = $db->bindVars($stock_query, ':stock_attributes:', $stock_attributes, 'passthru');
$stock_values = $db->Execute($stock_query, false, false, 0, true);
$stock_values = $db->Execute($stock_query); //, false, false, 0, true);
// return the stock qty for the attribute
if (!$stock_values->EOF) {
switch ($datatype) {
Expand Down Expand Up @@ -1349,7 +1355,7 @@ function zen_get_sba_attribute_info($products_id, $attribute_list = array(), $fr
$stock_query = $db->bindVars($stock_query, ':products_id:', $products_id, 'integer');
$stock_query = $db->bindVars($stock_query, ':TMPstock_attributes:', $stock_attributes, 'string');
// get the stock value for the combination
$stock_values = $db->Execute($stock_query, false, false, 0, true);
$stock_values = $db->Execute($stock_query); //, false, false, 0, true);
switch ($datatype) {
case 'stock':
$stockResult = $stock_values->fields['products_quantity'];
Expand Down Expand Up @@ -1395,7 +1401,7 @@ function zen_get_sba_attribute_info($products_id, $attribute_list = array(), $fr
$stock_query = $db->bindVars($stock_query, ':eachAttribute:', $eachAttribute, 'passthru');

// get the stock value for the combination
$stock_values = $db->Execute($stock_query, false, false, 0, true);
$stock_values = $db->Execute($stock_query); //, false, false, 0, true);
$stockResult = $stock_values->fields['products_quantity'];
$stockResultArray[] = $stock_values->fields['stock_id'];

Expand Down Expand Up @@ -1633,9 +1639,9 @@ function zen_product_is_sba($product_id, $reset = false) {
// the result instead of querying the database again unless there is a need
// to "reset" the query result. An important point of doing this is when
// dealing with the cart directly so that quantities are updated correctly.
/* if (array_key_exists((int)$product_id, $this->_isSBA) && $reset == false) {
//return $this->_isSBA[(int)$product_id];
}*/
if (isset($this->_isSBA[(int)$product_id]['status']) && $reset == false) {
return $this->_isSBA[(int)$product_id]['status'];
}

$inSBA_query = $sniffer->table_exists(TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK); /*'SELECT *
FROM information_schema.tables
Expand All @@ -1649,18 +1655,18 @@ function zen_product_is_sba($product_id, $reset = false) {
if ($inSBA_query /*!$SBA_installed->EOF && $SBA_installed->RecordCount() > 0*/) { // Added to simplify query/code, assuming caching won't/can't be an issue.
$isSBA_query = 'SELECT stock_id FROM ' . TABLE_PRODUCTS_WITH_ATTRIBUTES_STOCK . ' where products_id = :products_id:;';
$isSBA_query = $db->bindVars($isSBA_query, ':products_id:', $product_id, 'integer');
$isSBA = $db->Execute($isSBA_query, false, false, 0, true);
$isSBA = $db->Execute($isSBA_query);//, false, false, 0, true);

if (!$isSBA->EOF && $isSBA->RecordCount() > 0) {
// $this->_isSBA[(int)$product_id] = true;
$this->_isSBA[(int)$product_id]['status'] = true;
return true;
} else {
// $this->_isSBA[(int)$product_id] = false;
$this->_isSBA[(int)$product_id]['status'] = false;
return false;
}
}

// $this->_isSBA[(int)$product_id] = false;
$this->_isSBA[(int)$product_id] = false;
return false;
}

Expand Down
62 changes: 37 additions & 25 deletions includes/classes/observers/class.products_with_attributes_stock.php
Expand Up @@ -322,15 +322,16 @@ function updateNotifyAttributesModuleOriginalPrice(&$callingClass, $notifier, $p
) { // Perhaps only certain features need to be bypassed, but for now all mc12345678
// START "Stock by Attributes" SBA added original price for display, and some formatting
$originalpricedisplaytext = null;
if (STOCK_SHOW_ORIGINAL_PRICE_STRUCK == 'true' && !(zen_get_attributes_price_final($products_options->fields["products_attributes_id"], 1, '', 'false') == $new_attributes_price || (zen_get_attributes_price_final($products_options->fields["products_attributes_id"], 1, '', 'false') == -$new_attributes_price && ((int)($products_options->fields['price_prefix'] . "1") * $products_options->fields['options_values_price']) < 0)) ) {
$attributes_price_final = zen_get_attributes_price_final($products_options->fields["products_attributes_id"], 1, '', 'false');
if (STOCK_SHOW_ORIGINAL_PRICE_STRUCK == 'true' && !($attributes_price_final == $new_attributes_price || ($attributes_price_final == -$new_attributes_price && ((int)($products_options->fields['price_prefix'] . "1") * $products_options->fields['options_values_price']) < 0)) ) {
//Original price struck through
if ($products_options_names->fields['products_options_type'] == PRODUCTS_OPTIONS_TYPE_RADIO || $products_options_names->fields['products_options_type'] == PRODUCTS_OPTIONS_TYPE_CHECKBOX) {
//use this if a PRODUCTS_OPTIONS_TYPE_RADIO or PRODUCTS_OPTIONS_TYPE_CHECKBOX
//class="normalprice" can be used in a CSS file to control the text properties, not compatable with selection lists
$originalpricedisplaytext = ATTRIBUTES_PRICE_DELIMITER_PREFIX . '<span class="normalprice">' . $products_options->fields['price_prefix'] . $currencies->display_price(zen_get_attributes_price_final($products_options->fields["products_attributes_id"], 1, '', 'false'), zen_get_tax_rate($product_info->fields['products_tax_class_id'])) . '</span>' . ATTRIBUTES_PRICE_DELIMITER_SUFFIX;
$originalpricedisplaytext = ATTRIBUTES_PRICE_DELIMITER_PREFIX . '<span class="normalprice">' . $products_options->fields['price_prefix'] . $currencies->display_price($attributes_price_final, zen_get_tax_rate($product_info->fields['products_tax_class_id'])) . '</span>' . ATTRIBUTES_PRICE_DELIMITER_SUFFIX;
} else {
//need to remove the <span> tag for selection lists and text boxes
$originalpricedisplaytext = ATTRIBUTES_PRICE_DELIMITER_PREFIX . $products_options->fields['price_prefix'] . $currencies->display_price(abs(zen_get_attributes_price_final($products_options->fields["products_attributes_id"], 1, '', 'false')), zen_get_tax_rate($product_info->fields['products_tax_class_id'])) . ATTRIBUTES_PRICE_DELIMITER_SUFFIX;
$originalpricedisplaytext = ATTRIBUTES_PRICE_DELIMITER_PREFIX . $products_options->fields['price_prefix'] . $currencies->display_price(abs($attributes_price_final), zen_get_tax_rate($product_info->fields['products_tax_class_id'])) . ATTRIBUTES_PRICE_DELIMITER_SUFFIX;
}
}

Expand Down Expand Up @@ -943,6 +944,20 @@ function updateNotifyHeaderEndShoppingCart(&$callingClass, $notifier, $paramsArr

if (!defined('STOCK_MARK_ALLOW_MIX_TOTAL_ALL')) define ('STOCK_MARK_ALLOW_MIX_TOTAL_ALL', 'false');

if ( PRODUCTS_OPTIONS_SORT_BY_PRICE =='1' ) {
$order_by= ' order by LPAD(pa.products_options_sort_order,11,"0")';
} else {
$order_by= ' order by LPAD(pa.products_options_sort_order,11,"0"), pa.options_values_price';
}

//LPAD - Return the string argument, left-padded with the specified string
//example: LPAD(po.products_options_sort_order,11,"0") the field is 11 digits, and is left padded with 0
if (PRODUCTS_OPTIONS_SORT_ORDER=='0') {
$options_order_by= ' order by LPAD(popt.products_options_sort_order,11,"0"), popt.products_options_name';
} else {
$options_order_by= ' order by popt.products_options_name';
}

for ($i = 0, $n = count($productArray); $i < $n; $i++) {
if (is_array($productArray[$i]) && array_key_exists('attributes', $productArray[$i]) && is_array($productArray[$i]['attributes']) && !empty($productArray[$i]['attributes']) && $_SESSION['pwas_class2']->zen_product_is_sba($productArray[$i]['id'])) {
$productArray[$i]['attributeImage'] = array();
Expand Down Expand Up @@ -1021,31 +1036,28 @@ function updateNotifyHeaderEndShoppingCart(&$callingClass, $notifier, $paramsArr
*/

//LPAD - Return the string argument, left-padded with the specified string
//example: LPAD(po.products_options_sort_order,11,"0") the field is 11 digits, and is left padded with 0
if (PRODUCTS_OPTIONS_SORT_ORDER=='0') {
$options_order_by= ' order by LPAD(popt.products_options_sort_order,11,"0"), popt.products_options_name';
if (!isset($this->_isSBA[(int)$productArray[$i]['id']]['sql' . $i])) {
//get the option/attribute list
$sql = "select distinct popt.products_options_id, popt.products_options_name, popt.products_options_sort_order,
popt.products_options_type, popt.products_options_length, popt.products_options_comment,
popt.products_options_size,
popt.products_options_images_per_row,
popt.products_options_images_style,
popt.products_options_rows
from " . TABLE_PRODUCTS_OPTIONS . " popt
left join " . TABLE_PRODUCTS_ATTRIBUTES . " patrib ON (patrib.options_id = popt.products_options_id)
where patrib.products_id= :products_id:
and popt.language_id = :languages_id: " .
$options_order_by;

$sql = $db->bindVars($sql, ':products_id:', $productArray[$i]['id'], 'integer');
$sql = $db->bindVars($sql, ':languages_id:', $_SESSION['languages_id'], 'integer');
$products_options_names = $db->Execute($sql);
$this->_isSBA[(int)$productArray[$i]['id']]['sql' . $i] = $products_options_names;
} else {
$options_order_by= ' order by popt.products_options_name';
$products_options_names = $this->_isSBA[(int)$productArray[$i]['id']]['sql'. $i];
}

//get the option/attribute list
$sql = "select distinct popt.products_options_id, popt.products_options_name, popt.products_options_sort_order,
popt.products_options_type, popt.products_options_length, popt.products_options_comment,
popt.products_options_size,
popt.products_options_images_per_row,
popt.products_options_images_style,
popt.products_options_rows
from " . TABLE_PRODUCTS_OPTIONS . " popt
left join " . TABLE_PRODUCTS_ATTRIBUTES . " patrib ON (patrib.options_id = popt.products_options_id)
where patrib.products_id= :products_id:
and popt.language_id = :languages_id: " .
$options_order_by;

$sql = $db->bindVars($sql, ':products_id:', $productArray[$i]['id'], 'integer');
$sql = $db->bindVars($sql, ':languages_id:', $_SESSION['languages_id'], 'integer');
$products_options_names = $db->Execute($sql);

while (!$products_options_names->EOF) {
$sql = "select distinct pa.attributes_image
from " . TABLE_PRODUCTS_ATTRIBUTES . " pa
Expand Down

0 comments on commit fa2fa96

Please sign in to comment.