MySQL - ROUND() 是否使用索引?
MySQL 在使用 ROUND() 函数时是否使用任何索引?
考虑这个查询:
SELECT CPRID,ROUND(Dist*1000) AS `Distance`
FROM CPRaw_ID1
GROUP BY ROUND(1000*Dist)
ORDER BY ROUND(Dist*1000)
MySQL 还会在 Dist
列上使用我的索引吗?
Does MySQL make use of any indexes when using the ROUND()
function?
Consider this query:
SELECT CPRID,ROUND(Dist*1000) AS `Distance`
FROM CPRaw_ID1
GROUP BY ROUND(1000*Dist)
ORDER BY ROUND(Dist*1000)
Will MySQL Still use my Index on the Dist
column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先你可以使用 EXPLAIN 来查看 Dist 上的索引是否会被在查询中使用。我做了一个实验,发现事实并非如此。
实际上,一个建议是避免在其中使用函数条款。另外,这解释了 mysql 如何使用索引。
在您发布的查询中,我认为您可以尝试创建另一个表并将查询结果:
放入该临时表中并在距离上构建索引。然后索引将用于加速查询。
First you can use EXPLAIN to see whether index on Dist will be used in the query. I did an experiment and found out that is not.
Actually, an sugguestion is avoid using functions in where clause. Also, this explained how mysql uses indexes.
In the query you post, I think you can try to create another table and put the result of the query:
into that temporary table and build index on Distance. Then the index will be used to accelerate the query.
如果您存储以公里为单位的值并且想要将其转换为米,我认为这个查询就足够了:
通过这种方式,如果您定义了 Dist 的索引,您就可以使用索引。 ROUND 函数的第二个参数将标识您需要的小数位,如果将其设置为 0,则不会有小数,并且会自动对值进行四舍五入。
if your storing the values in kilometres and you want to convert it to metres I think this query would be enough:
in this manner you would able to use the index if you define the index for the Dist. The second parameter on the ROUND function will identify the decimal places you need if you set it to 0 there will be no decimal and it will automatically round the value.
不,它不会,你也有一个错误的查询。
no it won't and you do have a bad query too.