小编nic*_*nda的帖子

为什么使用INT来选择包含比使用字符串慢得多的数字的Varchar索引?

我有一个包含数千行的表,其中包含一个包含数字的Varchar列.尽管讨论了为什么这个列不是数字类型,但从该表中选择行显示出一种奇怪的行为.

虽然该列上有索引,但使用数字字符串查找行比使用Ints(0.54秒)快得多(0.01秒).这是什么原因?似乎无法转换和使用索引的值...

我忽略了什么吗?看起来它没有强制转换为将其用于索引?我是否必须提供有关索引使用的提示,或者是否有数据库切换来完成此操作?或者,如果我误解了Explain输出,为什么它会慢得多?

表布局显示示例:

CREATE TABLE `example` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `stuff` varchar(45) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_stuff` (`stuff`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Run Code Online (Sandbox Code Playgroud)

这里使用String作为索引:

explain select * from example where stuff='200';
----+-------------+---------+------+---------------+-----------+---------+-------+------+--------------------------+
| id | select_type | table   | type | possible_keys | key       | key_len | ref   | rows | Extra                    |
+----+-------------+---------+------+---------------+-----------+---------+-------+------+--------------------------+
|  1 | SIMPLE      | example | ref  | idx_stuff     | idx_stuff | 137     | const |    1 | Using where; Using …
Run Code Online (Sandbox Code Playgroud)

mysql indexing innodb database-performance

4
推荐指数
2
解决办法
2669
查看次数

标签 统计

database-performance ×1

indexing ×1

innodb ×1

mysql ×1