Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select by O(1) from SQL table

I looking for way to run select query by O(1).
Can I create index in this way that SELECT by primary key will take O (1) time complexity?

like image 856
Alex Dlikman Avatar asked Oct 19 '25 11:10

Alex Dlikman


1 Answers

A clustered primary key is organized as a b+ tree in SQL Server.

The clustered key is not a hash-based index, which is required for O(1).

I believe tree searches are O(log n).

So no, you can't

create an index in this way that SELECT by primary key will take O (1) time complexity

It is worth nothing that while a hash-based index would be quicker to do a look-up based on equality, they are less flexible than tree indexes.

For example, tree algorithms allow range operators. Also, a tree algorithm should be quicker to maintain, grow, and shrink.

like image 90
Joe Ratzer Avatar answered Oct 22 '25 00:10

Joe Ratzer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!