sql - Using a range filter in a greatest-n-per-group subquery has very poor performance -


i've got simple query (being used subquery in greatest-n-per-group situation). state_id primary key - else non-unique.

select max(states.state_id) max_state_id states states.created >= '2017-06-10 21:53:38.977455'   , states.created < '2017-06-26 07:00:00' group states.entity_id; 

the problem is, query horrifically slow, , don't believe order of multicolumn index can solve way it's written. ends using where; using index; using temporary; using filesort

in case it's not clear: we're trying here latest state_id each entity_id between 2 timestamps.

we instead max(states.created) (rather max(states.state_id)) better anyway, don't have state_id outer query join on.

here's full query including outer part give full context:

select states.state_id states_state_id, states.domain states_domain, states.entity_id states_entity_id, states.state states_state, states.attributes states_attributes, states.event_id states_event_id, states.last_changed states_last_changed, states.last_updated states_last_updated, states.created states_created states inner join (     select max(states.state_id) max_state_id     states     states.created >= '2017-06-10 21:53:38.977455' , states.created < '2017-06-26 07:00:00' group states.entity_id ) anon_1 on states.state_id = anon_1.max_state_id; 

surely there must way rewrite query in such manner allow index loose index scan...

first, query:

select max(s.state_id) max_state_id states s s.created >= '2017-06-10 21:53:38.977455' ,       s.created < '2017-06-26 07:00:00' group s.entity_id; 

i want sure have index on states(created, entity_d, state_id). see if help.

second, consider rewriting query as:

select max(s.state_id) max_state_id states s s.created >= '2017-06-10 21:53:38.977455' ,       s.created < '2017-06-26 07:00:00' ,       s.state_id = (select max(s2.state_id)                     states s2                     s2.entity_id = s.entity_id ,                           s2.created >= '2017-06-10 21:53:38.977455' ,                           s2.created < '2017-06-26 07:00:00'                    ); 

for query, want indexes on: states(entity_id, created, state_id) , states(created, entity_id, state_id) (the order of columns in indexes matters).


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -