优化mysql查询

时间:2014-08-26 12:46:03

标签: mysql

以下查询需要一秒钟才能执行。我需要优化以下内容。我的页面中有10个查询,因此需要超过15秒。加载页面。我需要帮助来优化查询。

SELECT p.mhSlug, p.cgSlug, p.CatDescription, p.cSlug, COUNT(DISTINCT(p.RootModelNumber)) AS prodCount 
FROM tbl_products AS p 
STRAIGHT_JOIN ModelPrice AS mp ON (mp.product_id = p.id AND mp.Available = 1) STRAIGHT_JOIN Brand_Data AS bd ON bd.pkID = p.pkBrand 
STRAIGHT_JOIN tbl_store_brands AS sb ON sb.categoryID = p.pkCategory AND sb.brandID = p.pkBrand 
LEFT JOIN tbl_filters AS f ON (f.SKU = p.SKU)
WHERE mhSlug = 'plumbing' AND p.cgSlug = 'bathroom-fixtures' AND mp.Available = 1
GROUP BY p.cSlug 
ORDER BY p.cSlug

解答这里:

id  select_type     table   type    possible_keys   key     key_len     ref     rows    Extra
1   SIMPLE  p   ref     PRIMARY,pkBrand,cgslug,mhslug,pkCategory,CategoryrBrand,mhcgc,mhcg  cgslug  228     const   41164   Using where; Using filesort
1   SIMPLE  mp  ref     product_id,product_idAvail  product_id  9   ekidtv_uakc.p.id    1   Using where
1   SIMPLE  bd  eq_ref  PRIMARY,id  PRIMARY     4   ekidtv_uakc.p.pkBrand   1   Using index
1   SIMPLE  sb  ref     brandID,categoryID,brandCat,CatBrand    brandCat    10  ekidtv_uakc.bd.pkID,ekidtv_uakc.p.pkCategory    1   Using where; Using index
1   SIMPLE  f   ref     SKU,SKUSortField    SKU     303     ekidtv_uakc.p.SKU   2   Using index

请帮助任何身体。我过去两天都在挣扎。

任何能够帮助我的人。我没有任何进步。

1 个答案:

答案 0 :(得分:1)

您需要在表格上创建或查看索引。有关mysql的信息,请参阅indexes文档。 我的查询没有看错。