假设我有一个表,其中有一个名为 的表上的bytea列,我该如何:datat
data。我正在使用 Postgresql 9.5。
使用 PostgreSQL 10.3。
CREATE TABLE tickets (
id bigserial primary key,
state character varying,
closed timestamp
);
CREATE INDEX "state_index" ON "tickets" ("state")
WHERE ((state)::text = 'open'::text));
Run Code Online (Sandbox Code Playgroud)
该表包含 1027616 行,其中 51533 行具有state = 'open'和closed IS NULL或 5%。
条件为 on 的查询state按预期使用索引扫描执行良好:
explain analyze select * from tickets where state = 'open';
Index Scan using state_index on tickets (cost=0.29..16093.57 rows=36599 width=212) (actual time=0.025..22.875 rows=37346 loops=1)
Planning time: 0.212 ms
Execution time: 25.697 …Run Code Online (Sandbox Code Playgroud) postgresql performance index postgresql-10 postgresql-performance
我有一个中等大小的“functionCalls”表(约 4M 行),由 2 列组成,input并且function(另一个表的两个 id):
Column | Type | Collation | Nullable | Default \n----------+---------+-----------+----------+---------\n input | integer | | not null | \n function | integer | | not null | \nIndexes:\n "functionCall_pkey" PRIMARY KEY, btree (input, function) CLUSTER\n "functionCallSearch" btree (function, input)\nForeign-key constraints:\n "fkey1" FOREIGN KEY (function) REFERENCES function(id) ON UPDATE CASCADE ON DELETE CASCADE\n "fkey2" FOREIGN KEY (input) REFERENCES input(id)\n\nRun Code Online (Sandbox Code Playgroud)\n\n我想找到与某个函数匹配的所有行,这就是我添加索引的原因functionCallSearch。这是我的查询:
Column | Type | Collation | …Run Code Online (Sandbox Code Playgroud) 我以前总是这样做:
SELECT column FROM table ORDER BY random() LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
对于大表,这令人难以忍受,慢得令人难以置信,以至于在实践中毫无用处。这就是为什么我开始寻找更有效的方法。人们推荐:
SELECT column FROM table TABLESAMPLE BERNOULLI(1) LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
虽然速度很快,但它也提供了毫无价值的随机性。它似乎总是选择相同的该死的记录,所以这也毫无价值。
我也试过:
SELECT column FROM table TABLESAMPLE BERNOULLI(100) LIMIT 1;
Run Code Online (Sandbox Code Playgroud)
它提供了更糟糕的随机性。它每次都选择相同的几条记录。这是完全没有价值的。我需要实际的随机性。
为什么仅选择随机记录显然如此困难?为什么它必须抓取每条记录然后对它们进行排序(在第一种情况下)?为什么“TABLESAMPLE”版本总是抓取相同的愚蠢记录?为什么它们不是随机的?当它一遍又一遍地选择相同的几条记录时,谁会想要使用这个“BERNOULLI”的东西?我不敢相信,经过这么多年,我仍然在询问随机记录……这是最基本的查询之一。
用于从 PG 中的表中抓取随机记录的实际命令是什么,该命令并没有慢到需要几秒钟才能获得一个体面大小的表?
将应用程序及其数据库从经典 PostgreSQL 数据库迁移到 Amazon Aurora RDS PostgreSQL 数据库(均使用 9.6 版本)后,我们发现特定查询在 Aurora 上的运行速度要慢得多——大约慢 10 倍在 PostgreSQL 上。
两个数据库都具有相同的配置,无论是硬件还是 pg_conf。
查询本身相当简单。它是从我们用 Java 编写的后端生成的,并使用 jOOQ 编写查询:
with "all_acp_ids"("acp_id") as (
select acp_id from temp_table_de3398bacb6c4e8ca8b37be227eac089
)
select distinct "public"."f1_folio_milestones"."acp_id",
coalesce("public"."sa_milestone_overrides"."team",
"public"."f1_folio_milestones"."team_responsible")
from "public"."f1_folio_milestones"
left outer join
"public"."sa_milestone_overrides" on (
"public"."f1_folio_milestones"."milestone" = "public"."sa_milestone_overrides"."milestone"
and "public"."f1_folio_milestones"."view" = "public"."sa_milestone_overrides"."view"
and "public"."f1_folio_milestones"."acp_id" = "public"."sa_milestone_overrides"."acp_id"
)
where "public"."f1_folio_milestones"."acp_id" in (
select "all_acp_ids"."acp_id" from "all_acp_ids"
)
Run Code Online (Sandbox Code Playgroud)
用temp_table_de3398bacb6c4e8ca8b37be227eac089是单个列的表,f1_folio_milestones(17万个条目)和sa_milestone_overrides(100万左右的条目)是具有在所有用于列索引类似设计的表LEFT OUTER JOIN。
temp_table_de3398bacb6c4e8ca8b37be227eac089 最多可以包含 5000 …
postgresql optimization execution-plan aws-aurora postgresql-performance
目前我正在使用一个看起来像这样的 postgres 表 (postgres12)
create table if not exists asset (
id text,
symbol text not null,
name text not null
primary key (id)
);
create table if not exists latest_value (
timestamp bigint,
asset text,
price decimal null,
market_cap decimal null,
primary key (asset),
foreign key (asset)
references asset (id)
on delete cascade
);
create table if not exists value_aggregation (
context aggregation_context,
timestamp bigint,
asset text,
price jsonb null,
market_cap jsonb null,
primary key (context, timestamp, asset),
foreign …Run Code Online (Sandbox Code Playgroud) 我有一个 PostgreSQL 11 数据库,其中包含多个数据库,其中一些包含经常插入和更新的表。最近写入 WAL 文件的数据量大幅增加(约 400%),尽管我相信插入数据库的数量仅增加了约 20%。
因此,我们不仅使用了更多的磁盘,而且我们的数据库性能现在似乎受到 WAL 所在磁盘的写入器性能的限制。
应用程序更新非常频繁,可能是应用程序中的某些内容(我们控制并能够更改)发生了变化,这导致插入/更新的效率降低,但我不知道我们如何会识别。自从这种行为改变开始以来,可能已经发生了很多变化,自从它第一次被注意到以来,可能已经发生了很多变化。
有没有办法确定哪些数据库/表/查询正在写入 WAL,以及(大约)数量?
我正在从 PostgreSQL 表中删除 750k 行中的 130k。
第一次,花了8个小时才完成删除查询。
第二次,我向表中添加了一个索引,并重建了该索引。现在用了3个小时完成删除查询
第三次,我添加了以下行:
alter table contact disable trigger ALL;
delete from contact where ....;
alter table contact enable trigger ALL;
Run Code Online (Sandbox Code Playgroud)
删除行花了不到一秒钟的时间。即使没有与当前和外键表关联的触发器。
即使没有与表关联的触发器,这种快速查询性能的原因可能是什么?数据库级别还有其他类型的触发器吗?
假设我有一个表,其描述如下:
create table my_table (
id serial,
create_date timestamp with time zone default now(),
data text
);
Run Code Online (Sandbox Code Playgroud)
和这样的查询:
select * from my_table
where create_date >= timestamp with time zone 'yesterday'
Run Code Online (Sandbox Code Playgroud)
理论上哪个索引会更快,为什么?
create index index_a on my_table (create_date);
create index index_b on my_table (create_date DESC);
Run Code Online (Sandbox Code Playgroud) 我正在尝试优化一个查询,该查询在 Postgres 12.7 上从未完成。需要几个小时甚至几天的时间才能使 CPU 达到 100%,并且永远不会返回:
SELECT "id", "counter", "item_id", "item_name", "type", "updated_time"
FROM "changes"
WHERE (type = 1 OR type = 3) AND user_id = 'kJ6GYJNPM4wdDY5dUV1b8PqDRJj6RRgW'
OR type = 2 AND item_id IN (SELECT item_id FROM user_items WHERE user_id = 'kJ6GYJNPM4wdDY5dUV1b8PqDRJj6RRgW')
ORDER BY "counter" ASC LIMIT 100;
Run Code Online (Sandbox Code Playgroud)
我随机尝试使用 UNION 重写它,我相信它是等效的。基本上查询中有两部分,一部分用于 type = 1 或 3,另一部分用于 type = 2。
(
SELECT "id", "counter", "item_id", "item_name", "type", "updated_time"
FROM "changes"
WHERE (type = 1 OR type = 3) AND user_id …Run Code Online (Sandbox Code Playgroud) postgresql union postgresql-12 postgresql-performance ugly-or
postgresql ×10
index ×3
performance ×3
optimization ×2
aws-aurora ×1
ddl ×1
foreign-key ×1
index-tuning ×1
random ×1
select ×1
trigger ×1
ugly-or ×1
union ×1