Reputation: 3195
Here i need a solution for my problem. I have a table that contain 2 million records related with geo coordinates. To get a perticular row from the table, query execution time will be 24 seconds. Here i want a selection of above 500 records for my application aspects. 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
Upvotes: 1
Views: 444
Reputation: 116177
Your query seems wrong: why do you want to compare IP address as numeric value to anything? IP addresses are not something that you can meaningfully compare.
However, since you are using map data already (as you have lat, long, etc), that means you should be using spatial extensions. 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.
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. This will give you maximum performance possible. You can read more about spatial extensions here.
Upvotes: -1
Reputation: 7171
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 ...
.
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, ...);
Upvotes: 1
Reputation: 5439
You need to create indexes on start_ip
and end_ip
columns. 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. You can compare two explains before and after adding indexes to see how they are different.
Upvotes: 3