2010-05-19 316 views
1

我在嘗試優化下面列出的SQL查詢。
它基本上是一個基於產品名稱檢索產品的搜索引擎代碼。它還檢查產品型號以及是否啓用。
這執行約1.6秒的時候我直接通過phpMyAdmin的工具運行它,但需要大約3秒,總在與它被放置在PHP文件一起加載。SQL查詢花費的時間太長

我需要添加一個類別搜索功能和現在,這是崩潰的MySQL服務器,幫助!

賈斯汀

SELECT DISTINCT 
    p.products_id, 
    p.products_image, 
    p.products_price, 
    s.specials_new_products_price, 
    p.products_weight, 
    p.products_unit_quantity, 
    pd.products_name, 
    pd.products_img_alt, 
    pd.products_affiliate_url 
FROM 
    products AS p 
    LEFT JOIN 
     vendors v 
    ON 
     v.vendors_id = p.vendors_id 
    LEFT JOIN 
     specials AS s 
    ON 
     s.products_id = p.products_id 
     AND 
     s.status = 1, 
    categories AS c, 
    products_description AS pd, 
    products_to_categories AS p2c 
WHERE 
    (
     (
      pd.products_name LIKE '%cleaning%' 
      AND 
      pd.products_name LIKE '%supplies%' 
     ) 
     OR 
     (
      p.products_model LIKE '%cleaning%' 
      AND 
      p.products_model LIKE '%supplies%' 
     ) 
     OR 
     p.products_id = 'cleaning supplies' 
     OR 
     v.vendors_prefix = 'cleaning supplies' 
     OR 
      CONCAT (CAST (v.vendors_prefix AS CHAR), '-', CAST (p.products_id AS CHAR)) = 'cleaning supplies' 
    ) 
    AND 
    p.products_status = '1' 
    AND 
    c.categories_status = '1' 
    AND 
    p.products_id = pd.products_id 
    AND 
    p2c.products_id = pd.products_id 
    AND 
    p2c.categories_id = c.categories_id 
ORDER BY 
    pd.products_name 
+4

你能後的輸出'EXPLAIN your_query'? – richsage 2010-05-19 19:41:29

+0

查詢沒有搜索部分需要多長時間;是搜索還是加入正在放慢速度? – jeroen 2010-05-19 19:59:48

+0

如果你有很多行,那麼你應該使用Mysql索引。谷歌關於他們,它可以使查詢更快。 – 2010-05-19 19:49:32

回答

3
left join specials as s on s.products_id = p.products_id and s.status = 1, 

有你已經全表掃描。這種數據(tinyint,true/false)並不能很好地被InnoDB處理。在Oracle中,你有類似於位圖索引的東西,這對低選擇性索引很有用。你可以使用子查詢,在那裏,這樣的事情:

... 
where 
    p.product_id IN (SELECT S.status FROM specials s WHERE s.status = 1) 
AND 
    (
      (pd.products_name like '%cleaning%' and pd.products_name like '%supplies%') or 
... 

然後,您的查詢緩存祈禱緩存從子查詢的結果。

發佈您的解釋!

0
select p.products_id, p.products_image, p.products_price, 
    p.products_weight, p.products_unit_quantity, 
    s.specials_new_products_price, 
    pd.products_name, pd.products_img_alt, pd.products_affiliate_url 
from products as p 
    left join vendors v ON v.vendors_id = p.vendors_id 
    left join specials as s on s.products_id = p.products_id, 
    left join products_description as pd on pd.products_id = p.products_id 
    left join products_to_categories as p2c on p2c.products_id = p.products_id 
    left join categories as c on c.categories_id = p2c.categories_id 
where 
     (
      (pd.products_name like '%cleaning%' and pd.products_name like '%supplies%') or 
      (p.products_model like '%cleaning%' and p.products_model like '%supplies%') or 
      p.products_id = 'cleaning supplies' or 
      v.vendors_prefix = 'cleaning supplies' or 
      CONCAT(CAST(v.vendors_prefix as char), '-', CAST(p.products_id as char)) = 'cleaning supplies' 
     ) 
    and p.products_status = '1' 
    and s.status = 1 
    and c.categories_status = '1' 
group by p.products_id 
order by pd.products_name 

確保您有以下指標設置:

create index prod_prodid_status on products(products_id, products_status); 
create index vendors_vendorid on vendors(vendors_id); 
create index specials_prodid_status on vendors(products_id, `status`); 
create index prod_desc_prodid_prodname on products_description(products_id, products_name); 
create index prod_cat_prodid_catid on products_to_categories(products_id, categories_id); 
create index categories_catid_status on categories(categories_id, categories_status); 

,我希望所有與「ID」字段在他們的名字是整數