我对一个将近6000条记录表进行了20次不同条件的count查询,结果花了3,4秒,但是这些count结果都放在同个页面的.3,4秒太慢了,如何才能提速,求高手帮忙

解决方案 »

  1.   

    做了公共部分好是好了点,但数据一大,查询又慢下来了.
    6000条
    SELECT COUNT(DISTINCT item.id) FROM item WHERE (((item.id) IN (1,2...,600)) 
    AND ((item.status) = (0)))
    +----+-------------+-------+------+---------------+------+---------+------+-----
    -+-------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows
     | Extra       |
    +----+-------------+-------+------+---------------+------+---------+------+-----
    -+-------------+
    |  1 | SIMPLE      | item  | ALL  | PRIMARY       | NULL | NULL    | NULL | 6089
     | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+-----
    -+-------------+查询用时0.5秒左右
    为什么type是ALL,不是index
      

  2.   

    SELECT COUNT(DISTINCT item.id) FROM item WHERE item.id BETWEEN 1 AND 600
    AND item.status = 0
    在哪个字段上建立的索引
      

  3.   

    item.id 是主键 这个上会产生索引的啊 item表里还有一个item.guid字段上也建立索引啦
      

  4.   

    看看能不能用这种方式
    select sum(case when (condition1 is true) then 1 else 0 end) count1,
           sum(case when (condition2 is true) then 1 else 0 end) count2
        from ....
      

  5.   

    SELECT COUNT(DISTINCT item.id) FROM item WHERE (((item.id) IN (1,2...,600)) AND ((item.status) = (0))) 
    item.id建立一个索引
    item.id和item.status建立一个联合索引
      

  6.   


    create table auto_id ().....;
    SELECT COUNT(DISTINCT a.id) FROM item  as a, auto_id as b where a.id = b.id
    AND a.status = 0 
      

  7.   

    哎,楼上的方法是好的,但是我用sqlobject 无法实现啊.因为整个项目是python做的,sqlobject只有95%的SQL能全部正确地处理,谁精通SQLOBJECT啊,高手救命啊.先谢谢各位的热心帮助了