一 問題層面
先從今天在項目當中遇到的一個問題開始:
項目采用的flask_sqlalchemy ,因為需求的原因,在model層加了一個包含兩個field的uniqueConstrain,在數據庫間庫的時候遇到了一個問題:
Specified Key was too long:max key length is 767 bytes
翻譯就是 個別field太長了,超過了767byte的限制
二 原因層面
1
之前普通field從未報過此種錯誤,原因肯定是加了uc,眾所周知,加了uc是有索引的,因為涉及了索引
2
inodb對索引長度是有限制的,但是限制長度是255字節啊,為什么還是報錯了?
"Prefixes can be up to 255 bytes long (or 1000 bytes for MyISAM and InnoDB tables as of MySQL
4.1.2). Note that prefix limits are measured in bytes, whereas the prefix length in CREATE INDEX
statements is interpreted as number of characters. Take this into account when specifying a
prefix length for a column that uses a multi-byte character set."
3
uc的索引長度應該是創建UC的field長度之和
三 原理層面
那么為什么索引要加一個最長字符的限制呢,從索引的實現層面來講,不管多長的索引按理我都可以排序放到B/B+_樹中?
十分費解