在 VIEW 上创建全文索引

发布于 2024-10-22 03:07:59 字数 583 浏览 3 评论 0原文

是否可以在 VIEW 上创建全文索引?

如果是这样,给定 VIEW 上的两列 column1column2,完成此操作的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只涨不跌 2024-10-29 03:08:00

一种可能效率低下但能完成工作的丑陋解决方法是将视图与基础表连接起来以访问全文列:

/* join the view with the underlying table, that is used in the view: */
select item.* from items_view as item
left join items as raw_item on raw_item.id = item.id
/* use the `item` from the view as usual: */
where item.foo = 'bar'
/* but use `raw_item` when you need the fulltext index: */
where match (raw_item.content) against ('foo bar')

当然,如果您需要在视图中创建新列,这不是一个相关的解决方案并为其提供全文索引,但如果您只需要以这种方式访问​​原始数据,并且只需要其他不相关操作的视图,那么这可能是正确的选择。

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:

/* join the view with the underlying table, that is used in the view: */
select item.* from items_view as item
left join items as raw_item on raw_item.id = item.id
/* use the `item` from the view as usual: */
where item.foo = 'bar'
/* but use `raw_item` when you need the fulltext index: */
where match (raw_item.content) against ('foo bar')

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.

怂人 2024-10-29 03:07:59

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文