我在嘗試優化下面列出的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
你能後的輸出'EXPLAIN your_query'? – richsage 2010-05-19 19:41:29
查詢沒有搜索部分需要多長時間;是搜索還是加入正在放慢速度? – jeroen 2010-05-19 19:59:48
如果你有很多行,那麼你應該使用Mysql索引。谷歌關於他們,它可以使查詢更快。 – 2010-05-19 19:49:32