How can I avoid a full table scan when performing inner joins in MySQL using IN in WHERE clause? For example:
explain SELECT
-> COUNT(DISTINCT(n.nid))
-> FROM node n
-> INNER JOIN term_node tn ON n.nid = tn.nid
-> INNER JOIN content_type_article ca ON n.nid = ca.nid
-> WHERE tn.tid IN (67,100)
-> ;
+----+-------------+-------+--------+----------------------------------+---------+---------+----------------------+-------+--------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+--------+----------------------------------+---------+---------+----------------------+-------+--------------------------+
| 1 | SIMPLE | tn | ALL | PRIMARY,nid | NULL | NULL | NULL | 42180 | Using where |
| 1 | SIMPLE | ca | ref | nid,field_article_date_nid_index | nid | 4 | drupal_mm_qas.tn.nid | 1 | Using index |
| 1 | SIMPLE | n | eq_ref | PRIMARY | PRIMARY | 4 | drupal_mm_qas.ca.nid | 1 | Using where; Using index |
+----+-------------+-------+--------+----------------------------------+---------+---------+----------------------+-------+--------------------------+
3 rows in set (0.00 sec)
It seems you're filtering by a column that mysql identified to be not selective enough. When a filter's cardinality is too low (ie, the number of distinct rows for that filter is low), mysql thinks, most of the time accurately, that a fts would be faster.
To confirm, please show the result of SELECT COUNT(DISTINCT tn.tid) FROM term_node tn
and SELECT COUNT(*) FROM term_node tn
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.