开发者

mysql group by performance tuning

select field1,count(*) from table where $condition group by field1
se开发者_开发技巧lect field2,count(*) from table where $condition group by field2

Basically that's what I'm doing the job now,is there a way to optimize the performance so that MySQL doesn't need to search two times to group by for the where clause?


If the table is large and $condition is 'rare', it might help to create a temporary table in memory. This way, you group it twice but filter it only once.

CREATE TEMPORARY TABLE temp ENGINE=MEMORY
 select field1,field2 from table where $condition;

select field1,count(*) from temp group by field1;
select field1,count(*) from temp group by field2;


No magic bullet here... the key upon which the aggregation takes place is distinct, so SQL needs to iterated over two different lists. To make this a bit more evident, ask yourself how you would like the data returned: all field1 first, then all field 2, or intertwined, or possibly "pivoted" (but how?..)

To avoid an extra "trip" to the server we could get these two results set returned together, or we could even group the two, using UNION ALL (and being careful to add a prefix column to to know what is what), but this latter solution would end up taxing the server a bit more if anything.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜