insert into sta_ip (url,ip,count,stadate) 
select url,ip,count(ip),'2007-05-11 00' 
from web_log 
where time like '%2007-05-11 00%'  group by url,ip order by url;web_log表里有至少100W条记录以上,使用的是mysql数据库web_log表是当天类似于apache日志表
sta_ip 表是用来每小时统计某一url的独立IP计数。

解决方案 »

  1.   

    呵呵,估计每次的执行时间大约在20秒以上吧。如果可以的话,可以这样做
    在web_log的time 、url、ip字段分别加索引(ip加索引影响并不是很大)。
    语句变更为:
    insert into sta_ip (url,ip,count,stadate) 
    select url,ip,count(ip),'2007-05-11 00' 
    from web_log 
    where time like '2007-05-11 00%'  group by url,ip order by url;如果上述语句许可,估计执行时间会是 现在执行时间的 1/100到 1/1000 
    楼主可以试试看。然后再给分。
      

  2.   

    where time like '%2007-05-11 00%'这样会出事的……
      

  3.   

    加索引是正解,不加索引,mysql数据量到了4k以上,就比较慢了.
      

  4.   

    where time like '%2007-05-11 00%' 应该不会报错,
    虽然没有试过,但感觉应该不会报错。
    唯一的缺点就是,速度可能有些慢。
      

  5.   

    但是,如果只加索引,SQL 语句不变的话,还不如不加time字段的索引呢。
      

  6.   


    explain
    insert into sta_ip (url,ip,count,stadate) 
    select url,ip,count(ip),'2007-05-11 00' 
    from web_log 
    where time like '%2007-05-11 00%'  group by url,ip order by url;
    分析一下,就可以知道要不要加索引,在那个字段加.
    另外,使用like的确比较慢,你可以试试使用正则表达式或者全文搜索,看看效率有没有提高.
      

  7.   

    explain执行计划看一下。
    like效率不会太高的。