我有两个相当简单的查询。第一个查询
UPDATE mp_physical SET periodic_number = '' WHERE periodic_number is NULL;
Run Code Online (Sandbox Code Playgroud)
这是计划
duration: 0.125 ms plan:
Query Text: UPDATE mp_physical SET periodic_number = '' WHERE periodic_number is NULL;
Update on mp_physical (cost=0.42..7.34 rows=1 width=801)
-> Index Scan using "_I_periodic_number" on mp_physical (cost=0.42..7.34 rows=1 width=801)
Index Cond: (periodic_number IS NULL)
Run Code Online (Sandbox Code Playgroud)
第二个:
UPDATE observations_optical_temp SET designation = '' WHERE periodic_number is NULL;
Run Code Online (Sandbox Code Playgroud)
它的计划是:
duration: 2817.375 ms plan:
Query Text: UPDATE observations_optical_temp SET periodic_number = '' WHERE periodic_number is NULL;
Update on observations_optical_temp …Run Code Online (Sandbox Code Playgroud) postgresql performance index execution-plan temporary-tables postgresql-performance
我需要在包含数百万行的表上运行这些简单的查询:
SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123;
Run Code Online (Sandbox Code Playgroud)
SELECT COUNT(*) FROM "subscriptions" WHERE "subscriptions"."project_id" = 123 AND "subscriptions"."trashed_at" IS NULL;
Run Code Online (Sandbox Code Playgroud)
对于项目 123,两个查询的计数结果约为 5M。
我在 上有一个索引project_id,在 上还有另一个索引(project_id, trashed_at):
"index_subscriptions_on_project_id_and_created_at" btree (project_id, created_at DESC)
"index_subscriptions_on_project_id_and_trashed_at" btree (project_id, trashed_at DESC)
Run Code Online (Sandbox Code Playgroud)
问题是这两个查询都非常慢,每个查询大约需要 17 秒。
这些是以下结果EXPLAIN ANALIZE:
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=2068127.29..2068127.30 rows=1 width=0) (actual time=17342.420..17342.420 rows=1 loops=1)
-> Bitmap Heap Scan on subscriptions (cost=199573.94..2055635.23 rows=4996823 width=0) (actual time=1666.409..16855.610 rows=4994254 loops=1)
Recheck Cond: (project_id = 123)
Rows Removed …Run Code Online (Sandbox Code Playgroud) postgresql performance count postgresql-9.5 query-performance
我们在 CentOS 6.5 上运行 Postgres 9.4.4 并且有一个已经工作多年的 SELECT 查询,但在我们从 9.2 升级后停止工作并挂起(花了一段时间才注意到它,所以我不知道它是否是我们升级与否后立即)。
SELECT id || ':' || group_number AS uniq_id
FROM table_one
WHERE id || ':' || group_number NOT IN (
SELECT id || ':' || group_number
FROM table_two
)
AND id NOT IN (
SELECT id
FROM table_three
WHERE timestamp > NOW() - INTERVAL '30 days'
AND client_id > 0
);
Run Code Online (Sandbox Code Playgroud)
在所有表中id都是一个整数,但存储为character varying (15)(旧系统)。group_number存储为smallint.
table_two 的子查询返回大约 250 万条记录。的子查询table_three返回大约 2,500 条记录。如果单独运行,两者都在大约 …
我正在尝试调查为什么此查询的性能如此不确定。它可能需要 1 秒到 60 秒及以上的任何时间。查询的本质是选择一个“时间窗口”,并从该时间窗口内获取所有行。
这是有问题的查询,在大约 10 亿行的表上运行:
SELECT CAST(extract(EPOCH from ts)*1000000 as bigint) as ts
, ticks
, quantity
, side
FROM order_book
WHERE ts >= TO_TIMESTAMP(1618882633073383/1000000.0)
AND ts < TO_TIMESTAMP(1618969033073383/1000000.0)
AND zx_prod_id = 0
ORDER BY ts ASC, del desc;
Run Code Online (Sandbox Code Playgroud)
这就是表的创建方式
CREATE TABLE public.order_book
(
ts timestamp with time zone NOT NULL,
zx_prod_id smallint NOT NULL,
ticks integer NOT NULL,
quantity integer NOT NULL,
side boolean NOT NULL,
del boolean NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
TO_TIMESTAMP当我走整张桌子时,其中的值将继续向前滑动。以下是EXPLAIN ANALYZE两个不同时间窗口上相同查询的输出: …
我使用 Postgresql 9.4,我有一个名为 foo 的大表。我想搜索它,但如果搜索文本很短(例如“v”)或很长(例如“这是一个在表 foo% 上使用 gin 的搜索示例”),我的执行时间会很长。在这种情况下,我的索引被忽略。这是我的搜索查询:
EXPLAIN (ANALYZE, TIMING)
SELECT "foo".* FROM "foo" WHERE "foo"."locale" = 'de'
AND f_unaccent(foo.name) ILIKE f_unaccent('v%')
AND foo.configuration->'bar' @> '{"is":["a"]}'
LIMIT 100;
Run Code Online (Sandbox Code Playgroud)
这是我的索引:
CREATE INDEX index_foo_on_name_de_gin ON foo USING gin(f_unaccent(name) gin_trgm_ops) WHERE locale = 'de';
Run Code Online (Sandbox Code Playgroud)
为什么索引被忽略并使用seq scan和/或Bitmap heap scan?如何添加其他索引来解决此问题?
为什么它会重新检查?
Recheck Cond: ((f_unaccent((name)::text) ~~* 'v%'::text) AND ((locale)::text = 'de'::text))
Run Code Online (Sandbox Code Playgroud)
功能f_unaccent:
CREATE OR REPLACE FUNCTION f_unaccent(text)
RETURNS text AS
$func$
SELECT unaccent('unaccent', $1)
$func$ LANGUAGE sql IMMUTABLE …Run Code Online (Sandbox Code Playgroud) postgresql performance index index-tuning postgresql-9.4 postgresql-performance
postgresql ×5
performance ×4
index ×2
cache ×1
count ×1
explain ×1
index-tuning ×1
timescaledb ×1