Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is a runtime complexity of this sql query?

I would like to know how fast is SELECT * FROM user_table WHERE email = 'test@gmail.com'
is this O(1) or O(n)?

how does sql search for a particular row?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If there is no index on "email" column, the search complexity is O(N).

If there was hash-based index on an "email" column, then the search could be performed in O(1).

However in real DB engines the indices are usually tree-based (as they enable quick search not for equality only, but also for "greater/less than" conditions).
For binary trees the search complexity is O(log N) so in most cases index on "email" column results in this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading