Alex Rivera | Logout

Database query time complexity

Asked 2009-04-07T21:46:22.803
36

In modern databases, if I use an index to access a row, 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?

Edit
Report

2 Answers

13

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.

answered 2009-04-07T21:54:11.333
0

There are different types of indexes, different execution plans and different implementations for different databases. Most of the code of relations database is in search-optimising algorithms. There is not a single answer to your question. You can use a tool to visualise the execution plan when you want to know how a query is going to be executed.

answered 2009-04-07T22:13:46.357

Your Answer