我有一张桌子residences
...
我安装了trgm: create extension pg_trgm;
使用以下方法将三元组索引添加到grp
和name
列后:
CREATE INDEX residences_name_trgm ON residences USING GIN (name gin_trgm_ops);
CREATE INDEX residences_grp_trgm ON residences USING GIN (grp gin_trgm_ops);
Run Code Online (Sandbox Code Playgroud)
并检查一个简单查询的性能......似乎name
列上查询的总运行时间运行速度提高了大约 100 倍。为什么是这样?
EXPLAIN ANALYZE SELECT * FROM residences WHERE name ILIKE '%Sutton%';
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on residences (cost=36.02..47.90 rows=3 width=1872) (actual time=0.390..0.720 rows=21 loops=1)
Recheck Cond: ((name)::text ~~ '%Sutton%'::text)
-> Bitmap Index Scan on residences_name_trgm_gin (cost=0.00..36.02 rows=3 width=0) (actual time=0.354..0.354 rows=21 loops=1)
Index …
Run Code Online (Sandbox Code Playgroud) 首先看看StackOverflow上的这个问题。
我希望完成相同的任务,但我还需要ST_Union
向我的查询添加聚合函数(PostGIS )。
如何将答案DISTINCT
与聚合结合使用...
我试过了:
SELECT DISTINCT ON (name, zonedistrict_id)
ST_Union(geom) as geom, gid, name, zonedistrict_id, zonestyle_id, longname
FROM zones
ORDER BY name, zonedistrict_id, zonestyle_id;
Run Code Online (Sandbox Code Playgroud)
哪个尖叫:
column "zones.gid" must appear in the GROUP BY clause or be used in an aggregate function
这很奇怪,因为如果我删除ST_Union(geom) as geom,
,则查询有效。但它没有联合几何。