您的位置:首页 > 数据库 > MySQL

关于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 |

+—-+————-+——–+——+—————+———-+———+——-+——+———————–+

| 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
相关文章推荐