在 MySQl InnoDB 中,btree 索引中有多少列是否重要?
例如:
Key1 contains cols (col1, col2, col3)
或者
Key1 contains col1
Key2 contains col2
Key3 contains col3
这有关系吗?谢谢!
索引(col1, col2, col3)
可用于:
where col1 = 1 and col2 = 2 and col3 = 3
where col1 = 1 and col3 = 3
where col1 = 1
但不适用于:
where col2 = 2 and col3 = 3
where col3 = 3
要快速搜索 的值col2
,您需要一个以col2.
Like开头(col2)
的索引(col2, col3, col1)
。
因此,如果您想搜索col2
或col3
独立于col1
,三个索引是更好的选择。
是的,这很重要。
更大的索引,嗯……更大。:-) 你知道,更多的磁盘空间,更慢的读取/更新等。
事实上,当它们太大时,如果较小的替代索引可以解决问题,规划器偶尔会忽略它们。