2009-08-24 66 views
4

我有3個表格,我想對搜索項文本框進行查詢。我的查詢目前看起來像這樣:在MySQL中進行全文搜索的最有效方法

SELECT Artist.* FROM Artist, Band, Instrument WHERE MATCH (Artist.name) AGAINST ('mysearchterm') OR MATCH (Band.name) AGAINST ('mysearchterm') OR MATCH (Instrument.name, Instrument.description) AGAINST ('mysearchterm'); 

此查詢花費太多時間才能執行。有什麼辦法可以改善嗎?難道我做錯了什麼?

謝謝

回答

5

我會移動到全文搜索引擎,而不是試圖優化這一點。

http://www.sphinxsearch.com/about.html

+1

+1使用外部引擎,無論是獅身人面像或其他任何東西(Lucene的?也有商業工具,順便說一句) – 2009-08-24 21:05:58

4

MySQL有全文檢索的支持,這將使更好的性能。

http://dev.mysql.com/doc/refman/5.0/en/fulltext-restrictions.html

但是,如果你打算把你的應用程序的任何顯著負載我會建議使用專爲全文檢索系統。

+0

謝謝你的回答,我希望我可以選擇多個正確的答案。 – 2009-08-24 21:12:33

0

你能做類似以下的事情嗎?這不是全文搜索?

SELECT Artist.* FROM Artist, Band, Instrument WHERE Artist.name LIKE '%mysearchterm%'... 

或(我的偏好):

SELECT Artist.* FROM Artist, Band, Instrument WHERE Artist.name REGEXP '<regexp here>'... 
1

對不起已故的後續,但不是你做一個笛卡爾加入對那些三個表?

SELECT Artist.* FROM Artist, Band, Instrument WHERE MATCH (Artist.name) AGAINST ('mysearchterm') OR MATCH (Band.name) AGAINST ('mysearchterm') OR MATCH (Instrument.name, Instrument.description) AGAINST ('mysearchterm'); 

假設數據庫中有100個樂隊,10個樂器和500位藝術家,您將通過500,000行搜索。

我希望看到類似假設你有一個數據庫,在那裏藝術家屬於一個樂隊,玩一種樂器:

SELECT Artist.* FROM Artist, Band, Instrument WHERE Artist.band_id = Band.id and Artist.instrument_id = Instrument.id and (MATCH (Artist.name) AGAINST ('mysearchterm') OR MATCH (Band.name) AGAINST ('mysearchterm') OR MATCH (Instrument.name, Instrument.description) AGAINST ('mysearchterm'));