閱讀991 返回首頁    go 技術社區[雲棲]


MySQL索引到底支持多少字節?

那麼我們來看一下MySQL varchar類型的索引到底能盛多少字節的東西。

MySQL的varchar索引隻支持不超過768個字節 
atin1 = 1 byte = 1 character
uft8 = 3 byte = 1 character
gbk = 2 byte = 1 character
如果是GBK,也就是雙字節,那麼這個索引能盛768/2=384字節。

如果創建一個大字段的索引的時候就需要注意這些,否則就會報錯,如下:

mysql> create index sql_q on slow_log(sql_md5,sql_info);
ERROR 1071 (42000): Specified key was too long; max key length is 767 bytes
mysql>

這種情況就可以使用前綴索引來解決這個問題:

alter table XXX add index ind_contentl(content(100)) //100 或其它的數

 MySQL 5.5 之前, UTF8 編碼隻支持1-3個字節 
從MYSQL5.5開始,可支持4個字節UTF編碼utf8mb4,一個字符最多能有4字節,utf8mb4兼容utf8,所以能支持更多的字符集。 


單列索引限製767,起因是256×3-1。這個3是字符最大占用空間(utf8)。
5.5以後,開始支持4個字節的utf8。255×4>767, 於是增加了一個參數叫做 innodb_large_prefix。
這個參數默認值是OFF。當改為ON時,允許列索引最大達到3072。


但是開啟該參數後還需要開啟表的動態存儲或壓縮:
Enable this option to allow index key prefixes longer than 767 bytes (up to 3072 bytes) for InnoDB tables that use DYNAMIC or COMPRESSED row format. (Creating such tables also requires the option values innodb_file_format=barracuda and innodb_file_per_table=true.) See Section 14.8.1.7, “Limits on InnoDB Tables” for maximums associated with index key prefixes under various settings.

最後更新:2017-08-13 22:33:56

  上一篇:go  被我誤解的max_connect_errors
  下一篇:go  JDK 1.8 LinkedList解碼解讀