考虑PostgreSQL上的这张表,但有数千条记录:
\n\n| id | uuid | color | ... | deleted_at |\n|----|---------|----------|-----|---------------------|\n| 1 | 4fc1... | red | ... | 2020-01-01 13:00:00 |\n| 2 | 4fc1... | gray | ... | 2020-01-01 13:00:00 |\n| 3 | 4fc1... | blue | ... | null |\n| 4 | 4fc1... | red | ... | null |\n| 5 | 4fc1... | blue | ... | 2019-12-03 00:45:00 |\nRun Code Online (Sandbox Code Playgroud)\n\n目的:
\n\nid(自动增量)用作主键和主要挂钩,以与其他表一起使用 JOIN。uuid是用于识别记录的面向公众的值。deleted_at(timestamp|null) …每当我需要从表中返回随机记录并且性能很重要时,而不是:
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)
这速度快多了,但似乎不是很随机?看起来重复使用此方法时会返回很多相同的记录。是我一个人的问题,还是这种方法的随机性要低得多(因此用处不大)?
我正在尝试优化这个 SQL 查询:
select topics.id from "topics"
left join "articles_topics" on "topics"."id" = "articles_topics"."topic_id"
left join "articles" on "articles_topics"."article_id" = "articles"."id"
where not "topics"."type" = 'sport' and "articles"."image" is not null
group by "topics"."id"
having COUNT(articles.id) > 10
Run Code Online (Sandbox Code Playgroud)
这是完整的查询成本(我使用过EXPLAIN (ANALYZE, COSTS, VERBOSE, BUFFERS))
Finalize HashAggregate (cost=12881.12..12974.90 rows=2501 width=8) (actual time=209.037..210.463 rows=1381 loops=1)
Output: topics.id
Group Key: topics.id
Filter: (count(articles.id) > 10)
Rows Removed by Filter: 5672
Buffers: shared hit=8624
-> Gather (cost=12018.39..12843.61 rows=7502 width=16) (actual time=198.146..205.348 rows=10376 loops=1) …Run Code Online (Sandbox Code Playgroud) 我知道如何设置特定列的统计级别,即
alter table table_name alter column column_name set statistics 100;
Run Code Online (Sandbox Code Playgroud)
但是,如果我希望为特定表或特定模式设置统计级别,是否有一个 liner 命令?
我确信以前有人问过这个问题,但由于这个问题可以用多种不同的方式表达,因此很难找到正确的答案。
我有一个订单表,其中订单号有一个varchar字段,其格式为 4 位年份、破折号 (-) 和渐进数值。例如,它可能包含以下值:
SELECT number FROM orders ORDER BY number LIMIT 10;
number
----------
1999-13
2019-11
2020-1
2020-10
2020-100
2020-12
2020-2
2020-21
2020-3
2021-1
Run Code Online (Sandbox Code Playgroud)
我需要按年份对该字段进行排序,然后按渐进数字进行排序,预期结果如下:
number
----------
1999-13
2019-11
2020-1
2020-2
2020-3
2020-10
2020-12
2020-21
2020-100
2021-1
Run Code Online (Sandbox Code Playgroud)
我的问题是:
我想至少保留第一个答案尽可能与数据库无关(这就是为什么我没有包含数据库特定标签的原因),但是如果不同的 DBMS/版本可能有不同的最佳答案,那么我们假设 PostgreSQL 12。
系统表pg_stat_all_tables记录 autovacuum 守护进程最后一次清理表的时间。
我注意到一些性能问题与数据库中较大的表之一被清理之间可能存在关联。然而,由于我不知道吸尘过程的持续时间,所以我不能 100% 确定。
如何查明在任何给定的表上自动清理花费了多长时间?
我正在尝试返回重叠多边形的数量。问题是它抱怨我的“$1”标志:
Run Code Online (Sandbox Code Playgroud)Error is "SQL state: 42601", syntax error at "$1".
为什么是这样?我在这里完全是初学者,除了用符号引用参数之外,我无法在网上找到解决方案$。
CREATE OR REPLACE FUNCTION any_overlap (x text)
RETURNS integer AS $$
DECLARE amount INTEGER;
BEGIN
SELECT COUNT(*) INTO amount FROM $1 a
INNER JOIN $1 b ON
(a.polygon && b.polygon AND ST_Relate(a.polygon, b.polygon, '2********'))
WHERE a.ctid != b.ctid;
RETURN amount AS id;
END; $$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud) 我正在使用 postgres v,我正在尝试让多表级联删除工作。
然而,当我这样做时,1 个表/条目不会被删除,我一生都无法理解为什么。
这是我的 DDL(已提取,因此请原谅格式)。
create table public.common
(
created_at timestamp with time zone default CURRENT_TIMESTAMP,
updated_at timestamp with time zone default CURRENT_TIMESTAMP
);
create table public.installations
(
id text not null constraint installations_pkey primary key,
client_key text,
data jsonb
) inherits (public.common);
create table public.platforms
(
id text not null constraint platforms_pkey primary key,
jpd_url text,
jpd_key text
) inherits (public.common);
create table public.webhooks
(
id serial not null constraint webhooks_pkey primary key,
team_id text,
salt text …Run Code Online (Sandbox Code Playgroud) 我们考虑了一些选项来将我们的 Postgres 服务器从当前 ( 10) 版本升级到最新 ( 14) 版本。我们决定不在主服务器上做任何事情 - 因为我们需要使用链接选项(所以就地,而不是复制),并且如果我们在新的 postgres 服务器启动后看到问题,我们无法回滚。我们认为首先进行复制会更安全。
pg_upgrade因此,我们计划使用复制来进行升级。这是我们到目前为止的计划:
pg_upgrade复制,然后启动它(逻辑复制将继续)在继续之前我有一些问题:
streaming replication到logical replication升级前的机制?在这样做之前有什么需要考虑的吗?pg_upgrade完成?我知道这取决于各种参数,但是根据数据大小约为 600 GB 的事实进行粗略估计吗?由于这是复制所需的停机时间 - 所以没有备份。在继续升级之前,请寻求有关这些问题的一些指导。
我有一个包含超过 10.000.000 条记录的表,并且我正在创建一个返回大约 4436 条记录的查询。
碰巧它给我的印象是获取最后一条记录的查询成本非常高。
Index Scan using idx_name on task (cost=0.28..142102.57 rows=3470 width=34) (actual time=14.690..22.894 rows=4436 loops=1)
" Index Cond: ((situation = ANY ('{0,1,2,3,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}'::integer[])) AND (deadline < CURRENT_TIMESTAMP))"
Planning Time: 1.335 ms
JIT:
Functions: 5
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 1.654 ms, Inlining 0.000 ms, Optimization 1.214 ms, Emission 13.163 ms, Total 16.030 ms
Execution Time: 24.758 ms
Run Code Online (Sandbox Code Playgroud)
这个成本水平是否可以接受,或者这个指标是否需要改进?
指数:
CREATE INDEX idx_name ON task (situation, deadline, approved)
WHERE
deadline IS NOT …Run Code Online (Sandbox Code Playgroud) postgresql ×10
cascade ×1
dynamic-sql ×1
foreign-key ×1
index-tuning ×1
natural-sort ×1
optimization ×1
order-by ×1
performance ×1
plpgsql ×1
postgis ×1
primary-key ×1
statistics ×1
syntax ×1
upgrade ×1
uuid ×1