几千万级别的表A:如下
Table A| Field          | Type         | Null | Key | Default           | Extra          |
+----------------+--------------+------+-----+-------------------+----------------+
| id             | int(10)      | NO   | PRI | NULL              | auto_increment | 
| insert_time    | timestamp    | NO   |     | CURRENT_TIMESTAMP |          
×××我想查询至2010-6-1日开始,每天插入多少条数据。要怎么查快?

解决方案 »

  1.   

    select date(insert_time),count(*) from tt where date(insert_time)>'2010-6-1' group by date(insert_time)在insert_time建立索引试试
      

  2.   

    如果 insert_time 表上没有索引的话,则只能是全表扫描。 没有任何办法来直接提高效率。或者可以考虑另外创建一个统计表(insert_day, cnt) 来存放每天的统计结果。然后每天半夜来更新这个表中的统计结果。
      

  3.   


    如果写程序来计算的话。有没有好的方法?比如:select id from A where id>=? and insert_time>='2010-06-01' limit 1;id1select id from A where id>=? and insert_time>='2010-06-02' limit 1;
    id2找出两个id。然后select count(*) as num from A where id>=id1 and id <=id2;
    有没有更好的方法?