0

我已尝试优化此查询,但由于特定的业务需求而无法做到。

表:

  • b2:约200万条记录,存储业务记录
  • 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 
4

1 回答 1

1

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

尝试重写查询以使用UNION ALLs 而不是ORs:

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 
于 2013-04-13T20:37:09.073 回答