在 VIEW 上创建全文索引
是否可以在 VIEW 上创建全文索引?
如果是这样,给定 VIEW 上的两列 column1
和 column2
,完成此操作的 SQL 是什么?
我想这样做的原因是我有两个非常大的表,我需要对每个表上的单个列进行全文搜索并合并结果。结果需要作为一个单元进行排序。
建议?
编辑:这是我尝试创建一个UNION
并按每个语句评分进行排序。
(SELECT a_name AS name, MATCH(a_name) AGAINST('$keyword') as ascore
FROM a WHERE MATCH a_name AGAINST('$keyword'))
UNION
(SELECT s_name AS name,MATCH(s_name) AGAINST('$keyword') as sscore
FROM s WHERE MATCH s_name AGAINST('$keyword'))
ORDER BY (ascore + sscore) ASC
无法识别sscore
。
Is it possible to create a full text index on a VIEW?
If so, given two columns column1
and column2
on a VIEW, what is the SQL to get this done?
The reason I'd like to do this is I have two very large tables, where I need to do a FULLTEXT search of a single column on each table and combine the results. The results need to be ordered as a single unit.
Suggestions?
EDIT: This was my attempt at creating a UNION
and ordering by each statements scoring.
(SELECT a_name AS name, MATCH(a_name) AGAINST('$keyword') as ascore
FROM a WHERE MATCH a_name AGAINST('$keyword'))
UNION
(SELECT s_name AS name,MATCH(s_name) AGAINST('$keyword') as sscore
FROM s WHERE MATCH s_name AGAINST('$keyword'))
ORDER BY (ascore + sscore) ASC
sscore
was not recognized.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种可能效率低下但能完成工作的丑陋解决方法是将视图与基础表连接起来以访问全文列:
当然,如果您需要在视图中创建新列,这不是一个相关的解决方案并为其提供全文索引,但如果您只需要以这种方式访问原始数据,并且只需要其他不相关操作的视图,那么这可能是正确的选择。
A kindof ugly workaround that might be inefficient but gets the job done would be to join the view with the underlying table to access the fulltext columns:
Of course, this isn't a relevant solution if you need to create a new column in the view and give it the fulltext index, but if you only need to access the original data in this way and you just need the view for other unrelated operations, then this might be the way to go.
MySQL 不允许在视图上建立任何形式的索引,只允许在其基础表上建立索引。这样做的原因是,由于底层表可能一直在更改数据,MySQL 仅在您从中进行选择时才具体化视图。如果您有一个返回 1000 万行的视图,那么您每次从中进行选择时都必须对其应用全文索引,这会花费大量时间。
如果您想要完整的索引功能,那么您不妨坚持使用您发布的 SQL 脚本,并手动(或使用 cronjob 脚本)每晚(或每小时,如果您在那个高流量市场)。
MySQL doesn't allow any form of indexes on a view, just on it's underlying tables. The reason for this is because MySQL only materializes a view when you select from it, due to the possibility of the underlying tables changing data all the time. If you had a view that returned 10 million rows, you'd have to apply a full text index to it every time you selected from it, and that takes a lot of time.
If you want full index functionality, then you might as well stick with the SQL script you've posted, and manually (or cronjob a script to) update the fulltext index of both tables on a nightly basis (or hourly if you're in that high traffic market).