我有以下带有用于搜索诱饵的子表的基表。(这是在与 mysql 5.7 兼容的 aws aurora 上)
tblA (id, userId, title, ...)
tblB (id, userId, title, ...)
tblA_searchBait (id->tblA.id, keywords)
tblB_searchBait (id->tblB.id, keywords)
我需要进行布尔全文搜索以在单个查询中获得组合结果。目前我使用以下内容:
select base.title as title,
match(search.keywords) against (? in boolean mode) as relevance
from tblA as base
join tblA_searchBait as search on search.id = base.id
where base.userId = ? and
match(search.keywords) against (? in boolean mode)
union all
select base.title as title,
match(search.keywords) against (? in boolean mode) as relevance
from tblB as base
join tblB_searchBait as search on search.id = base.id
where base.userId = ? and
match(search.keywords) against (? in boolean mode)
order by relevance desc
limit ?, ?;
这行得通,但我想知道:有什么方法可以提高查询的性能?(请注意,我无法更改表的架构。)。具体来说,我想知道这样的事情是否有任何区别:
select base.title as title,
match(search.keywords) against (? in boolean mode) as relevance
from tblA as base
join tblA_searchBait as search on search.id = base.id
and base.userId = ?
where match(search.keywords) against (? in boolean mode)
union all
select base.title as title,
match(search.keywords) against (? in boolean mode) as relevance
from tblB as base
join tblB_searchBait as search on search.id = base.id
and base.userId = ?
where match(search.keywords) against (? in boolean mode)
order by relevance desc
limit ?, ?;
我不相信两个不同表格提供的“相关性”具有可比性。所以,我会努力将所有全文数据放入一个表中。
为什么有 2 个相似的子表?
如果您的超级表中有一个列(如果有的话?),那么相对相关性是可能的,并且查询的运行速度大约是两倍。
对不起。但更改架构通常是优化的一个步骤。
在哪里与在
ON
“正确”编码指定了子句中的 JOINed 表和子句中的过滤之间的关系WHERE
。我不知道在什么情况下JOIN
放置东西很重要。(对 . 来说很重要LEFT JOIN
。)您可以通过
EXPLAIN SELECT ...; SHOW WARNINGS;
对每个版本进行检查来检查它们是否相同。警告应该SELECT
像优化器看到的那样拼写出来。除其他外,这将包括将ON
子句移入WHERE
(无论出于何种原因)。UNION 和括号
决赛
ORDER BY
就在旁边SELECT
。或者它就在a旁边UNION
?我喜欢通过这样做使其明确:请注意,每个
SELECT
也可以有自己的ORDER BY
,因此总共可能有 3 个ORDER BYs
。(有一些模糊的情况是有效的。)