I have a PostgreSQL 9.0.12 database with a master table and 2 child tables. My tables:
CREATE TABLE test2 (
id serial PRIMARY KEY,
coll character varying(15),
ts timestamp without time zone
);
CREATE INDEX ON test2(ts);
CREATE TABLE test2_20150812 (
CHECK ( ts >= timestamp '2015-08-12' AND ts < timestamp '2015-08-13' )
) INHERITS (test2);
CREATE TABLE test2_20150811 (
CHECK ( ts >= timestamp '2015-08-11' AND ts < timestamp '2015-08-12' )
) INHERITS (test2);
CREATE INDEX ON test2_20150812(ts);
CREATE INDEX ON test2_20150811(ts);
VACUUM FULL ANALYZE;
My select queries' explain result (There is not any rows in db):
EXPLAIN (ANALYZE, BUFFERS) select * from test2 WHERE ts >= '2015-08-11' ORDER BY ts DESC;
Sort (cost=89.87..92.09 rows=887 width=31) (actual time=0.245..0.245 rows=0 loops=1)
Sort Key: public.test2.ts
Sort Method: quicksort Memory: 17kB
Buffers: shared read=2
-> Result (cost=0.00..46.44 rows=887 width=31) (actual time=0.087..0.087 rows=0 loops=1)
Buffers: shared read=2
-> Append (cost=0.00..46.44 rows=887 width=31) (actual time=0.078..0.078 rows=0 loops=1)
Buffers: shared read=2
-> Seq Scan on test2 (cost=0.00..0.00 rows=1 width=31) (actual time=0.007..0.007 rows=0 loops=1)
Filter: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
-> Bitmap Heap Scan on test2_20150812 test2 (cost=7.68..23.22 rows=443 width=31) (actual time=0.024..0.024 rows=
0 loops=1)
Recheck Cond: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
Buffers: shared read=1
-> Bitmap Index Scan on test2_20150812_ts_idx (cost=0.00..7.57 rows=443 width=0) (actual time=0.016..0.016
rows=0 loops=1)
Index Cond: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
Buffers: shared read=1
-> Bitmap Heap Scan on test2_20150811 test2 (cost=7.68..23.22 rows=443 width=31) (actual time=0.033..0.033 rows=
0 loops=1)
Recheck Cond: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
Buffers: shared read=1
-> Bitmap Index Scan on test2_20150811_ts_idx (cost=0.00..7.57 rows=443 width=0) (actual time=0.026..0.026
rows=0 loops=1)
Index Cond: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
Buffers: shared read=1
Total runtime: 0.320 ms
(23 rows)
However if I change column coll from character varying(15) to character varying(255), and do these steps again;
CREATE TABLE test2 (
id serial PRIMARY KEY,
coll character varying(255),
ts timestamp without time zone
);
Explain output is (There is not any rows in db):
EXPLAIN (ANALYZE, BUFFERS) select * from test2 WHERE ts >= '2015-08-11' ORDER BY ts DESC;
Sort (cost=42.47..43.18 rows=287 width=157) (actual time=0.028..0.028 rows=0 loops=1)
Sort Key: public.test2.ts
Sort Method: quicksort Memory: 17kB
-> Result (cost=0.00..30.75 rows=287 width=157) (actual time=0.020..0.020 rows=0 loops=1)
-> Append (cost=0.00..30.75 rows=287 width=157) (actual time=0.015..0.015 rows=0 loops=1)
-> Seq Scan on test2 (cost=0.00..0.00 rows=1 width=157) (actual time=0.003..0.003 rows=0 loops=1)
Filter: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
-> Seq Scan on test2_20150812 test2 (cost=0.00..15.38 rows=143 width=157) (actual time=0.002..0.002 rows=0 loops
=1)
Filter: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
-> Seq Scan on test2_20150811 test2 (cost=0.00..15.38 rows=143 width=157) (actual time=0.002..0.002 rows=0 loops
=1)
Filter: (ts >= '2015-08-11 00:00:00'::timestamp without time zone)
Total runtime: 0.063 ms
(12 rows)
Is there any way for using indexes on the child tables in this new condition?