2011-03-01 51 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秒)