Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Indexes for Group By Fields?

Do you need to create an index for fields of group by fields in an Oracle database?

For example:

select *  from some_table where field_one is not null and field_two = ? group by field_three, field_four, field_five 

I was testing the indexes I created for the above and the only relevant index for this query is an index created for field_two. Other single-field or composite indexes created on any of the other fields will not be used for the above query. Does this sound correct?

like image 398
onejigtwojig Avatar asked Sep 18 '09 14:09

onejigtwojig


People also ask

Does index work on group by?

The Oracle database cannot read an index backwards in order to execute a pipelined group by that is followed by an order by .

Can we create index on multiple columns?

A composite index is an index on multiple columns. MySQL allows you to create a composite index that consists of up to 16 columns. A composite index is also known as a multiple-column index.

Which is the index used for multiple fields?

A multicolumn index is an index based on the values in multiple columns of a table.


1 Answers

It could be correct, but that would depend on how much data you have. Typically I would create an index for the columns I was using in a GROUP BY, but in your case the optimizer may have decided that after using the field_two index that there wouldn't be enough data returned to justify using the other index for the GROUP BY.

like image 133
Eric Petroelje Avatar answered Sep 22 '22 07:09

Eric Petroelje