关于mysql中由于类型转换导致索引无法使用的问题
来源:互联网 发布:python定义json字符串 编辑:程序博客网 时间:2024/06/11 17:26
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 |
+—-+————-+——–+——+—————+———-+———+——-+——+———————–+
| 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隐式类型转换导致的索引失效的问题
- mysql sql中类型转换引起的索引无法使用的问题
- 因为前缀索引问题导致mysql无法使用覆盖索引
- SQL中一些不经意隐式类型转换或者函数使用导致索引失效问题
- 关于android studio 由于版本问题导致低版本studio项目无法导入高版本的studio的项目中。
- 解决由于使用代理服务器导致Foxmail无法收发邮件的问题
- 类型转换导致的问题
- 由于MSSQLSERVER服务导致无法登录的问题
- 关于CSS中内层使用float导致外层的背景无法显示的问题的解决方法
- 关于CSS中内层使用float导致外层的背景无法显示的问题的解决
- 由于空间不足导致Mysql无法启动
- 关于MVC中无法将类型为“System.Int32”的对象强制转换为类型“System.String”的问题。
- mysql 类型转换对索引的影响
- 在使用CommonComplexAdapter时遇到的类型无法转换问题
- 如何避免mysql 主从同步中由于数据记录找不到和主键重复错误导致的同步异常问题
- 关于mysql、数字类型之间转换的问题
- 由于计算机磁盘硬件配置出现问题,导致 Windows 无法启动的解决方法 由于计算机磁盘硬件配置出现问题,导致 Windows 无法启动
- Linux 使用mysql简单教程
- Hibernate-Usage(basis)(hibernate基本用法)
- Python爬虫/文本处理/科学计算/机器学习——工具包总结
- 南邮离散数学实验三-偏序关系中盖住关系的求取及格论中有补格的判定
- 2016 总结记
- 关于mysql中由于类型转换导致索引无法使用的问题
- LeetCode 231. Power of Two
- CodeForces 59AWord 水题
- Spring自动检测并装配bean
- 远程调用的几种方式
- 【PAT】1004. Counting Leaves (30)
- 在linux环境中编译JNI的文件为.so包
- 积分
- CodeForces 59BFortune Telling