我相信postgresql可以更快地处理我的查询,但每次尝试修改它都会让它变慢!
我有两张桌子:
我在:
上创建了索引这是查询
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
);
最昂贵的操作是Hash Join。您如何重新组织查询以获得更好的结果?
以下是表格的架构
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);
每张表中约有40 000行
索引已创建如下
def map = ["curr_dept":codes.get("ICU_DEPT").value];
def list = getActLists(visit,"TRAN",map,[]);
boolean flag = false;
return list?.size() != 0
有关信息,我在geometry_id&上尝试了不同的索引(btree& gin)。 statistics_id。
答案 0 :(得分:2)
我发现您的查询没有任何问题。
要检查的事项
(geometry.id = (statistics.id)::text)
是否都是字段相同的数据类型?WHERE statistics.id not like '97%';
。 LIKE '%me'
永远不会使用索引,但LIKE 'me%'
可能会使用索引。 Why doesnt use index? st_x(st_centroid(st_transform(geometry.geom, 2154))) AS x,
是一个功能,需要时间。需要变换坐标然后提取值。如果计算该值并将其存储在字段中,则会更好。要尝试的事情
首先是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
然后在x, y
表
geometry
列
SELECT statistics.*,
geometry.x,
geometry.y,
FROM statistics
JOIN geometry ON statistics.id = geometry.id
然后加入+ st_x + where like
并加入+ geometry.xy + where like
比较每个步骤之间的时间,以检查花费最多的时间。