[Solved] How to efficient groupby two column and return each group\'s first row order by time_create?
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

How to efficient groupby two column and return each group\'s first row order by time_create?

I have a big table which used to record stock quantity, it is already order by time_create.

Now, I want to get latest all products stock quantity in each store.


select SQ.store_id,SQ.product_id, max(SQ.time_create) as last_time, SQ.store_product_quantity_new  
from stockquantitylogs as SQ 
where SQ.time_create > unix_timestamp("2018-08-01")*1000  
group by SQ.store_id, SQ.product_id 

But this is very very slow.

So I wonder how to make my query more efficient.

PS: Does SQ.store_product_quantity_new get by max(SQ.time_create) ? I am not sure this, just want to get store_product_quantity_new withmax(SQ.time_create)


create table stockquantitylogs
  id                         char(36) collate utf8_bin default '' not null
    primary key,
  store_id                   char(36) collate utf8_bin default '' not null,
  product_id                 char(36) collate utf8_bin default '' not null,
  time_create                bigint                               not null,
  time_update                bigint                               not null,
  store_product_quantity_old int                                  not null,
  store_product_quantity_new int                                  not null

create index IX_product_id
  on stockquantitylogs (product_id);

create index IX_store_id
  on stockquantitylogs (store_id);

create index IX_time_create
  on stockquantitylogs (time_create);

create index IX_time_update
  on stockquantitylogs (time_update);

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `stockquantitylogs` ADD INDEX `stockquantitylogs_idx_time_create` (`time_create`);
ALTER TABLE `stockquantitylogs` ADD INDEX `stockquantitylogs_idx_store_id_product_id` (`store_id`,`product_id`);
The optimized query:
        max(SQ.time_create) AS last_time,
        stockquantitylogs AS SQ 
        SQ.time_create > unix_timestamp('2018-08-01') * 1000 

Related Articles

* original question posted on StackOverflow here.