I have created a table with two columns and created index for the first column. Then inserted 2 million data using generate series function for first column (2nd column is having null). And executed below query
explain select * from mytable;
Getting cost as 19116.00 (something like this)
Then I am removing 1 million record from the mytable and running the explain query again, but getting more cost
Enabled vacuum parameters
track_counts = on
autovacuum = on
log_autovacuum_min_duration = 0
I could not understand, why cost is getting increased (expecting less cost). Is there any other parameter needs to be enabled?
(Using Postgres 9.1)
Edit 1
create table mytable (c1 numeric(19,0), c2 numeric(19,0));
explain analyze select * from mytable;
"Seq Scan on mytable (cost=0.00..21.60 rows=1160 width=40) (actual time=0.002..0.002 rows=0 loops=1)"
"Total runtime: 0.033 ms"
create index c1_idx on mytable using btree (c1);
insert into mytable (c1) values (generate_series(1,2000000))
explain analyze select * from mytable;
"Seq Scan on mytable (cost=0.00..19116.00 rows=1026600 width=40) (actual time=0.048..251.834 rows=2000000 loops=1)"
"Total runtime: 293.915 ms"
select pg_size_pretty(pg_table_size('mytable')) --"69 MB"
delete from mytable where c1 between 1 and 1000000
explain analyze select * from mytable;
"Seq Scan on mytable (cost=0.00..28850.00 rows=2000000 width=26) (actual time=356.616..458.278 rows=1000000 loops=1)"
"Total runtime: 478.645 ms"
(Actually, cost increased and still table size is same)
Edit 2
After runs vacuum on the table
"Seq Scan on mytable (cost=0.00..18850.00 rows=1000000 width=26) (actual time=31.308..117.244 rows=1000000 loops=1)"
"Total runtime: 138.911 ms"
Cost reduced as expected, but got another doubt on it. When mytable is having only 1M record cost is less than above.
truncate mytable;
insert into mytable (c1) values (generate_series(1000001,2000000))
explain analyze select * from mytable;
"Seq Scan on mytable (cost=0.00..10841.25 rows=641625 width=26) (actual time=0.058..121.894 rows=1000000 loops=1)"
"Total runtime: 154.454 ms"
Old cost is around 18K where for the same rows around 10K. Kindly ignore if its changing the topic