问题
If you want the relevance and also the results to be sorted by relevance the common format of a FULLTEXT query is:
SELECT name, MATCH(name) AGAINST('Bob') AS relevance FROM users WHERE MATCH(name) AGAINST('Bob')
As a developer I always like to make my code DRY(don't repeat yourself). Is there any reason not to write the query as:
SELECT name, MATCH(name) AGAINST('Bob') AS relevance FROM users HAVING relevance > 0 ORDER BY relevance DESC
It seems to return the same results but should I be worried about the ORDER BY causing the query to be slower? Are these queries equivalent?
Specifying MATCH()
twice does not decrease performance as noted in the MySQL manual.
Natural Language Full-Text Searches
To achieve this result, you should specify
MATCH()
twice: once in theSELECT
list and once in theWHERE
clause. This causes no additional overhead, because the MySQL optimizer notices that the twoMATCH()
calls are identical and invokes the full-text search code only once.
回答1:
Unfortunately, according to the MySQL SELECT documentation, "the HAVING clause is applied nearly last, just before items are sent to the client, with no optimization."
The difference is that the first query will use the fulltext index to calculate the relevance only for rows that have 'Bob' in name
. The second query will calculate the relevance for all rows, then throw out most of them (possibly after sorting the entire table). Therefore, the second query is significantly slower. Even if you put the ORDER BY clause onto the first query, it will still be faster than using HAVING:
SELECT name, MATCH(name) AGAINST('Bob') AS relevance
FROM users
WHERE MATCH(name) AGAINST('Bob')
ORDER BY relevance DESC
In general, "do not use HAVING for items that should be in the WHERE clause."
来源:https://stackoverflow.com/questions/2245974/alternative-mysql-fulltext-search-syntax