我有一个包含超过 130 万条记录的数据库。这些记录中的每一个都有一个或多个类别(或标签)与之关联,通过使用一个类别匹配表,该表包含一个或多个与 placeid 与 catid 匹配的行。我正在使用全文搜索在地名和/或 fullloc(完整位置)字段中查找匹配项。
正如您将在下面的查询中看到的那样,我使用“内连接”和“分组依据”来处理类别的匹配。一个地点可能与近 40 个类别相关联。在搜索 130 万条记录时,这非常慢。
SELECT
MATCH(placename,fullloc) AGAINST ('chappaqua school' IN BOOLEAN MODE)
as score,
places.PlaceName, places.fullloc, places.PlaceID, places.ImageThumb,
places.contributorid, places.verified, places.verified_by,
places.verified_date, places.visibility
FROM places
WHERE STATUS IN (1,0)
AND MATCH(placename,fullloc) AGAINST ('chappaqua school' IN BOOLEAN MODE)
AND places_cats.CATID IN (129,75,104,126,115,140,128,137,136,114,135,
105,142,141,90,121,122,117,98,1,130,127,25,116,102,5,
88,87,31,24,37,134,39,40,112,34,30,133,9,8,7,11,20,19,
3,2,4,131,132,89,38,125,139,36,124,138,35,119,118,120,
96,97,95,13,17,14,12,21,15,32,16,26,29,28,27,85,86,84)
AND (visibility = 1 )
GROUP BY PlaceID
ORDER BY score DESC LIMIT 500
我正在寻找一些帮助来寻找处理类别的新方法。有没有办法通过将 CatID 存储在 Places 表中来做到这一点?
这是上述查询的解释....
1,SIMPLE,places,fulltext,"PRIMARY,by_plaeid,by_full_text",by_full_text,0,,1,Using where; Using temporary; Using filesort
1,SIMPLE,places_cats,ref,"by_PlaceIDandCatID,by_catid,by_catID_and_placeID,by_placeid",by_placeid,4,mapthepast.places.PlaceID,1,Using where
上述查询目前需要 10.8 秒。删除 Category JOIN(下),执行时间下降到 2.08。
SELECT MATCH(placename,fullloc) AGAINST ('chappaqua school' IN BOOLEAN MODE) as score,
places.PlaceName, places.fullloc, places.PlaceID, places.ImageThumb, places.contributorid, places.verified, places.verified_by, places.verified_date, places.visibility
FROM places
WHERE STATUS IN (1,0) AND MATCH(placename,fullloc) AGAINST ('chappaqua school' IN BOOLEAN MODE)
GROUP BY PlaceID
ORDER BY score DESC LIMIT 500