mysql查询未优化

时间:2013-04-13 19:48:55

标签: mysql optimization

我尝试优化此查询但由于特定的业务需求而无法执行此操作。

表:

  • b2:约2mil记录,存储业务记录
  • business_reviews:小桌子,商店评论(每个商家可以有多个评论)
  • business_feature_item:小桌子,为商家存储功能(每个商家可以有多个功能

结果的具体业务要求:

  • 显示商家记录
  • 显示商家的相关评论
  • 允许搜索功能
  • 按b2.starbiz排序和得分(由MATCH AGAINST生成)

我当前的查询使用临时表,联合所有和排序,因此当结果很大时它不能很好地工作。有没有办法重新编写此查询以使其更有效?

SELECT temp.* FROM 

(SELECT DISTINCT b.business_id, b.description AS `extra`, '1' AS `type`, 0 as score 

FROM b2 as b 

LEFT JOIN business_feature_item AS i ON b.business_id = i.business_id 

WHERE ((b.cat_id = '93' OR b.cat_id2 = '93' OR b.cat_id3 = '93')) 

AND b.city_id = '152262' 

AND `approved`=1 

UNION ALL SELECT b.business_id, review_desc AS `extra`, '2' AS `type`, ((MATCH         `review_desc` AGAINST ('"restaurants"' IN BOOLEAN MODE) * 4) + (MATCH `review_desc` AGAINST ('restaurants' IN BOOLEAN MODE) )) AS score 

FROM b2 AS b, business_reviews AS r 

WHERE b.business_id =r.business_id 

AND b.city_id = '152262' 

AND ( MATCH `review_desc` AGAINST ('"restaurants"' IN BOOLEAN MODE) 

OR MATCH `review_desc` AGAINST ('restaurants' IN BOOLEAN MODE))) 

AS temp 

GROUP BY temp.business_id 

ORDER BY starbiz DESC, score DESC 

1 个答案:

答案 0 :(得分:1)

使用OR子句将导致MySQL不使用任何索引,而是执行全表扫描。

尝试重写查询以使用UNION ALL而不是OR s:

SELECT temp.* FROM 
(
    SELECT DISTINCT 
        b.business_id, 
        b.description AS `extra`,
        '1' AS `type`,
        0 as score 
    FROM 
        b2 as b 
    LEFT JOIN 
        business_feature_item AS i ON b.business_id = i.business_id 
    WHERE 
        b.cat_id = '93'
        AND b.city_id = '152262' 
        AND `approved`=1 
UNION ALL 
    SELECT DISTINCT 
        b.business_id, 
        b.description AS `extra`,
        '1' AS `type`,
        0 as score 
    FROM 
        b2 as b 
    LEFT JOIN 
        business_feature_item AS i ON b.business_id = i.business_id 
    WHERE 
        b.cat_id2 = '93'
        AND b.city_id = '152262' 
        AND `approved`=1 
UNION ALL 
    SELECT DISTINCT 
        b.business_id, 
        b.description AS `extra`,
        '1' AS `type`,
        0 as score 
    FROM 
        b2 as b 
    LEFT JOIN 
        business_feature_item AS i ON b.business_id = i.business_id 
    WHERE 
        b.cat_id3 = '93'
        AND b.city_id = '152262' 
        AND `approved`=1 
UNION ALL 
    SELECT 
        b.business_id,
        review_desc AS `extra`,
        '2' AS `type`,
        MATCH `review_desc` AGAINST ('"restaurants"' IN BOOLEAN MODE) * 4 AS score
    FROM 
        b2 AS b,
        business_reviews AS r 
    WHERE 
        b.business_id =r.business_id 
        AND b.city_id = '152262' 
        AND MATCH `review_desc` AGAINST ('"restaurants"' IN BOOLEAN MODE) 
UNION ALL 
    SELECT 
        b.business_id,
        review_desc AS `extra`,
        '2' AS `type`,
        MATCH `review_desc` AGAINST ('restaurants' IN BOOLEAN MODE) AS score 
    FROM 
        b2 AS b,
        business_reviews AS r 
    WHERE 
        b.business_id =r.business_id 
        AND b.city_id = '152262' 
        AND MATCH `review_desc` AGAINST ('restaurants' IN BOOLEAN MODE)
) 
AS temp 
GROUP BY temp.business_id 
ORDER BY starbiz DESC, score DESC