How to speed up a SQL query using the group by statement + max command?

I have a table with millions of rows, and I need to make a lot of queries that look something like this:

select max(date_field) 
where varchar_field1 = 'something' 
group by varchar_field2;

My questions:

  • Is there a way to create an index that helps with this query?
  • What (other) parameters do I need to improve the performance of this request?
+3
source share
3 answers

An index on (varchar_field1, varchar_field2, date_field)will be most useful. The database can use the first index field to suggest where, the second to group by, and the third to calculate the maximum date. He can complete the entire query only with this index, without looking at the rows in the table.

+15
source

, varchar_field1 .

0

  varchar_field1  (unique index)
  max_date_field 

, , , - , ​​ , .

, . , , , . , max_date_field , .

-1

Source: https://habr.com/ru/post/1722935/


All Articles