2011-03-01 48 views
-3
SELECT 
sql_no_cache 
COUNT(p.id) 
FROM shop_products p  
LEFT OUTER JOIN shop_currency currency ON (p.currencyId=currency.id) 
INNER JOIN shop_l2p l2p2 FORCE INDEX(show2) ON (p.id=l2p2.pid and l2p2.status>0) 
INNER JOIN shop_labels l2 ON (l2p2.lid=l2.id and l2.type=2 and l2.status=1) 
LEFT JOIN shop_l2p l2p3 ON (p.id=l2p3.pid and l2p3.status=1) 
LEFT JOIN shop_labels l3 ON (l2p3.lid=l3.id and l3.type=3 and l3.status=1) 
WHERE 
CONCAT(p.label,l3.label,l2.label,p.stockCode) LIKE '%moda%' 
AND p.status='1' A 
ND p.stockAmount<>0 
AND p.isOption=0 
limit 1; 

+-------------+ 
| COUNT(p.id) | 
+-------------+ 
|  6669 | 
+-------------+ 
1 row in set (3.91 sec) 

有不同的想法?計數時間過長

+0

你有什麼指標對這些表?查詢的執行計劃是什麼? – andri 2011-03-01 09:49:44

+0

您是否創建了索引? 'LIKE「%MODA%」'如果搜索字符串包含''%在前面的索引將無法正常工作 – 2011-03-01 09:50:33

+0

我想你也應該存在,你有指標,也許你以前多少條記錄已經和更explicative – 2011-03-01 09:50:54

回答

0

解決這個性能問題,最好的辦法是在其上運行的查詢(或查詢時)分析儀,它會找出該條款造成問題的原因。

+0

喜;如何獲得比多joinli – 2011-03-01 09:54:25

1

寬鬆的CONCAT。把它放在不同的領域。

WHERE 
    (p.label LIKE '%moda%' or 
    l3.label LIKE '%moda%' or 
    l2.label LIKE '%moda%' or 
    p.stockCode LIKE '%moda%') 
    AND .... 

對於這些標籤,使用全文索引可能會更好,儘管它們搜索方式有點不同。這可能是有利或不利的。好處是你可以得到每個結果的分數,而不僅僅是如果他們匹配或不匹配。

+0

良好的通話結構更快的計數'CONCAT'防止有效的索引使用 – Seth 2011-03-01 09:56:54

+0

這不是完全一樣的。解決方案不匹配'moda'在串聯邊界上運行的位置,例如,當p.label ='amo'和l3.label ='d'和l2.label ='aa'時。是否這種行爲是需要的,因爲它看起來很奇怪。 – sfussenegger 2011-03-02 12:34:17

+0

我認爲這不是所期望的,因爲你在不同的標籤中搜索,並且在連接的邊界上進行搜索沒有任何意義。但是你是對的,我應該提到我的實施可能會導致不同的結果。 – GolezTrol 2011-03-02 16:01:44

0

嘗試這些變化:

SELECT 
sql_no_cache 
COUNT(p.id) 
FROM shop_products p  
INNER JOIN shop_l2p l2p2 FORCE INDEX(show2) ON (p.id=l2p2.pid and l2p2.status>0) 
INNER JOIN shop_labels l2 ON (l2p2.lid=l2.id and l2.type=2 and l2.status=1) 
LEFT JOIN shop_l2p l2p3 ON (p.id=l2p3.pid and l2p3.status=1) 
LEFT JOIN shop_labels l3 ON (l2p3.lid=l3.id and l3.type=3 and l3.status=1) 
WHERE 
p.status='1' and 
AND p.stockAmount<>0 
AND p.isOption=0 
(p.label like '%moda%' 
or l3.label like '%moda%' 
or l2.label like '%moda%' 
or p.stockCode like '%moda%') 
limit 1; 

原因:

  • 左加入到shop_currency心不是使用。
  • CONCAT強制所有thos領域無需confeat。
  • 如果您先使用最快的字段進行過濾,則無需檢查字符串。
0
1 SIMPLE p ref  PRIMARY,productAdmin,isOption,show,status,stockAmo... status 1 const 40018 Using where 
1 SIMPLE l2p3 ref  lid,show2,pid,l2p_products show2 5 ideashopfix.p.id,const 2 Using where 
1 SIMPLE l3 eq_ref PRIMARY,show,type,typeStatus,status  PRIMARY  4 ideashopfix.l2p3.lid 1 Using where 
1 SIMPLE l2p2 ref  show2 show2 4 ideashopfix.p.id 3 Using where 
1 SIMPLE l2 eq_ref PRIMARY,show,type,typeStatus,status  PRIMARY  4 ideashopfix.l2p2.lid 1 Using where 

CONCAT喜歡改變,時間太長(4.07秒)