我在 Ubuntu 12.04 上使用 PostgreSQL 9.1。
我需要在一个时间范围内选择记录:我的表time_limits有两个timestamp字段和一个integer属性。我的实际表中还有其他列与此查询无关。
create table (
start_date_time timestamp,
end_date_time timestamp,
id_phi integer,
primary key(start_date_time, end_date_time,id_phi);
Run Code Online (Sandbox Code Playgroud)
该表包含大约 200 万条记录。
像下面这样的查询花费了大量的时间:
select * from time_limits as t
where t.id_phi=0
and t.start_date_time <= timestamp'2010-08-08 00:00:00'
and t.end_date_time >= timestamp'2010-08-08 00:05:00';
Run Code Online (Sandbox Code Playgroud)
所以我尝试添加另一个索引 - PK的倒数:
create index idx_inversed on time_limits(id_phi, start_date_time, end_date_time);
Run Code Online (Sandbox Code Playgroud)
我的印象是性能有所提高:访问表中间记录的时间似乎更合理:介于 40 到 90 秒之间。
但是对于时间范围中间的值,它仍然是几十秒。在针对表格末尾时(按时间顺序),还有两次。
我explain analyze第一次尝试得到这个查询计划:
Bitmap Heap Scan on time_limits (cost=4730.38..22465.32 rows=62682 width=36) (actual time=44.446..44.446 rows=0 loops=1)
Recheck …Run Code Online (Sandbox Code Playgroud) 我station_logs在 PostgreSQL 9.6 数据库中有一个表:
Column | Type |
---------------+-----------------------------+
id | bigint | bigserial
station_id | integer | not null
submitted_at | timestamp without time zone |
level_sensor | double precision |
Indexes:
"station_logs_pkey" PRIMARY KEY, btree (id)
"uniq_sid_sat" UNIQUE CONSTRAINT, btree (station_id, submitted_at)
Run Code Online (Sandbox Code Playgroud)
我试图level_sensor根据submitted_at, 对于每个station_id. 大约有 400 个唯一station_id值,每个station_id.
创建索引之前:
EXPLAIN ANALYZE
SELECT DISTINCT ON(station_id) station_id, submitted_at, level_sensor
FROM station_logs ORDER BY station_id, submitted_at DESC;
Run Code Online (Sandbox Code Playgroud)
唯一(成本=4347852.14..4450301.72行=89宽度=20)(实际时间=22202.080..27619.167行=98循环=1) -> Sort …
postgresql performance greatest-n-per-group postgresql-9.6 query-performance
我的数据库(Postgresql 10)具有以下架构:
CREATE TABLE "PulledTexts" (
"Id" serial PRIMARY KEY,
"BaseText" TEXT,
"CleanText" TEXT
);
CREATE TABLE "UniqueWords" (
"Id" serial PRIMARY KEY,
"WordText" TEXT
);
CREATE TABLE "WordTexts" (
"Id" serial PRIMARY KEY,
"TextIdId" INTEGER REFERENCES "PulledTexts",
"WordIdId" INTEGER REFERENCES "UniqueWords"
);
CREATE INDEX "IX_WordTexts_TextIdId" ON "WordTexts" ("TextIdId");
CREATE INDEX "IX_WordTexts_WordIdId" ON "WordTexts" ("WordIdId");
Run Code Online (Sandbox Code Playgroud)
一些示例数据:
INSERT INTO public."PulledTexts" ("Id", "BaseText", "CleanText") VALUES
(1, 'automate business audit', null),
(2, 'audit trial', null),
(3, 'trial', null),
(4, 'audit', null),
(5, 'fresh …Run Code Online (Sandbox Code Playgroud) postgresql performance subquery relational-division postgresql-performance
Postgres 文档说:
使用索引来强制唯一约束可以被视为不应直接访问的实现细节。但是,应该注意没有必要在唯一的列上手动创建索引;这样做只会复制自动创建的索引。
基于此,如果我想要一个列上的表达式索引并且还希望该列是唯一的,那么case 2下面会更好,因为它可以使用单个索引完成上述操作。而case 1由于唯一约束而自动创建一个索引,而另一个因为我需要小写索引而自动创建?
正如@Colin'tHart 指出的那样,这两种情况不一样。我应该在不使用lower()表达式的情况下发布这个问题。在那种情况下,我的理解是 aCREATE UNIQUE INDEX比唯一约束和简单索引更好。
基于此,如果我想要text_pattern_ops在列上使用运算符类(例如)的索引并且还希望该列是唯一的,那么case 2下面会更好,因为它可以使用单个索引完成上述操作。而case 1会因为唯一约束而自动创建一个索引,而另一个因为我需要不同的运算符类而自动创建?
情况1:
CREATE TABLE book (
id SERIAL PRIMARY KEY,
name text NOT NULL,
CONSTRAINT book_name_key UNIQUE (name)
);
CREATE INDEX book_name_like ON book (name text_pattern_ops);
Run Code Online (Sandbox Code Playgroud)
案例2:
CREATE TABLE book (
id SERIAL PRIMARY KEY,
name text NOT NULL
);
CREATE UNIQUE INDEX book_name_like ON book (name text_pattern_ops);
Run Code Online (Sandbox Code Playgroud)