开发者

Fulltext index on MySQL view or correct the way to search in joined tables

开发者 https://www.devze.com 2023-02-15 02:19 出处:网络
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.

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 widgetby 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:

  1. This obviously does开发者_运维问答n't work... :-)
  2. 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.


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:

select page_sid, page_title, sum(score) as total_score
from 
(
SELECT p.sid as page_sid,p.title as page_title,NULL as widget_sid, 
  MATCH(p.title, p.content) AGAINST("'.$keyword.'") AS score 
FROM page p
WHERE MATCH(p.title, p.content) AGAINST("'.$keyword.'")
UNION
SELECT p.sid as page_sid,p.title as page_title,w.sid as widget_sid, 
  MATCH(w.content) AGAINST("'.$keyword.'") AS score 
FROM page p
  inner join widget_to_page wtp on p.sid = wtp.page_sid
  inner join widget w on w.sid = wtp.widget_sid
WHERE MATCH(w.content) AGAINST("'.$keyword.'")
) as sub_query
group by page_sid
ORDER BY total_score DESC
0

精彩评论

暂无评论...
验证码 换一张
取 消