繁体   English   中英

改进联接查询postgresql / postgis

[英]Improving join query postgresql/postgis

我相信postgresql可以更快地处理我的查询,但是每次尝试修改它都会使其变慢!

我有2张桌子:

  • 统计信息(id,field1,[...],field10)
  • 几何(id,geom)

我在创建索引:

  • statistics.id
  • geometry.id
  • 几何(st_x(st_centroid(st_transform(geom,2154))),st_y(st_centroid(st_transform(geom,2154))))

这是查询

EXPLAIN ANALYZE SELECT 
statistics.*,
st_x(st_centroid(st_transform(geometry.geom, 2154))) AS x,
st_y(st_centroid(st_transform(geometry.geom, 2154))) AS y

FROM statistics
 JOIN geometry ON statistics.id = geometry.id 

WHERE statistics.id not like '97%';

这是结果

Hash Join  (cost=1294.66..5158.10 rows=36593 width=342) (actual time=20.788..1085.257 rows=36552 loops=1)
Hash Cond: (geometry.id = (statistics.id)::text)
->  Seq Scan on geometry  (cost=0.00..2445.46 rows=36593 width=279) (actual time=0.010..25.271 rows=36597 loops=1)
    Filter: (id !~~ '97%'::text)
->  Hash  (cost=835.96..835.96 rows=36696 width=69) (actual time=19.892..19.892 rows=36696 loops=1)
    Buckets: 4096  Batches: 1  Memory Usage: 3780kB
    ->  Seq Scan on statistics  (cost=0.00..835.96 rows=36696 width=69) (actual time=0.005..6.871 rows=36696 loops=1)
Planning time: 0.401 ms
Execution time: 1088.612 ms

最昂贵的操作是哈希联接。 您将如何重组那里的查询以获得更好的结果?

下面是表的架构

CREATE TABLE "statistics" (
    "REG" integer,
    "DEP" character varying(10),
    "COM" character varying(50),
    "D03" integer,
    "D04" integer,
    "D05" integer,
    "D06" integer,
    "D07" integer,
    "D08" integer,
    "D09" integer,
    "D10" integer,
    "D11" integer,
    "D12" integer,
    "D13" integer,
    "id" text
);

CREATE TABLE geometry (  
    id text NOT NULL,
    id_geo numeric(10,0),
    cm_code character varying(3),
    name character varying(50),
    status character varying(20),
    lat integer,
    long integer,
    lat_centroid integer,
    long_centroid integer,
    z_ smallint,
    area numeric(10,0),
    population double precision,
    code_ct character varying(2),
    code_r character varying(1),
    code_dp character varying(2),
    name_dp character varying(30),
    code_rg character varying(2),
    geom geometry(MultiPolygon,4326),
    x real,
    y real
);

每个表中约有4万行

索引已创建如下

CREATE INDEX statistics_id_idx ON public.statistics USING btree (id COLLATE pg_catalog."default");
CREATE INDEX geometry_geom_idx ON public.geometry USING gist (geom);
CREATE INDEX geometry_id_gin2 ON public.geometry  USING gin (id COLLATE pg_catalog."default" gin_trgm_ops);

对于信息,我尝试了geometry_id和statistics_id的不同索引(btree和gin)。

我看不到您的查询有任何问题。

检查事项

  • (geometry.id = (statistics.id)::text)都是相同的数据类型吗?
  • WHERE statistics.id not like '97%'; LIKE '%me'永远不会使用索引,但是LIKE 'me%'可能会使用索引。 为什么不使用索引?
  • st_x(st_centroid(st_transform(geometry.geom, 2154))) AS x,是一个函数,需要花费时间。 需要先转换坐标,然后提取一个值。 计算该值并将其存储在字段中会更好。
  • 您的几何索引对该查询没有任何影响,因为您正在计算不搜索内容的值。
  • 如果您要进行perfom地理搜索,那么两者都不是正确的索引。 但是我们可以稍后再谈

尝试的事情

首先是where like

SELECT *
FROM statistics
WHERE statistics.id not like '97%';

然后只是join

SELECT statistics.*,
       geometry.geom
FROM statistics
JOIN geometry ON statistics.id = geometry.id 

然后加入+ st_x

SELECT statistics.*,
       st_x(st_centroid(st_transform(geometry.geom, 2154))) AS x,
       st_y(st_centroid(st_transform(geometry.geom, 2154))) AS y
FROM statistics
JOIN geometry ON statistics.id = geometry.id 

然后在geometry表中创建预先计算的x, y

SELECT statistics.*,
       geometry.x,
       geometry.y,
FROM statistics
JOIN geometry ON statistics.id = geometry.id 

然后加入+ st_x + where like然后加入+ geometry.xy +相同where like

比较每个步骤之间的时间,以检查花费最多的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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