I have a table structure like this:
CREATE TABLE `test` ( `a` tinyint(3) unsigned DEFAULT 0, `b` tinyint(3) unsigned DEFAULT 0, `c` tinyint(3) unsigned DEFAULT 0, `d` tinyint(3) unsigned DEFAULT 0, `e` tinyint(3) unsigned DEFAULT 0 );
This has about 30 columns with some columns that have values from 0-200 (a, b), and some have only 5 values (0,1,2,3,4) (column cd). There is aprox. 120k rows per table.
To display the number of elements in a row, I use a query for each column:
select a, count(*) FROM test group by a; select b, count(*) FROM test group by b; select c, count(*) FROM test group by c; select d, count(*) FROM test group by d; select e, count(*) FROM test group by e;
The problem is that it will run 30 queries (one per column) and basically transfers the same data set every time.
Is there a better way to do this?
I tried with GROUP BY WITH ROLLUP, but this leads to a massive result set that is processed more slowly than every single request.
You can view the SQLfiddle data selection: http://sqlfiddle.com/#!2/a9fd8/1
performance mysql group-by
Nin
source share