简体   繁体   English

如何使我的查询选择速度超过200万条记录

[英]How to make my query selection faster over a 2 million records

Here i need a solution for my problem. 在这里,我需要一个解决我的问题。 I have a table that contain 2 million records related with geo coordinates. 我有一个表,其中包含200万条与地理坐标相关的记录。 To get a perticular row from the table, query execution time will be 24 seconds. 要从表中获取特定行,查询执行时间将为24秒。 Here i want a selection of above 500 records for my application aspects. 在这里,我想要为我的应用方面选择500以上的记录。 So any one kindly suggest any solutio n for making my query faster. 因此,任何人都建议任何解决方案,以使我的查询更快。

Query 询问

SELECT * FROM myProject.MAP where start_ip<=419297593 and end_ip>=419297593;

Table structur 表结构

Field,Type,Null,Key,Default,Extra
start_ip,"int(10) unsigned",YES,MUL,NULL,
end_ip,"int(10) unsigned",YES,MUL,NULL,
country_id,int(11),NO,,NULL,
lat,double,YES,,NULL,
lng,double,YES,,NULL,
id,int(11),NO,PRI,NULL,auto_increment

You need to create indexes on start_ip and end_ip columns. 您需要在start_ipend_ip列上创建索引。 For the syntax and different types of indexes, have a look at documentation 有关语法和不同类型的索引,请查看文档

CREATE INDEX id_index ON myproject.MAP (start_ip);

As mentioned in the comments, the Visual Explain also helps you determine how to improve queries. 正如评论中所提到的,Visual Explain还可以帮助您确定如何改进查询。 You can compare two explains before and after adding indexes to see how they are different. 您可以在添加索引之前和之后比较两个解释,以了解它们的不同之处。

For this particular query I would suggest a composite index like: 对于这个特定的查询,我建议一个复合索引,如:

CREATE INDEX map_ix1 ON myproject.MAP (end_ip, start_ip);

If that is a unique combination, create unique index ... . 如果这是一个独特的组合,请create unique index ...

Also, don't use * in the query. 另外,请勿在查询中使用*。 If you need all columns it is still better (for maintainability) to explicitly declare the columns. 如果您需要所有列,则显式声明列仍然更好(可维护性)。 You may also consider a covering index (an index that contains all columns accessed in the query): 您还可以考虑覆盖索引(包含查询中访问的所有列的索引):

CREATE INDEX map_ix1 ON myproject.MAP (end_ip, start_ip, ...);

Your query seems wrong: why do you want to compare IP address as numeric value to anything? 您的查询似乎有误:为什么要将IP地址作为数值比较? IP addresses are not something that you can meaningfully compare. IP地址不是您可以有意义地比较的东西。

However, since you are using map data already (as you have lat, long, etc), that means you should be using spatial extensions. 但是,由于您已经使用了地图数据(因为您有lat,long等),这意味着您应该使用空间扩展。 Good news is that spatial index allows you to execute queries of this exact kind very efficiently. 好消息是空间索引允许您非常有效地执行这种类型的查询。 You should already have created spatial index on (lat, long) or using (lat, long) as composite type POINT - this allows you to execute coordinate based search quickly. 您应该已经在(lat, long)或使用(lat, long)创建空间索引作为复合类型POINT - 这允许您快速执行基于坐标的搜索。

If you still want to compare IP addresses, you can treat tuple (start_ip, end_ip) as spatial POINT and execute search for this point belonging to range you specify. 如果您仍想比较IP地址,可以将元组(start_ip, end_ip)视为空间POINT并执行搜索属于您指定范围的此点。 This will give you maximum performance possible. 这将为您提供最佳性能。 You can read more about spatial extensions here . 您可以在此处详细了解空间扩展。

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

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