我有如下结构的位置表之后变得缓慢:POSTGIS插入一段时间
CREATE TABLE location
(
id BIGINT,
location GEOMETRY,
CONSTRAINT location_pkey PRIMARY KEY (id, location),
CONSTRAINT enforce_dims_geom CHECK (st_ndims(location) = 2),
CONSTRAINT enforce_geotype_geom CHECK (geometrytype(location) = 'POINT'::TEXT OR location IS NULL),
CONSTRAINT enforce_srid_geom CHECK (st_srid(location) = 4326)
)
WITH (
OIDS=FALSE
);
CREATE INDEX location_geom_gist ON location
USING
GIST (location);
我运行下面的查询插入数据:
def insert_location_data(msisdn, lat, lon):
if not (lat and lon):
return
query = "INSERT INTO location (id, location) VALUES ('%s', ST_GeomFromText('POINT(%s %s)', 4326))"%(str(id), str(lat), str(lon))
try:
cur = get_cursor()
cur.execute(query)
conn.commit()
except:
tb = traceback.format_exc()
Logger.get_logger().error("Error while inserting location in sql: %s", str(tb))
return False
return True
我运行的代码块1000万次在一个循环中却有一百万插入插入速度急剧下降。当我重新启动脚本时,速度恢复正常,但它又下降了大约一百万个文档,并继续保持相同的趋势。我无法弄清楚为什么? 任何帮助。
使用扩展插入语句。在循环中准备查询,然后将其发送到PostgreSQL –
这里有一个关于在postgres上加载批量数据的指南:http://www.postgresql.org/docs/current/interactive/populate.html –
但是为什么情况在发生。查询起初速度很快,但随着时间的推移,速度正在下降。数据库大小不是问题,因为重新启动脚本会将速度恢复到最大值。 –