Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

insert_productsonline() can be a major resource hog

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions
 
Thread Tools Search this Thread
  #1  
Old 02-26-2005, 05:00 PM
 
NuAlpha NuAlpha is offline
 

X-Adept
  
Join Date: Aug 2003
Location: US
Posts: 598
 

Default insert_productsonline() can be a major resource hog

I was doing some profiling on Xcart and discovered that insert_productsonline() can take upwards of 15,000 milliseconds to execute, making up about 60% of the shopping cart's resource usage. This happens when MySQL hasn't stored and optimized the query for future use. Most of the time the calling of this function takes an average of 740 ms and about 11% of the shopping cart's execution time & resource usage.

I have found a simple remedy to this problem that produces the same results as the original code, but uses up only an average of 120 ms.

Note that our site holds over 65,000 products, so it is no wonder this takes up a lot of processing power to gather the statistics.

Replace the return line in insert_productsonline() in include/func.php with the following code:
Code:
return func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products], $sql_tbl[categories] WHERE $sql_tbl[products].forsale='Y' AND $sql_tbl[products].categoryid=$sql_tbl[categories].categoryid AND ($sql_tbl[categories].membership='$user_account[membership]' OR $sql_tbl[categories].membership='')");

The key to this code speed increase is the usage of COUNT(*) instead of COUNT($sql_tbl[products].productid).

Please note that your must be using a version of Xcart that has the func_query_first_cell() function available. If not, then simply use array_pop(func_query_first()) in its place.
__________________
X-Cart Pro 4.5.5 Platinum
X-Payments 1.0.6
PHP 5.3.14
MySQL 5.1.68
Apache 2.2.23
Reply With Quote
  #2  
Old 02-27-2005, 04:35 AM
  TelaFirma's Avatar 
TelaFirma TelaFirma is offline
 

X-Adept
  
Join Date: Nov 2002
Location: North Carolina USA
Posts: 930
 

Default

Whole crap... nice catch. Big difference made....


Thanks.
Reply With Quote
  #3  
Old 03-05-2005, 09:05 PM
 
CopperB CopperB is offline
 

Advanced Member
  
Join Date: Jan 2005
Posts: 58
 

Default

I tried this MOD and my site only loads a blank white page.

Original code from func.php

Code:
# # Return number of available products # function insert_productsonline() { global $sql_tbl; return func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products] WHERE forsale!='N'");

Modded code from func.php

Code:
# # Return number of available products # function insert_productsonline() { global $sql_tbl; return return func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products], $sql_tbl[categories] WHERE $sql_tbl[products].forsale='Y' AND $sql_tbl[products].categoryid=$sql_tbl[categories].categoryid AND ($sql_tbl[categories].membership='$user_account[membership]' OR $sql_tbl[categories].membership='')");

Am I missing or doing something wrong?


Thanks

ps. NuAlpha, thanks for all of your mods and posts. They are much apprecited.
__________________
CopperB

XCart Gold -> v4.0.11
PHP -> v4.3.10
MySQL -> v4.0.22
Apache -> v1.3.33
Smarty -> v2.6.3
Reply With Quote
  #4  
Old 03-06-2005, 03:28 AM
  TelaFirma's Avatar 
TelaFirma TelaFirma is offline
 

X-Adept
  
Join Date: Nov 2002
Location: North Carolina USA
Posts: 930
 

Default

The original post states:
Quote:
The key to this code speed increase is the usage of COUNT(*) instead of COUNT($sql_tbl[products].productid).

If you look at your original code, you see that the function is:
Code:
return func_query_first_cell("SELECT COUNT(*) FROM $sql_tbl[products] WHERE forsale!='N'");

therefore this modification is not for you. You do not need it. COUNT(*) is already written into that function in your version of X-Cart.
Reply With Quote
  #5  
Old 03-06-2005, 09:41 AM
 
NuAlpha NuAlpha is offline
 

X-Adept
  
Join Date: Aug 2003
Location: US
Posts: 598
 

Default

Quote:
Originally Posted by CopperB
Am I missing or doing something wrong?

You have a double "return return" in your modified code instead of just one "return".

Also, as TeleFirma stated, you wouldn't really need this code change as this code already appears to be in effect with Xcart 4.0.11.
__________________
X-Cart Pro 4.5.5 Platinum
X-Payments 1.0.6
PHP 5.3.14
MySQL 5.1.68
Apache 2.2.23
Reply With Quote
  #6  
Old 03-06-2005, 10:31 AM
 
CopperB CopperB is offline
 

Advanced Member
  
Join Date: Jan 2005
Posts: 58
 

Default

Thanks for the replies guys. It was getting late and obviously I wasn't thinking or reading very clearly. I'm also a newbie and still getting the hang of x-cart.
__________________
CopperB

XCart Gold -> v4.0.11
PHP -> v4.3.10
MySQL -> v4.0.22
Apache -> v1.3.33
Smarty -> v2.6.3
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 05:16 AM.

   

 
X-Cart forums © 2001-2020