I tried to compare potential perfomance difference between covering b-tree index and simple b-tree index and was confused with EXPLAIN(ANALYZE,BUFFERS)
output.
Test environment
-- function to fill test table
CREATE OR REPLACE FUNCTION fillTable (n INTEGER)
RETURNS INTEGER AS $rowsCount$
DECLARE
counter INTEGER := 0 ;
BEGIN
IF (n < 1) THEN
RETURN 0 ;
END IF;
LOOP
EXIT WHEN counter = n ;
counter := counter + 1 ;
insert into key_value_test(key, value) VALUES (counter,counter);
END LOOP ;
return counter;
END ;
$rowsCount$
LANGUAGE plpgsql;
Test case for simple b-tree index
drop table key_value_test;
create table key_value_test
(
key bigint not null,
value integer not null,
test VARCHAR(100),
CONSTRAINT pk_key_value_test
PRIMARY KEY(key)
);
SELECT fillTable(1000000);
vacuum;
EXPLAIN( ANALYZE,BUFFERS ) SELECT key, value
FROM key_value_test
WHERE key = 740080;
Test run results:
Index Scan using pk_key_value_test on key_value_test (cost=0.42..8.44 rows=1 width=12) (actual time=0.013..0.014 rows=1 loops=1)
Index Cond: (key = 913680)
Buffers: shared hit=4
Planning Time: 0.062 ms
Execution Time: 0.026 ms
Test case for covering b-tree index
drop table if exists key_value_test;
create table key_value_test
(
key bigint not null,
value integer not null,
CONSTRAINT pk_key_value_test
PRIMARY KEY(key) include(value)
);
SELECT fillTable(1000000);
EXPLAIN( ANALYZE,BUFFERS ) SELECT key, value
FROM key_value_test
WHERE key = 740080;
vacuum;
EXPLAIN( ANALYZE,BUFFERS ) SELECT key, value
FROM key_value_test
WHERE key = 740080;
Test run results
Before vacuum:
Index Only Scan using pk_key_value_test on key_value_test (cost=0.43..8.45 rows=1 width=12) (actual time=0.127..0.128 rows=1 loops=1)
Index Cond: (key = 740080)
Heap Fetches: 1
Buffers: shared hit=4
Planning Time: 0.078 ms
Execution Time: 0.146 ms
After vacuum:
Index Only Scan using pk_key_value_test on key_value_test (cost=0.42..4.44 rows=1 width=12) (actual time=0.143..0.144 rows=1 loops=1)
Index Cond: (key = 740080)
Heap Fetches: 0
Buffers: shared hit=4
Planning Time: 0.273 ms
Execution Time: 0.156 ms
My qiestion is why Heap Fetches
shows only for index-only scans, but for index scans it information is hidden?
Now i see that i has same request count to shared buffers for simple index and covering index. So where is perfomance
impact for covering indexes in this situation? It realy strange, that i can't measure potential perfomance impact using EXPLAIN.