Select DATE_FORMAT(JoinDate,'%Y-%m-%d') as JoinDate,sum(user_info.IsActivate = 1) AS ActivedUser,count(0) AS TotalUser 
from user_info where JoinDate like '2011-01%' 
group by DATE_FORMAT(JoinDate,'%y-%m-%d')  Order By JoinDate desc LIMIT 0,100我想根据JoinDate来Group By,但是JoinDate是带小时和分钟的,如果只是Group By JoinDate的话没有什么意义.
但是如果group by DATE_FORMAT(JoinDate,'%y-%m-%d')这样的话,查询的时候索引又无法用到.请高手指教.在线等候!!!!!!!!

解决方案 »

  1.   


    mysql> show index from user_info;
    +-----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | Table     | Non_unique | Key_name  | Seq_in_index | Column_name | Collation |Cardinality | Sub_part | Packed | Null | Index_type | Comment |
    +-----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    | user_info |          0 | PRIMARY   |            1 | ID          | A         |      30126 |     NULL | NULL   |      | BTREE      |         |
    | user_info |          0 | UserEmail |            1 | UserEmail   | A         |      30126 |     NULL | NULL   |      | BTREE      |         |
    | user_info |          0 | UserID    |            1 | UserID      | A         |      30126 |     NULL | NULL   |      | BTREE      |         |
    | user_info |          1 | JoinDate  |            1 | JoinDate    | A         |      30126 |     NULL | NULL   | YES  | BTREE      |         |
    +-----------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
    4 rows in set (0.02 sec)
      

  2.   

    where JoinDate like '2011-01%' 这里可以优化一下,换成 where JoinDate between '2011-01-01'  and '2011-01-31' 这样如果你的JoinDate有索引,则可以被利用。没有索引,则创建JoinDate的索引。
      

  3.   

    未使用Date_fromat()的索引情况,mysql> explain Select JoinDate,sum(user_info.IsActivate = 1) AS ActivedUser,coun
    t(0) AS TotalUser
        -> from user_info where JoinDate like '2011-01%'
        -> group by JoinDate  Order By JoinDate desc LIMIT 0,100;
    +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+
    | id | select_type | table     | type  | possible_keys | key      | key_len | ref  | rows | Extra       |
    +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+
    |  1 | SIMPLE      | user_info | index | JoinDate      | JoinDate | 9       | NULL |  100 | Using where |
    +----+-------------+-----------+-------+---------------+----------+---------+------+------+-------------+
    1 row in set, 1 warning (0.02 sec)
    使用后的情况:mysql> explain Select DATE_FORMAT(JoinDate,'%Y-%m-%d') as JoinDate,sum(user_info
    .IsActivate = 1) AS ActivedUser,count(0) AS TotalUser
        -> from user_info where JoinDate like '2011-01%'
        -> group by DATE_FORMAT(JoinDate,'%y-%m-%d')  Order By JoinDate desc LIMIT 0
    ,100;
    +----+-------------+-----------+------+---------------+------+---------+------+-------+----------------------------------------------+
    | id | select_type | table     | type | possible_keys | key  | key_len | ref  |rows  | Extra                                        |
    +----+-------------+-----------+------+---------------+------+---------+------+-------+----------------------------------------------+
    |  1 | SIMPLE      | user_info | ALL  | JoinDate      | NULL | NULL    | NULL |30126 | Using where; Using temporary; Using filesort |
    +----+-------------+-----------+------+---------------+------+---------+------+-------+----------------------------------------------+
    1 row in set, 1 warning (0.00 sec)
      

  4.   

    JoinDate 这个上面加了索引就差不多了.
    或则试下 IsAcJoinDate ,tivate  复合索引;
      

  5.   

    这里就这样写也能用到索引.主要是Group By的时候调用了date_format()函数.!这有没有其他什么办法.!
      

  6.   

    一定要用的话,新建字段, 替换此字段为DATE_FORMAT(JoinDate,'%Y-%m-%d')
    建立此字段索引
      

  7.   

    Select DATE_FORMAT(JoinDate,'%Y-%m-%d') as JoinDate,sum(user_info.IsActivate = 1) AS ActivedUser,count(0) AS TotalUser 
    from user_info where JoinDate like '2011-01%' 
    group by JoinDate  Order By JoinDate desc LIMIT 0,100这个看看.前面你已经把 DATE_FORMAT(JoinDate,'%Y-%m-%d') as JoinDate 
    group by 就不要再用date_format函数了.
      

  8.   

    1. where JoinDate like '2011-01%' 是用不到索引的。
    2. group by DATE_FORMAT(JoinDate,'%y-%m-%d') 这种没有办法,MYSQL只能把所有的计算出来 再使用。这里最多是用group by date(JoinDate) 加快一点运算。
      

  9.   

    这个不行.我试过了.前面转换了,后面还是得要date_format()不然得话,数据不能统计,日期后面的时分秒不同就会有多条记录.
      

  10.   

    DATE_FORMAT(JoinDate,'%y-%m-%d')似乎有重复,
    可以为其建一个冗余列,然后建索引,然后把like替换为between ......