explain analyze select * from pg_class; QUERY PLAN --------------------------------------------------------------------------------------------------------- Seq Scan on pg_class (cost=0.00..10.92 rows=292 width=202) (actual time=0.009..0.049 rows=295 loops=1) Total runtime: 0.249 ms (2 rows)
explain analyze select * from pg_class limit 2; QUERY PLAN ------------------------------------------------------------------------------------------------------------- Limit (cost=0.00..0.07 rows=2 width=202) (actual time=0.014..0.014 rows=2 loops=1) -> Seq Scan on pg_class (cost=0.00..10.92 rows=292 width=202) (actual time=0.009..0.009 rows=2 loops=1) Total runtime: 0.132 ms (3 rows)
explain analyze select * from pg_class where relname ~ 'a'; QUERY PLAN --------------------------------------------------------------------------------------------------------- Seq Scan on pg_class (cost=0.00..11.65 rows=227 width=202) (actual time=0.030..0.294 rows=229 loops=1) Filter: (relname ~ 'a'::text) Rows Removed by Filter: 66 Total runtime: 0.379 ms (4 rows)
explain analyze select * from pg_class where oid = 1247; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------- Index Scan using pg_class_oid_index on pg_class (cost=0.15..8.17 rows=1 width=202) (actual time=0.007..0.007 rows=1 loops=1) Index Cond: (oid = 1247::oid) Total runtime: 0.077 ms (3 rows)
explain analyze select * from pg_class order by oid limit 10; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.15..1.67 rows=10 width=206) (actual time=0.017..0.029 rows=10 loops=1) -> Index Scan using pg_class_oid_index on pg_class (cost=0.15..44.53 rows=292 width=206) (actual time=0.014..0.026 rows=10 loops=1) Total runtime: 0.145 ms (3 rows)
explain analyze select * from pg_class where oid > 1247 order by oid limit 10; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.15..4.03 rows=10 width=206) (actual time=0.021..0.035 rows=10 loops=1) -> Index Scan using pg_class_oid_index on pg_class (cost=0.15..37.84 rows=97 width=206) (actual time=0.017..0.031 rows=10 loops=1) Index Cond: (oid > 1247::oid) Total runtime: 0.132 ms (4 rows)
explain analyze select * from pg_class where oid < 1247 order by oid desc limit 10; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=0.15..4.03 rows=10 width=206) (actual time=0.012..0.026 rows=10 loops=1) -> Index Scan Backward using pg_class_oid_index on pg_class (cost=0.15..37.84 rows=97 width=206) (actual time=0.009..0.022 rows=10 loops=1) Index Cond: (oid < 1247::oid) Total runtime: 0.119 ms (4 rows)
create table test (id serial primary key, i int4); CREATE TABLE insert into test (i) select random() * 1000000000 from generate_series(1,100000); INSERT 0 100000 vacuum analyze test; VACUUM
select * from test limit 10; id | i ----+----------- 1 | 546119592 2 | 253476978 3 | 235791031 4 | 654694043 5 | 187647296 6 | 709050245 7 | 210316749 8 | 348927354 9 | 120463097 10 | 5611946 (10 rows)
\d test Table "public.test" Column | Type | Modifiers --------+---------+--------------------------------------------------- id | integer | not null default nextval('test_id_seq'::regclass) i | integer | Indexes: "test_pkey" PRIMARY KEY, btree (id)
explain analyze select id from test order by id asc limit 10; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------ Limit (cost=0.29..0.55 rows=10 width=4) (actual time=0.039..0.042 rows=10 loops=1) -> Index Only Scan using test_pkey on test (cost=0.29..2604.29 rows=100000 width=4) (actual time=0.036..0.038 rows=10 loops=1) Heap Fetches: 0 Total runtime: 0.092 ms (4 rows)
explain analyze select * from test where i < 100000; QUERY PLAN ------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on test (cost=4.37..39.99 rows=10 width=8) (actual time=0.025..0.110 rows=13 loops=1) Recheck Cond: (i < 100000) -> Bitmap Index Scan on i1 (cost=0.00..4.37 rows=10 width=0) (actual time=0.013..0.013 rows=13 loops=1) Index Cond: (i < 100000) Total runtime: 0.154 ms (5 rows)
explain analyze select * from test where i < 5000000 or i > 950000000; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Bitmap Heap Scan on test (cost=107.36..630.60 rows=5323 width=8) (actual time=1.023..4.353 rows=5386 loops=1) Recheck Cond: ((i < 5000000) OR (i > 950000000)) -> BitmapOr (cost=107.36..107.36 rows=5349 width=0) (actual time=0.922..0.922 rows=0 loops=1) -> Bitmap Index Scan on i1 (cost=0.00..12.25 rows=527 width=0) (actual time=0.120..0.120 rows=491 loops=1) Index Cond: (i < 5000000) -> Bitmap Index Scan on i1 (cost=0.00..92.46 rows=4822 width=0) (actual time=0.799..0.799 rows=4895 loops=1) Index Cond: (i > 950000000) Total runtime: 4.765 ms (8 rows)
alter table test add column j int4 default random() * 1000000000; ALTER TABLE alter table test add column h int4 default random() * 1000000000; ALTER TABLE create index i2 on test (j); CREATE INDEX create index i3 on test (h); CREATE INDEX
explain analyze select * from test where j < 50000000 and i < 50000000 and h > 950000000; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------ Bitmap Heap Scan on test (cost=280.76..323.61 rows=12 width=16) (actual time=2.295..2.352 rows=11 loops=1) Recheck Cond: ((h > 950000000) AND (j < 50000000) AND (i < 50000000)) -> BitmapAnd (cost=280.76..280.76 rows=12 width=0) (actual time=2.278..2.278 rows=0 loops=1) -> Bitmap Index Scan on i3 (cost=0.00..92.53 rows=4832 width=0) (actual time=0.546..0.546 rows=4938 loops=1) Index Cond: (h > 950000000) -> Bitmap Index Scan on i2 (cost=0.00..93.76 rows=4996 width=0) (actual time=0.783..0.783 rows=5021 loops=1) Index Cond: (j < 50000000) -> Bitmap Index Scan on i1 (cost=0.00..93.96 rows=5022 width=0) (actual time=0.798..0.798 rows=4998 loops=1) Index Cond: (i < 50000000) Total runtime: 2.428 ms (10 rows)
Source: https://habr.com/ru/post/276973/
All Articles