在MySQL中使用in IN WHERE子句执行内部连接时,如何避免全表扫描?例如:
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)发布于 2013-10-08 02:19:22
您似乎是在按mysql认为不够有选择性的列进行筛选。当筛选器的基数太低(即,该筛选器的不同行的数量很低)时,mysql认为,在大多数情况下,fts会更快。
为了确认,请显示SELECT COUNT(DISTINCT tn.tid) FROM term_node tn和SELECT COUNT(*) FROM term_node tn的结果
https://stackoverflow.com/questions/19231459
复制相似问题