gpt4 book ai didi

mysql - 奇怪的行为 : less rows are scanned without index on the columns in where condition

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:33 25 4
gpt4 key购买 nike

我有一个名为 A 的表,它存储经度和纬度数据。并且有一个查询来获取其位置在某个地理矩形内的行,例如:

SELECT *
FROM A
WHERE A.longitude > -121.276052
AND A.longitude < -110.159143
AND A.latitude > 32.802275
AND A.latitude < 38.335916
ORDER BY A.id
LIMIT 100

A 只有 id 列有索引,这也是主键。我希望扫描大量行,因为 longitudelatitude 列上没有索引。然而,事实证明它只从 explain 的输出中扫描了 100 行。

(我还验证了输出行的 id 并非偶然是最小的。它们实际上在 id 范围的中间)

为什么mysql只用id索引就能准确识别出100行?

最佳答案

我不是 100% 确定问题是什么。在您的查询中,MySQL 正在扫描 order by 的索引——它以 id 顺序获取所有行。然后,它会查找数据页中的每一行,看它是否与 where 子句匹配。

据推测,索引中的前 100 行与 where 子句匹配。因此,MySQL 可以在 100 行后停止扫描。

关于mysql - 奇怪的行为 : less rows are scanned without index on the columns in where condition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36321001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com