I want to know the time complexity when I use "select * from TABLE where primary key=x" in mysql; Is the answer O(log(n))? (n is the record number)
Asked
Active
Viewed 7,725 times
1 Answers
9
The primary key has an index on it, which is typically a b-tree. The time complexity would be O(log(n)) where "n" is the size of the table. THere is an additional fetch for the data from the page. In practice, the data fetch could be much more expensive than the index lookup.
But, performance in databases is much more complicated than this. You have to deal with multiple levels of memory hierarchy, different implementations of algorithms, and issues related to grid computing.

Gordon Linoff
- 1,242,037
- 58
- 646
- 786