测试验证datatime字段是否走索引
来源:互联网 发布:mac os ps 破解版 编辑:程序博客网 时间:2024/06/07 14:12
测试验证datatime字段是否走索引?
(product)root@localhost [lots]> show create table t_order\G
*************************** 1. row ***************************
Table: t_order
Create Table: CREATE TABLE `t_order` (
`ID` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`CUSTOMER_ID` varchar(50) DEFAULT NULL COMMENT '客户号',
.....................................................................
`CREATE_DATE` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
.....................................................................
PRIMARY KEY (`ID`),
.....................................................................
KEY `index_create_date` (`CREATE_DATE`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2360435 DEFAULT CHARSET=gbk COMMENT='订单表'
1 row in set (0.00 sec)
查看下面有走索引:
(product)root@localhost [lots]> explain select customer_id from t_order where create_date='2015-10-19 21:03:40';
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------+
| 1 | SIMPLE | t_order | ref | index_create_date | index_create_date | 6 | const | 1194 | NULL |
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------+
1 row in set (0.00 sec)
(product)root@localhost [lots]> explain select customer_id from t_order where create_date=date_format('2015-10-19 21:03:40',' %Y-%m-%d %H:%i:%S');
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------+
| 1 | SIMPLE | t_order | ref | index_create_date | index_create_date | 6 | const | 1194 | NULL |
+----+-------------+---------+------+-------------------+-------------------+---------+-------+------+-------+
1 row in set (0.00 sec)
为什么不会存在字段类型的隐式转换呢?
当你需要同时包含日期和时间信息的值时则使用DATETIME类型。MySQL以'YYYY-MM-DD HH:MM:SS'格式检索和显示DATETIME值。支持的范围为'1000-01-01 00:00:00'到'9999-12-31 23:59:59'
。
说明mysql本身就支持这种显示格式,且只能说明是上面SQL发生类型转换的是在右边的值部分'2015-10-19 21:03:40',只要左边字段不发生转换,就能用上索引。
测试oracle的行为:
alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
SQL> explain plan for select * from zeng_test where datelock ='2009-3-11';
Explained.
SQL> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
----------------------------------------------------------------------------------------------------
Plan hash value: 822430127
--------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 4 | 52 | 4 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| ZENG_TEST | 4 | 52 | 4 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | DATELOCK_IX1 | 4 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("DATELOCK"='2009-3-11')
14 rows selected
以上测试说明,mysql和oralce下只要左边字段不发生转换,就能用上索引。
- 测试验证datatime字段是否走索引
- 测试mysql单表排序是否有走索引
- MySQL里bool类型字段是否需要添加索引测试
- 验证索引列NULL不走索引
- Oracle直方图的深入理解-和测试-直返图直接影响sql是否走正确的索引?
- 某字段不走索引分析
- Select * 一定不走索引是否正确?
- JAVA 添加SQL datatime字段
- 时间字段是否适合建立索引
- 验证年龄字段是否为数字
- ORACLE验证字段值是否为数字
- 用反射验证字段是否为空
- 在oracel表中的一个datatime字段上建了索引,然后使用to_char函数进行检索,能否使用该索引?
- 字段数据类型对索引的影响测试
- datatime
- DataTime
- 类型转换导致执行计划不走索引测试案例
- 列中有索引而执行计划不走索引的原因之一的验证
- IOS 读二进制数据文件
- 剑指offer 两个链表的第一个公共结点
- ubuntu14.04中改变文件夹的图标
- jQuery EasyUI使用教程之创建简单窗口
- [转] CocoaPods详解之----使用篇
- 测试验证datatime字段是否走索引
- sqlserver 用 STUFF 和 for xml path 把查询结果连接成字符串
- POJ 3468-A Simple Problem with Integers(区间更新线段树)
- <!DOCTYPE html>重要性
- BootStrap响应式导航条
- JAVA 中Exception和Error 面试问题
- synchronized与static synchronized 的区别
- Dr.Elephant用户指南 - 【Dr.Elephant系列文章-2】
- Java 对象流ObjectInputStream和ObjectOutputStream类