计数时间太长

时间:2011-03-01 09:47:39

标签: mysql

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)

有不同的想法?

4 个答案:

答案 0 :(得分:1)

松散结束。把它放在不同的领域。

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

对于这些标签,您可能最好使用全文索引,尽管它们的搜索方式有所不同。这可能是优点或缺点。优点是您可以获得每个结果的分数,而不仅仅是它们是否匹配。

答案 1 :(得分:0)

解决此性能问题的最佳方法是在查询上(或在查询期间)运行分析器,它将查明导致问题的子句。

答案 2 :(得分: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字段不需要。
  • 如果您先使用最快的字段过滤掉,则无需检查字符串。

答案 3 :(得分: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 to like changed,time too long(4.07 sec)