title: How to efficient groupby two column and return each group's first row order by time_create?
body: <p>I have a big table which used to record stock quantity, it is already order by time_create.</p>
  
  <p>Now, I want to get latest all products stock quantity in each store.</p>
  
  <p>Code: </p>
  
  <pre><code>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 &gt; unix_timestamp("2018-08-01")*1000  
  group by SQ.store_id, SQ.product_id 
  </code></pre>
  
  <p>But this is very very slow.</p>
  
  <p>So I wonder how to make my query more efficient.</p>
  
  <p>PS: Does <code>SQ.store_product_quantity_new</code> get by <code>max(SQ.time_create)</code> ? I am not sure this,  just want to get <code>store_product_quantity_new</code> with<code>max(SQ.time_create)</code> </p>
  
  <hr>
  
  <p>Structure</p>
  
  <pre><code>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);
  </code></pre>