我不知道为什么在我的“城市”桌子上搜索这么慢。我的查询是寻找一个离城市约25公里的表格“城市”。我使用这个简单的查询,数据库大约需要20秒才能返回结果。
SELECT city_destination,distance FROM cities WHERE city_start='Wien' AND distance <= 25 ORDER BY distance ASC表引擎是InnoDB。这张桌子大约有几分。7百万行:
+--------------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_of_start | int(11) | NO | | NULL | |
| id_of_destination | int(11) | NO | | NULL | |
| city_start | text | NO | | NULL | |
| city_destination | text | NO | | NULL | |
| distance | double | NO | | NULL | |
+--------------------+-------------+------+-----+---------+----------------+有人能告诉我如何优化数据库或查询吗?
发布于 2022-04-15 20:52:25
对于这个查询,您应该使用一个city_start +距离索引
CREATE INDEX idx_citie_start_distance ON cities (city_start, distance);您还可以创建两个索引: city_start和另一个表示距离。这也应该很好。
https://serverfault.com/questions/1098720
复制相似问题