Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database query time complexity

I'm pretty new to databases, so forgive me if this is a silly question.

In modern databases, if I use an index to access a row, I believe this will be O(1) complexity. But if I do a query to select another column, will it be O(1) or O(n)? Does the database have to iterate through all the rows, or does it build a sorted list for each column?

like image 853
Zifre Avatar asked Apr 07 '09 21:04

Zifre


People also ask

How long should DB queries take?

The query takes 20 to 500 ms (or sometimes more) depending on the system and the amount of data. The performance of the database or the database server has a significant influence on the speed. A tip: Test your system using the demo license of the Connector to get an indication of the performance of your components.

Which database is best for complex queries?

As your queries are complex, SQL is the way to go. MongoDB is what's known as a NoSQL database. It is very fast, however it sacrifices robustness and has weak relational guarantees. Contrasting, SQL is resistant to outages and guarantees consistency between queries.

What is complexity in database?

Data complexity is the complexity of evaluating a query on a database instance, when the query is fixed, and we express the complexity as a function of the size of. the database. The other, called combined complexity, considers both the query and.


2 Answers

Actually, I think access based on an index will be O(log(n)), because you'll still be searching down through a B-Tree-esque organization to get to your record.

like image 70
Dana Avatar answered Sep 22 '22 07:09

Dana


To answer your literal question, yes if there is no index on a column, the database engine will have to look at all rows.

In the more interesting case of selecting by multiple columns, both with and without index, the situation becomes more complex: If the Query Optimizer chooses to use the index, then it'll first select rows based on the index and then apply a filter with the remaining constraints. Thus reducing the second filtering operation from O(number of rows) to O(number of selected rows by index). The ratio between these two number is called selectivity and an important statistic when choosing which index to use.

like image 44
David Schmitt Avatar answered Sep 22 '22 07:09

David Schmitt