是否有更好的查询来获得最匹配的结果
我正在尝试使用匹配对抗最匹配的结果。
我的第一个查询正常工作,但它使用filesort。
第二个查询也效果很好,但是查询复制并看起来很混乱。
我如何优化查询?
为了参考,索引列为class_id和name。
*仅与匹配的查询
SELECT id, name, MATCH (name) AGAINST('john smi') AS score
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi');
*我的第一个查询
SELECT * FROM (
SELECT id, name, MATCH (name) AGAINST('john smi') AS score, dense_rank() over (order by MATCH (name) AGAINST('john smi') desc) ranking
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi')
)t where t.ranking = 1;
*第二查询
SELECT id, name, MATCH (name) AGAINST('john smi') AS score
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi') AND MATCH (name) AGAINST('john smi') >= (
SELECT MATCH (name) AGAINST('john smi')
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi') limit 1
)l
I'm trying to get most matched result using MATCH AGAINST.
My first query works correctly, but it using filesort.
And the second query also works well, but the query is duplicated and looks messy.
How can I optimize my query?
For reference, the index columns are class_id and name.
*query with match against only
SELECT id, name, MATCH (name) AGAINST('john smi') AS score
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi');
What I want is two top values with the highest score.
*my first query
SELECT * FROM (
SELECT id, name, MATCH (name) AGAINST('john smi') AS score, dense_rank() over (order by MATCH (name) AGAINST('john smi') desc) ranking
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi')
)t where t.ranking = 1;
*second query
SELECT id, name, MATCH (name) AGAINST('john smi') AS score
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi') AND MATCH (name) AGAINST('john smi') >= (
SELECT MATCH (name) AGAINST('john smi')
FROM member
WHERE class_id=5
AND MATCH (name) AGAINST('john smi') limit 1
)l
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论