does setting a column to index in a mysql table ensure O(1) look ups?

匆匆过客 提交于 2021-01-27 10:10:29

问题


so when there's an index on a column, and you do a simple SELECT * FROM table WHERE indexed_column = value, is that a O(1) search? does it matter whether the contents indexed are integers or string?


回答1:


None of the lookups in MySQL's MyISAM or InnoDB storage engines are O(1) searches. Those storage engines use B+Trees to implement indexes. The best they can do is O(log2n) searches.

The MEMORY storage engine uses a HASH index type by default, as well as the B+Tree index type. Only the HASH index can achieve O(1) lookups.

The data type of the indexed column doesn't change this in either case.

For more on MySQL indexes, read http://dev.mysql.com/doc/refman/5.1/en/mysql-indexes.html




回答2:


It does not.

MySQL uses B-Trees as explained in this online book. Their complexity depends on the number of keys per node and therefore can do much better than O(log2n).

The number of keys per node used by MySQL depends on different factors as pointed out in this question.



来源:https://stackoverflow.com/questions/7681257/does-setting-a-column-to-index-in-a-mysql-table-ensure-o1-look-ups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!