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

Optimize mysql query

 
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design
 
Thread Tools Search this Thread
  #1  
Old 10-20-2009, 09:45 PM
 
mrerotic mrerotic is offline
 

eXpert
  
Join Date: Feb 2009
Posts: 264
 

Default Optimize mysql query

Is there anyway to make this query faster? When I have benchmarking enabled I can see that this code takes 3.40 seconds to generate. I have 100,000 products with this extra_field. Can anyone please help?
Code:
// GET STUDIOS INFO // $getstudios = func_query("SELECT DISTINCT value AS studioname FROM $sql_tbl[extra_field_values] WHERE fieldid='4' AND value IS NOT NULL ORDER BY RAND() LIMIT 20");
__________________
x-cart 4.2.3
AlteredCart Smart Search
Auto Featured Products Mod (Personal Mod)
BCSE Product Importer Pro & Drop Shipper Pro
CDSEO Pro
CMS EZRecommends
EE slider login (Personal Mod)
Next Prev Link Mod (Customized)
ShadowBox Detailed Images (Personal Mod)
Shop By Price (Customized)
Social Media (Personal Mod)
Switch Layout View (Personal Mod)
Special Offers
Testimonials (Personal Mod)
Whats New (Customized)
Reply With Quote
  #2  
Old 10-21-2009, 04:45 AM
  rogue's Avatar 
rogue rogue is offline
 

X-Adept
  
Join Date: Apr 2007
Location: Loveland, Ohio
Posts: 770
 

Default Re: Optimize mysql query

I'll wager that all 100,000 rows are being selected, then sorted then 20 are re-selected.

Try taking the 'order by' out of the select and see if that makes a difference.

I'm not sure what you can do if this is indeed the case.
__________________
Richard Williams
Rogue Wave Limited

Initial Inventory Imports
Daily Inventory Updates
Daily Inventory Reports
Drop Ship Support
Order Export to Shipper/Supplier
Shopping Feeds That Work
Programming for X-Cart

richard@roguewavelimited.com
http://www.roguewavelimited.com
Reply With Quote
  #3  
Old 10-21-2009, 05:02 PM
 
mrerotic mrerotic is offline
 

eXpert
  
Join Date: Feb 2009
Posts: 264
 

Default Re: Optimize mysql query

Yes, this is the case. I have it selecting all products that have a fieldid ='4' but as soon as I add "ORDER BY" OR "RAND()" to this query benchmark shows this query gets very heavy. I can't seem to find another way to randomly select 20 results and have them being displayed ASC.

If anyone has any solution - PLEASE - let me know.
__________________
x-cart 4.2.3
AlteredCart Smart Search
Auto Featured Products Mod (Personal Mod)
BCSE Product Importer Pro & Drop Shipper Pro
CDSEO Pro
CMS EZRecommends
EE slider login (Personal Mod)
Next Prev Link Mod (Customized)
ShadowBox Detailed Images (Personal Mod)
Shop By Price (Customized)
Social Media (Personal Mod)
Switch Layout View (Personal Mod)
Special Offers
Testimonials (Personal Mod)
Whats New (Customized)
Reply With Quote
  #4  
Old 10-22-2009, 05:21 AM
 
BritSteve BritSteve is offline
 

eXpert
  
Join Date: Apr 2006
Posts: 339
 

Default Re: Optimize mysql query

What is the ratio of rows that satisfy the condition? I mean how many of the 100,000 rows have a fieldid of 4 and are not null. Is there a primary key, like ID that is numeric?

I agree that the SQL is getting the relevant rows, adding the rand and then sorting them, probably using a temp table.

There are other ways you can try, but it will involve some PHP coding.

Steve
__________________
Version 4.1.8 & 4.1.9
ezcheckout4.1.x
cdseolinks2
product_metatags41x
shipping_per_product41x

http://www.earthsmagic.com
Reply With Quote
  #5  
Old 10-22-2009, 05:27 AM
  rogue's Avatar 
rogue rogue is offline
 

X-Adept
  
Join Date: Apr 2007
Location: Loveland, Ohio
Posts: 770
 

Default Re: Optimize mysql query

The best way to do this involves some PHP code.
__________________
Richard Williams
Rogue Wave Limited

Initial Inventory Imports
Daily Inventory Updates
Daily Inventory Reports
Drop Ship Support
Order Export to Shipper/Supplier
Shopping Feeds That Work
Programming for X-Cart

richard@roguewavelimited.com
http://www.roguewavelimited.com
Reply With Quote
  #6  
Old 10-22-2009, 10:37 AM
 
mrerotic mrerotic is offline
 

eXpert
  
Join Date: Feb 2009
Posts: 264
 

Default Re: Optimize mysql query

Well Im down to try whatever if you can assist please.

Thanks guys - Kyle
__________________
x-cart 4.2.3
AlteredCart Smart Search
Auto Featured Products Mod (Personal Mod)
BCSE Product Importer Pro & Drop Shipper Pro
CDSEO Pro
CMS EZRecommends
EE slider login (Personal Mod)
Next Prev Link Mod (Customized)
ShadowBox Detailed Images (Personal Mod)
Shop By Price (Customized)
Social Media (Personal Mod)
Switch Layout View (Personal Mod)
Special Offers
Testimonials (Personal Mod)
Whats New (Customized)
Reply With Quote
  #7  
Old 10-22-2009, 11:01 AM
 
exsecror exsecror is offline
 

X-Wizard
  
Join Date: Apr 2007
Posts: 1,284
 

Default Re: Optimize mysql query

Run the EXPLAIN query analyzer and make sure it's hitting appropriate indexes, if not make the necessary indexes to speed it up. Also @rogue using PHP to handle an SQL programming issue is not generally a great idea, many web apps fail in performance because people use a language for something SQL can do a lot faster when the query and tables are properly designed. I had to fix a lot of x-cart bottlenecks because of that.
Reply With Quote
  #8  
Old 10-22-2009, 11:45 AM
 
BritSteve BritSteve is offline
 

eXpert
  
Join Date: Apr 2006
Posts: 339
 

Default Re: Optimize mysql query

Order by rand() is notorious for being slow on large tables.

http://www.webtrenches.com/post.cfm/avoid-rand-in-mysql
__________________
Version 4.1.8 & 4.1.9
ezcheckout4.1.x
cdseolinks2
product_metatags41x
shipping_per_product41x

http://www.earthsmagic.com
Reply With Quote
  #9  
Old 10-23-2009, 05:00 AM
  rogue's Avatar 
rogue rogue is offline
 

X-Adept
  
Join Date: Apr 2007
Location: Loveland, Ohio
Posts: 770
 

Default Re: Optimize mysql query

try this select:

select distinct value as studioname, rand() as random from
$sql_tbl[extra_field_values] WHERE fieldid='4' AND value IS NOT NULL ORDER BY random LIMIT 20
__________________
Richard Williams
Rogue Wave Limited

Initial Inventory Imports
Daily Inventory Updates
Daily Inventory Reports
Drop Ship Support
Order Export to Shipper/Supplier
Shopping Feeds That Work
Programming for X-Cart

richard@roguewavelimited.com
http://www.roguewavelimited.com
Reply With Quote
  #10  
Old 10-27-2009, 02:59 AM
 
exsecror exsecror is offline
 

X-Wizard
  
Join Date: Apr 2007
Posts: 1,284
 

Default Re: Optimize mysql query

The problem with that query is that RAND() is very poor on MySQL and LIMIT 20 only works AFTER MySQL has pulled down the entire table (due to the bad coding of their query analyzer on MySQL version prior to 6)
Reply With Quote
Reply
   X-Cart forums > X-Cart 4 > Dev Questions > Changing design


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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:18 AM.

   

 
X-Cart forums © 2001-2020