MySQL 视图上的全文索引或更正在连接表中搜索的方式
尽管这个话题已经被广泛讨论,但它让我非常困惑,我无法提炼出正确的答案。你能帮助我朝正确的方向前进吗?谢谢。
我有这样的设置:表page
通过参考表widget_to_page
连接到表widget
:
page (p)
<代码> sid |标题 |内容
小部件 (w)sid |内容
widget_to_page (wtp)sid | page_sid | widget_sid
创建了全文索引
我已经在 p.title
、p.content
、w.content
接下来 ,我想要页面小部件组合:
SELECT *, MATCH(p.title, p.content, w.content) AGAINST("'.$keyword.'") AS score
FROM page p, widget w, widget_to_page wtp
WHERE MATCH(p.title, p.content, w.content) AGAINST("'.$keyword.'")
AND p.sid = wtp.page_sid
AND w.sid = wtp.widget_sid
ORDER BY score DESC
一些备注:
- 这显然不起作用...:-)
- 我一直在阅读如果我使用此语法来连接表(或使用 JOIN)语句,它会导致 MySQL 忽略全文索引。正确的方法是什么?
我尝试过的另一种方法是使用以下语句创建 MySQL 视图:
SELECT *
FROM page p, widget w, widget_to_page wtp
WHERE p.sid = wtp.page_sid
AND w.sid = wtp.widget_sid
但这似乎也不起作用。有些人谈到在此特定视图上创建全文索引,但我在 PHPMyAdmin 中找不到任何选项来执行此操作,而且我不知道这是否可能。
简而言之......这种情况下的最佳做法是什么?谢谢。
Although this topic has been widely discussed, it left me very confused and I haven't been able to distill the correct answer. Could you please help me in the right direction? Thanks.
I've got this setup: table page
is connected to table widget
by means of a reference table widget_to_page
:
page (p)sid | title | content
widget (w)sid | content
widget_to_page (wtp)sid | page_sid | widget_sid
I've created a fulltext index on p.title
, p.content
, w.content
Next up, I would like to the page-widget-combination:
SELECT *, MATCH(p.title, p.content, w.content) AGAINST("'.$keyword.'") AS score
FROM page p, widget w, widget_to_page wtp
WHERE MATCH(p.title, p.content, w.content) AGAINST("'.$keyword.'")
AND p.sid = wtp.page_sid
AND w.sid = wtp.widget_sid
ORDER BY score DESC
Some remarks:
- This obviously doesn't work... :-)
- I've been reading that if I use this syntax to join the tables (or use a JOIN) statement, that it would cause MySQL to ignore the fulltext index. What is the correct method?
An alternative I've tried out was to create a MySQL view out of the following statement:
SELECT *
FROM page p, widget w, widget_to_page wtp
WHERE p.sid = wtp.page_sid
AND w.sid = wtp.widget_sid
But this also does not seem to work. Some people speak of creating a fulltext index on this particular view, but I can't find any options in PHPMyAdmin to do this and I do not know if this is even possible.
In short... what is the best practice in such a situation? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FULLTEXT 索引可以包含多个列,但这些列必须位于同一个表中。因此,您将无法在单个索引中覆盖 2 个表中的这 3 列。您至少需要 2 个索引才能完成此任务。
此外,FULLTEXT 索引仅适用于 MyISAM 存储引擎,因此如果您使用的是 InnoDB 等事务引擎,则这将不起作用。您应该阅读手册以了解全文索引的其他功能和限制(包括布尔模式、停用词等):
http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
假设你使用的是MyISAM,你可以创建1个全文索引(p.title,p.content) 和 (w.content) 上的另一个全文索引,然后分别查询这两个索引并使用 UNION ALL 组合结果。
生成的查询将类似于以下内容,但您需要对其进行调整以满足您的特定要求:
A FULLTEXT index can include multiple columns, but those columns have to be in the same table. So you will not be able to cover those 3 columns from 2 tables in a single index. You will need at least 2 indexes to accomplish that.
Also, FULLTEXT indexes only work with the MyISAM storage engine, so if you are using a transactional engine like InnoDB this will not work. You should read the manual to learn about other features and limitations of fulltext indexes (including boolean mode, stopwords, etc.):
http://dev.mysql.com/doc/refman/5.1/en/fulltext-search.html
Assuming you are using MyISAM, you can create 1 fulltext index on (p.title,p.content) and another fulltext index on (w.content), then query the two indexes separately and use UNION ALL to combine the results.
The resulting query will be something like this, but you'll need to tweak it to meet your specific requirements: