mysql - Efficiently calculating number of results by range in grouped SQL query -
i'm working large database in every row has specific class. every class in turn belongs generalized class, specified in separate table. so, in order find generalized class of item, join has performed.
also, 1 of columns contains numeric value, 'w'. i'm trying find efficient query gives number of items within specific range of 'w', grouped generalized class. so, example, 1 of result rows give breakdown of number of items in class w < 500, 500 < w < 1500 , w > 1500.
right now, use pre-existing view joins generalized class result set, there's no need join in final query. final query uses trick found here calculate amount of items within specific range.
select generalized_class, sum(if(w between 1 , 500, 1, 0)) low, sum(if(w between 500 , 1500, 1, 0)) middle, sum(if(w > 1500, 1, 0)) high table_vw group generalized_class however, i'm not quite sure whether efficient way it. database large, entire thing takes on 5 minutes complete. imagine calculating sort of stuff expensive operation in general, i'm wondering whether use different approach remove load. anyone?
-edit- definition of view nothing spectacular, it's like
create view table_vw select d.id, d.class, c.generalized_class, <more fields>, (w_high/w_low)/2 w base_table d left outer join secondary_table c on (d.class = c.class) i'm pretty sure there's 1 more left outer join being made table, no rows table used (as said, it's pre-existing view, there's lot of stuff in isn't tailored query). join row 'class' indexed in secondary table.
the reason query takes long execute may doesn't use indexes (or doesn't use them @ all!). use explain see how mysql executes query.
http://dev.mysql.com/doc/refman/5.5/en/explain.html
another reason may mysql first computes entire result of view, uses compute result of whole query.
in case, solution merge view definition query.
Comments
Post a Comment