简体   繁体   English

MySQL查询需要很长时间

[英]Mysql query takes long time

Hello I have table with 500k records and folowing columns: 您好,我有500k记录和以下列的表:

id, id_route, id_point, lat, lng, distance, status id,id_route,id_point,lat,lng,距离,状态

I want to select id_routes which are inside radius from my defined point. 我想选择id_routes,它们位于我定义的点的半径内。

Thats no problem 那没问题

SELECT id_route 
FROM route_path
WHERE (((lat < 48.7210 + 2.0869) AND 
        (lat > 48.7210 - 2.0869)) AND 
       ((lng < 21.2578 + 2.0869) AND 
        (lng > 21.2578 - 2.0869))) 
GROUP BY id_route

But according PHPmyadmin it takes 0.2s. 但是根据PHPmyadmin,它需要0.2秒。 This is pretty to much since I am going to build huge query and this is just beginning. 因为我将要建立庞大的查询,所以这几乎就是全部,这才刚刚开始。

I have also index on id_route. 我在id_route上也有索引。

Primary key is id, schema is MyISAM 主键是id,架构是MyISAM

EXPLAIN of SELECT: SELECT的说明:

id select_type table type possible_keys key key_len ref rows Extra id select_type表的类型possible_keys键key_len参考行额外

1 SIMPLE route_path ALL NULL NULL NULL NULL 506902 Using where; 1 SIMPLE route_path ALL NULL NULL NULL NULL 506506使用where; Using temporary; 使用临时; Using filesort 使用文件排序

How can I reduce time, I think 500K is not su much records to make it so long? 我想如何减少时间,我认为500K记录不足以使时间这么长? Thanks 谢谢

如果查询需要更长的时间,并且已正确设置了索引,则需要功能强大的服务器来快速计算查询!

A 2-dimensional search is inherently slow. 二维搜索本质上是缓慢的。 The tools won't tell you how to improve this particular query. 这些工具不会告诉您如何改进此特定查询。

You seem to have no indexes in your table?? 您的表中似乎没有索引? You should at least try INDEX(lat) . 您至少应该尝试INDEX(lat) that will limit the effort to a stripe of about 4 degrees (in your example). 这样会将工作量限制为大约4度的条纹(在您的示例中)。 This probably includes thousands of rows. 这可能包括数千行。 Most of them are then eliminated by checking lng , but not until after fetching all of those thousands. 然后通过检查lng消除其中的大多数,但是直到获取了所有这些数千之后才删除。

So, you are tempted to try INDEX(lat, lng) only to find that it ignores lng . 因此,您很想尝试INDEX(lat, lng)只是发现它忽略了lng And perhaps it runs slower because the index is bigger. 也许它运行速度较慢,因为索引更大。

INDEX(lat, lng, id) and using a subquery to find the id s, then doing a self-join back to the table to do the rest of the work is perhaps the simplest semi-straightforward solution. INDEX(lat, lng, id)并使用子查询来查找id ,然后对表进行自联接以完成其余工作,这也许是最简单的半直截了当的解决方案。 This is slightly beneficial because that is a "covering index" for the subquery, and, although you scan thousands of rows in the index , you don't have to fetch many rows in the data . 这有点好处,因为这是子查询的“覆盖索引”,并且,尽管您扫描了索引中的数千行,但不必获取数据中的许多行。

Can it be made faster? 可以更快吗? Yes. 是。 However, the complexity is beyond the space available here. 但是,复杂性超出了此处可用的空间。 See Find the nearest 10 pizza parlors . 请参阅查找最近的10家比萨店 It involves InnoDB (to get index clustering), PARTITIONs (as crude 2D indexing) and modifications to the original data (to turn lat/lng into integers for PARTITION keys). 它涉及InnoDB(用于获取索引聚类),PARTITION(作为原始2D索引)以及对原始数据的修改(用于将经纬度变为PARITION关键字的lat / lng整数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM