简体   繁体   中英

Simple MySQL query in a relatively large table runs very slow

I have a database table with zipcodes, timezones, latitude, longitude and some other location data covering the entire world. There are 8,379,070 rows in the table. Over half of GB of data, probably. The table is called timezones .

When I try to run a query to get all records that have a postcode of "90210" like this: select * from timezones where postcode = 90210; the query returns 28 rows, but only after 7.73 seconds.

I tried adding an auto-incrementing primary key integer field, and indexing the table by postcode field, but nothing helps.

At this speed, the table will not be usable. It was supposed to be used for retrieving suggestions for an auto-complete zipcode field in a form on a website.

Is there any way I could make this run faster?


This is my table description:

+-----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+----------------+ | country | varchar(45) | YES | | NULL | | | region1 | varchar(45) | YES | | NULL | | | region2 | varchar(45) | YES | | NULL | | | region3 | varchar(45) | YES | | NULL | | | locality | varchar(45) | YES | | NULL | | | postcode | varchar(45) | YES | MUL | NULL | | | latitude | varchar(45) | YES | | NULL | | | longitude | varchar(45) | YES | | NULL | | | timezone | varchar(45) | YES | | NULL | | | utc | varchar(45) | YES | | NULL | | | dst | varchar(45) | YES | | NULL | | | idx | int(11) | NO | PRI | NULL | auto_increment | +-----------+-------------+------+-----+---------+----------------+ 12 rows in set (0.00 sec)

Keep the index on postcode :

create index idx_timezones_postcode on timezones(postcode);

Then, be sure that your types are correct when you write the query:

select tz.*
from timezones tz
where tz.postcode = '90210';

Type conversion can prevent the use of the index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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