mysql - sql statement optimization issues
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-06-14 10:50:39
0
4
916
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

女神的闺蜜爱上我
女神的闺蜜爱上我

reply all (4)
三叔

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

    给我你的怀抱

    Let’s add the index to field A first

      某草草

      How to optimize GROUP BY in MySQL

        黄舟

        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

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!