Solved

Help optimizing this query...

Posted on 2011-03-11
6
283 Views
Last Modified: 2012-05-11
SELECT p.*, pa.`id_product_attribute`, pl.`description`, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, i.`id_image`, il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name, t.`rate`, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL 20 DAY)) > 0 AS new,
(p.`price` * ((100 + (t.`rate`))/100) - IF((DATEDIFF(`reduction_from`, CURDATE()) <= 0 AND DATEDIFF(`reduction_to`, CURDATE()) >=0) OR `reduction_from` = `reduction_to`, IF(`reduction_price` > 0, `reduction_price`, (p.`price` * ((100 + (t.`rate`))/100) * `reduction_percent` / 100)),0)) AS orderprice
FROM `ps_category_product` cp
LEFT JOIN `ps_product` p ON p.`id_product` = cp.`id_product`
LEFT JOIN `ps_product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1)
LEFT JOIN `ps_category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = 1)
LEFT JOIN `ps_product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = 1)
LEFT JOIN `ps_image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `ps_image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = 1)
LEFT JOIN `ps_tax` t ON t.`id_tax` = p.`id_tax`
LEFT JOIN `ps_tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = 1)
LEFT JOIN `ps_manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer`
WHERE cp.`id_category` = 14 AND p.`active` = 1
ORDER BY `orderprice` ASC
LIMIT 10,10

Open in new window

Here is the EXPLAIN:
"id","select_type","table","type","possible_keys","key","key_len","ref","rows","Extra"
"1","SIMPLE","pa","system","product_attribute_product,product_default",NULL,NULL,NULL,"0","const row not found"
"1","SIMPLE","cp","range","category_product_index,id_product","category_product_index","4",NULL,"135000","Using where; Using index; Using temporary; Using filesort"
"1","SIMPLE","p","eq_ref","PRIMARY","PRIMARY","4","partrepo_store.cp.id_product","1","Using where"
"1","SIMPLE","cl","eq_ref","category_lang_index","category_lang_index","8","partrepo_store.p.id_category_default,const","1",""
"1","SIMPLE","pl","eq_ref","product_lang_index,id_lang","product_lang_index","8","partrepo_store.p.id_product,const","1",""
"1","SIMPLE","i","ref","image_product,product_position,id_product_cover","id_product_cover","5","partrepo_store.p.id_product,const","12",""
"1","SIMPLE","il","eq_ref","image_lang_index,id_image","image_lang_index","8","partrepo_store.i.id_image,const","1",""
"1","SIMPLE","t","eq_ref","PRIMARY","PRIMARY","4","partrepo_store.p.id_tax","1",""
"1","SIMPLE","tl","eq_ref","tax_lang_index","tax_lang_index","8","partrepo_store.t.id_tax,const","1",""
"1","SIMPLE","m","eq_ref","PRIMARY","PRIMARY","4","partrepo_store.p.id_manufacturer","1",""

Open in new window

table                         #rows
----------------------------  -----------
ps_category_product           342,386
ps_product                    330,111
ps_product_attribute          0
ps_category_lang              12
ps_product_lang               1,027,155
ps_image                      342,384
ps_image_lang                 1,027,149
ps_tax                        1
ps_tax_lang                   3
ps_manufacturer               1

Open in new window

What can be done to optimize this, it currently takes 56 seconds to run even when there is a limit.
0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 26

Author Comment

by:EddieShipman
ID: 35112651
Seems that the ORDER BY calculated value is what is causing the problem.
If I run the same query without the ORDER BY it runs in < 1 sec.
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 35112682
This query, when run on my host returns this:
Incorrect key file for table '/tmp/#sql_15b5_0.MYI'; try to repair it
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 35112719
Well, it's not the calculated column causing the problem. It also runs very slow when I change the ORDER BY to quantity
0
Webinar: Security & Encryption in the MySQL world

Join Percona’s Solutions Engineer, Dimitri Vanoverbeke as he presents “Security and Encryption in the MySQL world” on Thursday, July 6, 2017 at 7:00 am PDT / 10:00 am EDT (UTC-7).

 
LVL 39

Accepted Solution

by:
Aaron Tomosky earned 500 total points
ID: 35114471
Take out the order by, wrap the whole thing in
Select * from (
--current query
) order by quantity

This way it just sorts the output instead of the whole thing
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 35114551
Holy bejeezus. What a difference. Thanks a million.
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 35114553
Excellent
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question