I\'m trying to write a query to search for a record using a wild card.
I have two queries below which works but I like to know which one of the is more optimise.
Query one does gives me what i'm looking for but query two gives me different results.
WHERE name LIKE '%County Down%'
WHERE match( name ) AGAINST ( '%County Down%' IN BOOLEAN MODE )
The first query will return results for "LACounty Down" and "NYCounty Down", but the second query will not return these results. Both queries will return results like "LA County Down" and "NY County Down," though.
To make the results match and to have it return these "fuzzy match" results, change your second query to...
WHERE match( name ) AGAINST ( '*County Down*' IN BOOLEAN MODE )
There's a significant difference between the two queries.
The first query is searching for an occurrence of the single string 'County Down'
within the name column.
The second query is searching for occurrences of either of the two separate words (separate strings) 'County'
and 'Down'
within the text. (The purpose and effect of that '%'
character before 'County'
in that second query is unknown to me.)
The relevance from a BOOLEAN MODE fulltext search is going to be 1.0. If you want to return only those rows that have both the words 'County' and 'Down', then you'd really want to use the '+'
qualifier before each word, for example:
MATCH(name) AGAINST('+County +Down' IN BOOLEAN MODE)
Note that this predicate will also "match" to a name containing 'Some Down and out County'
, for example, where the first query would not.
Also, the approach used to get the result set ordered by relevance is almost right. There's a subtle problem: including IN BOOLEAN MODE
modifier causes the expression return 1.0, instead of returning the weighted float as would be returned with NATURAL LANGUAGE MODE
.
To answer your question: if the first query is returning the result set you need, then use that query. The downside of that query is that the LIKE predicate in that query is not sargable, that is, MySQL can't make use of a index range scan to satisfy that predicate. (An index may be used for the other predicates, but that name
column on each of those rows needs to be checked.
The advantage of a query of the second form is that it can make use of a FULLTEXT INDEX, if one is created, which can improve performance.