关于mysql中由于类型转换导致索引无法使用的问题
2017-01-23 16:42
1011 查看
mysql> create index idx_create_time on baixyu(create_time);
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from baixyu where create_time >’2017-01-23’;
+—-+————-+——–+——-+—————–+—————–+———+——+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——-+—————–+—————–+———+——+——+———————–+
| 1 | SIMPLE | baixyu | range | idx_create_time | idx_create_time | 6 | NULL | 1 | Using index condition |
+—-+————-+——–+——-+—————–+—————–+———+——+——+———————–+
1 row in set (0.00 sec)
这种的是用上索引了,不是说把字符转换成了日期类型,就不会使用索引。
mysql> select count(*) from baixyu where table_rows is not null;
+———-+
| count(*) |
+———-+
| 25344 |
+———-+
1 row in set (0.04 sec)
mysql> create index idx_rows on baixyu(table_rows);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from baixyu where table_rows=1;
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| 1 | SIMPLE | baixyu | ref | idx_rows | idx_rows | 9 | const | 767 | Using index condition |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
1 row in set (0.00 sec)
mysql> explain select * from baixyu where table_rows=’1’;
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| 1 | SIMPLE | baixyu | ref | idx_rows | idx_rows | 9 | const | 767 | Using index condition |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
mysql> explain select * from temp where table_rows=2;
+—-+————-+——-+——+—————+——+———+——+——-+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+—————+——+———+——+——-+————-+
| 1 | SIMPLE | temp | ALL | idx_rows | NULL | NULL | NULL | 39802 | Using where |
+—-+————-+——-+——+—————+——+———+——+——-+————-+
1 row in set (0.00 sec)
看到这种int类型转换到varchar的是用不上索引的
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from baixyu where create_time >’2017-01-23’;
+—-+————-+——–+——-+—————–+—————–+———+——+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——-+—————–+—————–+———+——+——+———————–+
| 1 | SIMPLE | baixyu | range | idx_create_time | idx_create_time | 6 | NULL | 1 | Using index condition |
+—-+————-+——–+——-+—————–+—————–+———+——+——+———————–+
1 row in set (0.00 sec)
这种的是用上索引了,不是说把字符转换成了日期类型,就不会使用索引。
mysql> select count(*) from baixyu where table_rows is not null;
+———-+
| count(*) |
+———-+
| 25344 |
+———-+
1 row in set (0.04 sec)
mysql> create index idx_rows on baixyu(table_rows);
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> explain select * from baixyu where table_rows=1;
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| 1 | SIMPLE | baixyu | ref | idx_rows | idx_rows | 9 | const | 767 | Using index condition |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
1 row in set (0.00 sec)
mysql> explain select * from baixyu where table_rows=’1’;
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| 1 | SIMPLE | baixyu | ref | idx_rows | idx_rows | 9 | const | 767 | Using index condition |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| temp | CREATE TABLE `temp` ( `TABLE_CATALOG` varchar(512) CHARACTER SET utf8 NOT NULL DEFAULT '', `TABLE_SCHEMA` varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '', `TABLE_NAME` varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '', `TABLE_TYPE` varchar(64) CHARACTER SET utf8 NOT NULL DEFAULT '', `ENGINE` varchar(64) CHARACTER SET utf8 DEFAULT NULL, `VERSION` bigint(21) unsigned DEFAULT NULL, `ROW_FORMAT` varchar(10) CHARACTER SET utf8 DEFAULT NULL, `TABLE_ROWS` varchar(22) DEFAULT NULL, `AVG_ROW_LENGTH` bigint(21) unsigned DEFAULT NULL, `DATA_LENGTH` bigint(21) unsigned DEFAULT NULL, `MAX_DATA_LENGTH` bigint(21) unsigned DEFAULT NULL, `INDEX_LENGTH` bigint(21) unsigned DEFAULT NULL, `DATA_FREE` bigint(21) unsigned DEFAULT NULL, `AUTO_INCREMENT` bigint(21) unsigned DEFAULT NULL, `CREATE_TIME` datetime DEFAULT NULL, `UPDATE_TIME` datetime DEFAULT NULL, `CHECK_TIME` datetime DEFAULT NULL, `TABLE_COLLATION` varchar(32) CHARACTER SET utf8 DEFAULT NULL, `CHECKSUM` bigint(21) unsigned DEFAULT NULL, `CREATE_OPTIONS` varchar(255) CHARACTER SET utf8 DEFAULT NULL, `TABLE_COMMENT` varchar(2048) CHARACTER SET utf8 NOT NULL DEFAULT '', KEY `idx_name` (`TABLE_NAME`), KEY `idx_name_schema` (`TABLE_NAME`,`TABLE_SCHEMA`), KEY `idx_create_time` (`CREATE_TIME`), KEY `idx_rows` (`TABLE_ROWS`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
mysql> explain select * from temp where table_rows=2;
+—-+————-+——-+——+—————+——+———+——+——-+————-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+——-+——+—————+——+———+——+——-+————-+
| 1 | SIMPLE | temp | ALL | idx_rows | NULL | NULL | NULL | 39802 | Using where |
+—-+————-+——-+——+—————+——+———+——+——-+————-+
1 row in set (0.00 sec)
看到这种int类型转换到varchar的是用不上索引的
相关文章推荐
- 由于mysql隐式类型转换导致的索引失效的问题
- 因为前缀索引问题导致mysql无法使用覆盖索引
- SQL中一些不经意隐式类型转换或者函数使用导致索引失效问题
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- 关于spring mvc拦截器拦截js文件等导致无法正常使用问题的解决
- 关于使用Spring声明式事务时,在类的内部方法互相调用时,Spring无法拦截内部方法调用,导致事务不起作用的问题研究
- 关于UseSubmitBehavior和OnClientClick同时使用,导致无法触发后台事件的问题
- 关于mysql导出数据库后,再进行还原时,视图无法使用的问题,请各位大神帮忙出招
- 关于CSS中内层使用float导致外层的背景无法显示的问题的解决
- 一个由于网络问题导致的服务无法使用——调试过程总结
- 关于spring mvc拦截器拦截js文件等导致无法正常使用问题的解决
- 关于mysql、数字类型之间转换的问题
- 解决由于使用代理服务器导致Foxmail无法收发邮件的问题
- 关于使用AJAX获取数据时,由于IE缓存而导致数据不更新,串数据的问题!
- Java--因为char类型无法强制转换负数导致IO读取文件无法停止问题
- 关于UseSubmitBehavior和OnClientClick同时使用,导致无法触发后台事件的问题
- 关于使用LayoutParams清除设置以及DateFormat无法正确转换格式化日期的问题
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- 关于2147217913 从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界 的问题解决方法
- paping使用来测试联通&网站由于tcp协议导致的无法通信问题超时问题