search

Home  >  Q&A  >  body text

mysql - sql statement optimization issues

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

女神的闺蜜爱上我女神的闺蜜爱上我2762 days ago1075

reply all(4)I'll reply

  • 三叔

    三叔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

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-14 10:52:39

    Let’s add the index to field A first

    reply
    0
  • 某草草

    某草草2017-06-14 10:52:39

    How to optimize GROUP BY in MySQL

    reply
    0
  • 黄舟

    黄舟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

    reply
    0
  • Cancelreply