更快地创建SQLITE查询

问题描述 投票:0回答:2

我有一个超过2.570.000记录的SQLITE表,我将使用此查询选择数据...

select * from table_1 where City = 'Roma' and dist <= 0

......这就是结果......

pgid    City    ptid     osm_id         name                                        dist
4715    Roma    1   248528643   Pronto Soccorso Ospedale "Sandro Pertini"           0
4715    Roma    9   434692525   Aurelia Hospital                                    0
4715    Roma    24  1273194519  Pronto Soccorso Ospedale Oftalmico                  0
4715    Roma    64  2318096925  Pronto Soccorso                                     0
4715    Roma    130 4839842653  Pronto Soccorso Policlinico Tor Vergata             0
4715    Roma    190 5220345821  Pronto Soccorso Ospedale Bambino Gesù               0
4715    Roma    192 5220405958  Pronto Soccorso CTO "Andrea Alesini"                0
4715    Roma    194 5220418538  Pronto Soccorso Ospedale "Cristo Re"                0
4715    Roma    197 5220461914  Pronto Soccorso Ospadale "Giovanni Battista Grassi" 0
4715    Roma    198 5220473819  Pronto Soccorso Ospedale "Madre G. Vannini"         0
4715    Roma    202 5222071850  Pronto Soccorso Policlinico "Agostino Gemelli"      0
4715    Roma    203 5222084908  Pronto Soccorso Policlinico Casilino                0
4715    Roma    206 5222104766  Pronto Soccorso Ospedale "San Camillo"              0
4715    Roma    207 5222113675  Pronto Soccorso Ospedale "San Filippo Neri"         0
4715    Roma    208 5222142783  Pronto Soccorso Ospedale "San Giovanni Calibita"    0
4715    Roma    213 5222229247  Pronto Soccorso Ospedale "Sant'Andrea"              0
4715    Roma    214 5222235383  Pronto Soccorso Ospedale "Sant'Eugenio"             0

它工作但查询不是那么快(有时我第一次进入Sqlite时超过一分钟......),所以我要优化它。

我可以在某处创建索引吗?我可以用作唯一键的唯一字段是osm_id字段..所有其他字段不是唯一的

我是SQL的新手,所以我需要一些建议/例子将不胜感激

sql indexing sqlite
2个回答
2
投票

对于此查询:

select *
from table_1
where City = 'Roma' and dist <= 0

您可以创建索引:

create index idx_table1_city_dist on table_1(city, dist);

索引中列的顺序很重要。 city需要先行。


-1
投票

1-在City和dist列上创建非聚集索引,

2-如果你真的不需要所有的行,请不要使用select *,只需选择你需要的,

3-使用存储过程而不是从代码中执行文本命令,

4-有时使用分区可能会提高性能,qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.