我在数据库中有一个表 t (PostgreSQL 10.4):
\d t;
Table "public.t"
Column | Type | Collation | Nullable | Default
----------+------------------------+-----------+----------+---------
sn | character varying(11) | | |
site | character varying(50) | | |
Indexes:
"site_2018_idx" btree (site), tablespace "indexspace"
"sn_2018_idx" btree (sn), tablespace "indexspace"
Run Code Online (Sandbox Code Playgroud)
我需要为特定站点找到不同的 'sn,我这样做:
SELECT DISTINCT sn FROM t WHERE site='a_b301_1' ORDER BY sn ;
Run Code Online (Sandbox Code Playgroud)
它可以工作,但速度很慢,返回 75 个不同的“sn”值大约需要 8 分钟!有没有办法加快速度?解释分析给出了这个输出:
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sort (cost=42873094.21..42873103.25 rows=3615 width=12) (actual time=190431.413..190431.417 rows=75 loops=1)
Output: sn
Sort Key: t.sn
Sort Method: quicksort …Run Code Online (Sandbox Code Playgroud) postgresql performance distinct postgresql-10 query-performance