可能重复:
在 Mysql 查询中引用整数的缺点?
我在 MYSql 数据库上有一个非常简单的名为 Device 的表。
+-----------------------------------+--------------+------+-----+----------------+
| Field | Type | Null | Key | Extra |
+-----------------------------------+--------------+------+-----+----------------+
| DTYPE | varchar(31) | NO | | |
| id | bigint(20) | NO | PRI | auto_increment |
| dateCreated | datetime | NO | | |
| dateModified | datetime | NO | | |
| phoneNumber | varchar(255) | YES | MUL | |
| version | bigint(20) | NO | | |
| oldPhoneNumber | varchar(255) | YES | | |
+-----------------------------------+--------------+------+-----+----------------+
该表有超过 100K 的记录。我正在运行一个非常简单的查询
select * from AttDevice where phoneNumber = 5107357058;
此查询几乎需要 4-6 秒,但是当我稍微更改此查询时,如下所示。
select * from AttDevice where phoneNumber = '5107357058';
执行几乎不需要时间。请注意,phoneNumber 列是 varchar。我不明白为什么前一种情况需要更长的时间而后来不需要。这两个查询之间的区别在于单引号。如果是这样,MYSQL 是否会以不同的方式处理这些查询,那为什么?
编辑 1
我使用EXPLAIN
并得到了以下输出,但不知道如何解释这两个结果。
mysql> EXPLAIN select * from AttDevice where phoneNumber = 5107357058;
+----+-------------+-----------+------+---------------------------------------+------+---------+------+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------------------------------+------+---------+------+---------+-------------+
| 1 | SIMPLE | Device | ALL | phoneNumber,idx_Device_phoneNumber | NULL | NULL | NULL | 6482116 | Using where |
+----+-------------+-----------+------+---------------------------------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)
mysql> EXPLAIN select * from AttDevice where phoneNumber = '5107357058';
+----+-------------+-----------+------+---------------------------------------+-------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------+------+---------------------------------------+-------------+---------+-------+------+-------------+
| 1 | SIMPLE | Device | ref | phoneNumber,idx_Device_phoneNumber | phoneNumber | 258 | const | 2 | Using where |
+----+-------------+-----------+------+---------------------------------------+-------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
有人可以解释一下 EXPLAIN 查询输出中存在的键、key_len 和行吗?