我很糟糕,在做好MySQL查詢。我創建了這一個:MySQL - 如何優化我的查詢
SELECT SQL_CALC_FOUND_ROWS
`products_stock`.`products_id`,
`products_stock`.`products_stock_attributes`,
`products_stock`.`products_stock_quantity`,
`products`.`manufacturers_id`,
`products_description`.`products_name`
FROM `products_stock`
LEFT JOIN `products`
ON `products_stock`.`products_id` = `products`.`products_id`
LEFT JOIN `products_description`
ON `products_stock`.`products_id` = `products_description`.`products_id`
LEFT JOIN `products_to_categories`
ON `products_stock`.`products_id` = `products_to_categories`.`products_id`
WHERE `products_stock`.`products_stock_quantity` >=3
AND `products`.`products_status` = 1
AND ISNULL(`products`.`products_image`) = false
AND `products`.`products_image` != ""
AND EXISTS(
select * from `allegro`
where `products_stock`.`products_id` = `allegro`.`product_id`
and `allegro`.`attributes` = `products_stock`.`products_stock_attributes`
) = false
products
表有大約17K行, allegro
表有行約3K。
查詢想法是選擇所有產品,其中stock_quanity> 3,其中是照片,並且allegro
表中沒有產品ID。
現在查詢大約需要10秒。我不知道如何優化它。
@SOLVED
我已經添加索引allegro.product_id和allegro.attributes現在查詢tooks不到半秒。感謝所有的幫助
你有沒有在數據庫中的任何索引? – Timst 2012-07-11 09:15:02
請發佈EXPLAIN返回的內容以及所有表模式。 – Pentium10 2012-07-11 09:16:46
您有解釋查詢結果:http://scr.hu/0an/q09nm – PiKey 2012-07-11 09:22:26