select a,sum(b) from c GROUP BY a;
select a from c;
Assuming that the amount of data in table c is millions, how to optimize these two pieces of data to make the execution faster.
Thank you
三叔2017-06-14 10:52:39
There is no way to optimize from the index. If you use group by and do not set the where condition, mysql has already read the entire table. And if there are millions of data, I personally do not recommend using sum directly to count the total number of each a in the table. The recommended optimization is this.
1. Establish scheduled tasks, temporary tables, and statistical tables
2. Read a certain amount of data regularly, record the statistical information of that data into the statistical table, and write the last id value of a certain amount of data as a record. Temporary tables. The id will be used as the condition when reading next time.
This segmented statistics can satisfy your two optimization conditions
黄舟2017-06-14 10:52:39
Create a composite index (a, b), both SQL will perform index scan
In addition, statistical SQL must be done in a special report library, and timed and segmented statistics