I have a table with millions of rows, and I need to do LOTS of queries which look something like:
select max(date_field)
where varchar_field1 = 'something'
group by varchar_field2;
My questions are:
- Is there a way to create an index to help with this query?
- What (other) options do I have to enhance performance of this query?
An index on
(varchar_field1, varchar_field2, date_field)would be of most use. The database can use the first index field for thewhereclause, the second for thegroup by, and the third to calculate the maximum date. It can complete the entire query just using that index, without looking up rows in the table.