When I change the order of the keywords in a boolean search, I get the same result but very different performance results.
The profiling on MySQL 5.6.33 with a MyISAM table, ft_min_word_len=2
and description_index
as a FULLTEXT
index on title
and description
returns this:
# Query 1
SELECT id
FROM archive, topic
WHERE topic.type=0 AND archive.status=2
AND MATCH(title, description) AGAINST ('+house* +tz*' IN BOOLEAN MODE)
AND archive.topicId = topic.id
ORDER BY archive.featured DESC, archive.submissionDate DESC LIMIT 0,20
Result:
Total count: 12
Key_read_requests: 2384607
Creating sort index: 7.950430 sec (!)
Duration: 8.851252 sec
# Query 2
SELECT id
FROM archive, topic
WHERE topic.type=0 AND archive.status=2
AND MATCH(title, description) AGAINST ('+tz* +house*' IN BOOLEAN MODE)
AND archive.topicId = topic.id
ORDER BY archive.featured DESC, archive.submissionDate DESC LIMIT 0,20
Result:
Total count: 12
Key_read_requests: 415
Creating sort index: 0.003449
Duration: 0.004054 sec
Total records per keyword:
tz*: 135092
tz: 25596
house*: 12
Explain is the same for both queries:
id | select_type | Table | Type | Key | Key len | Ref | Rows | Extra
1 | SIMPLE | archive | fulltext | description_index | 0 | | 1 | Using where; Using filesort
1 | SIMPLE | topic | eq_ref | PRIMARY | 3 | archive.topicId | 1 | Using where
Only Key_read_requests
and Creating sort index
are different between the 2 queries.
It seems that:
Questions:
Edit after OP comment:
I'm not sure about the exact query plan when resolving this query.
Sometimes one operation is more expensive than another therefore doing the less expensive operation first can sort out many rows that then don't have to go through the more expensive operation which leads to a reduced running time.
(In your example one of the matching-operations could be more expensive than the other which increases and reduces running time by changing the order of the strings to match against).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With