select   case when createDate...case when
case when
endfrom  a这里createDate  是否需要建立索引?
(按照道理一般是在where语句后面加上索引 
但是这个判断在前面,所以也要加索引的哦)

解决方案 »

  1.   

    select  count(distinct id), case when..
    ....
    group byhis explain:
    +----+-------------+-------+--------+---------------------------------------------+---------+---------+------------------------+--------+---------------------------------+
    | id | select_type | table | type   | possible_keys                               | key     | key_len | ref                    | rows   | Extra                           |
    +----+-------------+-------+--------+---------------------------------------------+---------+---------+------------------------+--------+---------------------------------+
    |  1 | SIMPLE      | ul    | ALL    | idx_SessionLog,    idx_Id_lastime           | NULL    | NULL    | NULL                   | 183498 | Using temporary; Using filesort |
    |  1 | SIMPLE      | u     | eq_ref | PRIMARY                                     | PRIMARY | 4       | .ul.Id                   |      1 | Using where; Using index        |
    +----+-------------+-------+--------+---------------------------------------------+---------+---------+------------------------+--------+---------------------------------+
    2 rows in set 如何让 
    1 | SIMPLE      | ul    | ALL    | idx_SessionLog,    idx_Id_lastime           | NULL    | NULL    | NULL                   | 183498 | Using temporary; Using filesort 
    这里能走索引 
      

  2.   

    在select 和 from 之间的表达式是不会用到索引的。 (特例是,你的查询返回字段在索引中都已包含时会直接读索引而不去用数据文件)where , group , order (distinct 类似于 group ) 会利用到索引。