mysql表關聯查詢索引不生效問題


 

 

今天在執行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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM