今天在執行sql語句時,使用表關聯查詢,結果發現子查詢中的索引未使用,直接使用了全表查詢,如圖所示:
找了半天原因,最后發現,是由於字符集設置問題導致的
當將兩個字段的字符集統一后,查詢結果如下:
Mysql 參考文檔中也明確說非二進制String應該使用相同字符集:
To retrieve rows from other tables when performing joins. MySQL can use indexes on columns more efficiently if they are declared as the same type and size.
For comparisons between nonbinary string columns, both columns should use the same character set. For example, comparing a utf8 column with a latin1 column precludes use of an index.
參考文獻:
1. http://www.cnblogs.com/weizhenlu/p/5970392.html
2. http://fatkun.com/2011/05/mysql-alter-charset.html
3. https://dev.mysql.com/doc/refman/5.5/en/mysql-indexes.html