MySQL 在主键上搜索时使用了 ALL 类型
MySQL used ALL Type while searching on Primary Key
我的 table 架构:
我上面的 table 有 ~10L 数据。在使用 EXPLAIN 时,它显示为,
由此,type显示ALL,Extra显示Using其中 和 行 不在 O(1) 中。但是,对于主键搜索,type 应该是 const,rows 在 O(1) ??我无法找出问题所在,这导致查询速度变慢。
由于你的id列是varchar,搜索时需要提供String。
试试,id= '123456'
原因:
由于您将 varchar 列与 Int 进行比较,它会首先将所有行转换为 Int,然后将其与 123456(int) 进行匹配。
您的 id
字段是 varchar,而您将要查找的值作为数字传递。
这意味着 mysql 必须执行隐式数据转换和 will not be able to use the index 以查找值:
For comparisons of a string column with a number, MySQL cannot use an
index on the column to look up the value quickly. If str_col is an
indexed string column, the index cannot be used when performing the
lookup in the following statement:
SELECT * FROM tbl_name WHERE str_col=1;
The reason for this is that
there are many different strings that may convert to the value 1, such
as '1', ' 1', or '1a'.
将您的 id
字段转换为数字或将值作为字符串传递。
我的 table 架构:
我上面的 table 有 ~10L 数据。在使用 EXPLAIN 时,它显示为,
由此,type显示ALL,Extra显示Using其中 和 行 不在 O(1) 中。但是,对于主键搜索,type 应该是 const,rows 在 O(1) ??我无法找出问题所在,这导致查询速度变慢。
由于你的id列是varchar,搜索时需要提供String。
试试,id= '123456'
原因: 由于您将 varchar 列与 Int 进行比较,它会首先将所有行转换为 Int,然后将其与 123456(int) 进行匹配。
您的 id
字段是 varchar,而您将要查找的值作为数字传递。
这意味着 mysql 必须执行隐式数据转换和 will not be able to use the index 以查找值:
For comparisons of a string column with a number, MySQL cannot use an index on the column to look up the value quickly. If str_col is an indexed string column, the index cannot be used when performing the lookup in the following statement:
SELECT * FROM tbl_name WHERE str_col=1;
The reason for this is that there are many different strings that may convert to the value 1, such as '1', ' 1', or '1a'.
将您的 id
字段转换为数字或将值作为字符串传递。