Search code examples
indexingnuodb

Among the multiple columns in where clause, one column is not indexed - Nuo Database


Please consider a table "TABLE" in a NuoDB database that consists of 70 million records. Also consider the columns present in the "TABLE" is column_1, columne_2, column_3, column_4 and column_5. Among the above columns, column_1 is the Primary key. And column_1, column_2 and column_3 is indexed.

Now we have a requirement to execute an SQL query of the following template:

select * from TABLE where column_2 = ? and column_3 = ? and column_4 = ?

We can see that column_2 and column_3 are already indexed and hence no problem with that. But column_4 is not indexed.

Will it make a difference? Is it mandatory to include column_4 as an indexed column?


Solution

  • It's not mandatory. Will it make a difference? It depends on your data. NuoDB has a decent optimizer, so it will look at that query and do index scans on column_2 and column_3 first. But once it has that partial result it will have to load the atoms for each of the remaining records to process the last part of the predicate.

    So if you have 70,000,000 records in the table and 90% of them match column_2=? and column_3=?. then it will matter a lot. If only 7 records match column_2 and column_3 then it won't matter much.

    Run an EXPLAIN on the query and see what the optimizer's plan will be. This page will help you interpret the results.