字段使用函数索引被抑制

 在索引字段上使用函数,该字段的索引将会被抑制。如下案例:
查看表结构:

点击(此处)折叠或打开

  1. mysql> show create table test06 \G
  2. *************************** 1. row ***************************
  3.        Table: test06
  4. Create Table: CREATE TABLE `test06` (
  5.   `id` bigint(11) NOT NULL DEFAULT '0',
  6.   `u_id` bigint(11) NOT NULL,
  7.   `openid` varchar(100) DEFAULT NULL,
  8.   `unionid` varchar(100) DEFAULT NULL,
  9.   `username` varchar(100) NOT NULL,
  10.   `password` varchar(100) NOT NULL,
  11.   `create_time` datetime NOT NULL,
  12.   KEY `idx_test03_id` (`id`),
  13.   KEY `idx_test03_name` (`username`),
  14.   KEY `idx_test06_crea_time` (`create_time`)
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
使用函数进行查询:

点击(此处)折叠或打开

  1. mysql> select count(*) from test06 where date(create_time)=curdate();
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 0 |
  6. +----------+
  7. 1 row in set (1.00 sec)
不使用函数:

点击(此处)折叠或打开

  1. mysql> select count(*) from test06 where create_time=date_format(curdate(),'%Y-%m-%d');
  2. +----------+
  3. | count(*) |
  4. +----------+
  5. | 0 |
  6. +----------+
  7. 1 row in set (0.03 sec)
  可以看出:查询时间变快很多。
 对比一下执行计划:

点击(此处)折叠或打开

  1. mysql> explain select count(*) from test06 where date(create_time)=curdate();
  2. +----+-------------+--------+-------+---------------+----------------------+---------+------+---------+--------------------------+
  3. | id | select_type | table  | type  | possible_keys | key                  | key_len | ref  | rows    | Extra                    |
  4. +----+-------------+--------+-------+---------------+----------------------+---------+------+---------+--------------------------+
  5. | 1  | SIMPLE      | test06 | index | NULL          | idx_test06_crea_time | 5       | NULL | 2009559 | Using where; Using index |
  6. +----+-------------+--------+-------+---------------+----------------------+---------+------+---------+--------------------------+
  7. 1 row in set (0.00 sec)

  8. mysql> explain select count(*) from test06 where create_time=date_format(curdate(),'%Y-%m-%d');
  9. +----+-------------+--------+------+----------------------+----------------------+---------+-------+------+-------------+
  10. | id | select_type | table  | type | possible_keys    key | key_len              | ref     | rows  | Extra              |
  11. +----+-------------+--------+------+----------------------+----------------------+---------+-------+------+-------------+
  12. | 1  | SIMP        | test06 | ref  | idx_test06_crea_time | idx_test06_crea_time | 5       | const | 1    | Using index |
  13. +----+-------------+--------+------+----------------------+----------------------+---------+-------+------+-------------+

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30135314/viewspace-2144996/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30135314/viewspace-2144996/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值