MySQL:有索引和小文件排序更好还是没有索引和文件排序更好?

发布于 2024-09-24 22:52:46 字数 285 浏览 3 评论 0原文

我有一个大型数据库(180k+行并且增长很快)的位置数据,并将它们绘制在谷歌地图上。对于给定的视口,我只想提供 100 个适用点的样本。因此,数据库是按纬度/经度查询的,但如果我在这些行上放置索引,问题是 100 个点的样本将位于视图端口的底部或顶部(取决于索引的使用方式) 。如果不使用索引,这些点几乎随机地分散在视口中,这是更理想的。我可以通过第三个几乎随机的字段进行文件排序,对索引结果产生相同的效果。

所以,问题似乎是,哪个更好:对 180k+ 行进行无索引查询,或者对 4k 行和 4k 行等内容进行索引查询。进行文件排序?谢谢!

I have a large database (180k+ rows and growing fast) of location data and am plotting them on a google map. For a given view port, I just want to serve up a sample of 100 applicable points. The database is therefore queried by lat/lng, but if I put an index on these rows the problem is that the sample of 100 points will be either at the bottom or the top of the view port (depending on how the index is used). If no index is used, the points are pretty much randomly scattered across the view port, which is much more desirable. I can create the same effect on the indexed results by doing a filesort by a third, pretty much random field.

So, the issue seems to be, what is better: An unindexed query on 180k+ rows, or an indexed query which will look at something like 4k rows & do a filesort? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2024-10-01 22:52:46

您会发现许多反对使用“ORDER BY RAND()”的论点,尽管在这种情况下如果您对字段建立索引并且发现分析结果可以接受,那么它可能会很有用:

mysql> select id from table where id > 10000 and id < 20000 order by rand() limit 0,10;
+-------+
| id    |
+-------+
| 18560 | 
| 18408 | 
| 14058 | 
| 19090 | 
| 11100 | 
| 18945 | 
| 12656 | 
| 16549 | 
| 19321 | 
| 12003 | 
+-------+
10 rows in set (0.04 sec)

You'll find many arguments against using "ORDER BY RAND()", although it may be useful in this situation if you do index the field and you find the profiling results to be acceptable:

mysql> select id from table where id > 10000 and id < 20000 order by rand() limit 0,10;
+-------+
| id    |
+-------+
| 18560 | 
| 18408 | 
| 14058 | 
| 19090 | 
| 11100 | 
| 18945 | 
| 12656 | 
| 16549 | 
| 19321 | 
| 12003 | 
+-------+
10 rows in set (0.04 sec)
心的位置 2024-10-01 22:52:46

分析带索引和不带索引的查询。我在 dbForge Studio for MySQL 中使用探查器。

Profiling your query with index and without it. I use profiler in dbForge Studio for MySQL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文