简体   繁体   中英

MySQL not using index when using “OR” in WHERE

I have a problem with the query:

Select * From table Where MATCH(col1) AGAINST('mystring' IN BOOLEAN MODE) OR MATCH(col2) AGAINST('mystring' IN BOOLEAN MODE)

because it is not using the fulltext index, as the query:

Select * From table Where MATCH(col1) AGAINST('mystring' IN BOOLEAN MODE) AND MATCH(col2) AGAINST('mystring' IN BOOLEAN MODE)

does.

Both col1 and col2 has a fulltext index, but when using OR instead of AND, mysql performs a full table search instead of using one of the indexes.

How can I select records with (at least) one of the 2 columns matching 'mystring' using indexes?

Just try to select each set by isolated query (with only one filter) and than intersect results with UNION.
Another form:

SELECT * FROM table WHERE MATCH (col1, col2) AGAINST ('mystring' IN BOOLEAN MODE)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM