Why aren'y my CHECK CONSTRAINTS on child tables being utilized by the planner to reduce the size of the plan?
I have a table that is partitioned by month and is used for holding apache log information. When I run EXPLAIN for a simple query which has a WHERE clause on the same field as the partition CHECKs, I get the same plan whether constraint_exclusion is on or off. Below is the master table info and two examples of child tables info for Feb and Mar of 2010. Overall, there is a child table for each month from Feb of 2010 through June of 2011. On average, each child table has around 100M records. Also below is the output of EXPLAIN for a simple query, once with constraint_exclusion on and once with it off. Unfortunately, the CHECK constraints aren't being used to reduce the plan size. Is this due to the fact that request_dt datatype is TIMESTAMP WITH TIME ZONE but the CHECK constraints are DATES? Any other thoughts? I have not yet created indexes on request_dt, but according to the documentation that isn't necessary. I'll be adding them, but I wouldn't think they should impact the use of the CHECK constraints.
I'm using Postgres 8.3.6.
spatial_data=# \d rpt.websvcs_logs
Table "rpt.websvcs_logs"
Column | Type | Modifiers
--------------------------+-----------------------------+---------------
id | bigint |
ins_ts | timestamp without time zone | default now()
server | text |
host | text |
request_dt | timestamp with time zone |
method | text |
url | text |
api_method | text |
api_key | text |
geo_type | text |
geo_name | text |
radius | text |
lat | text |
long | text |
id_param | text |
state | text |
max | text |
sort_by | text |
sort_dir | text |
rpp | text |
page | text |
ver | text |
output | text |
http_ver | text |
status | text |
size | text |
x_forwarded_for | text |
referrer | text |
agent | text |
accept_encoding | text |
processing_time_sec | text |
processing_time_microsec | text |
spatial_data=# \d rpt.websvcs_logs_201102
Table "rpt.websvcs_logs_201102"
Column | Type | Modifiers
--------------------------+-----------------------------+---------------
id | bigint | not null
ins_ts | timestamp without time zone | default now()
server | text |
host | text |
request_dt | timestamp with time zone |
method | text |
url | text |
api_method | text |
api_key | text |
geo_type | text |
geo_name | text |
radius | text |
lat | text |
long | text |
id_param | text |
state | text |
max | text |
sort_by | text |
sort_dir | text |
rpp | text |
page | text |
ver | text |
output | text |
http_ver | text |
status | text |
size | text |
x_forwarded_for | text |
referrer | text |
agent | text |
accept_encoding | text |
processing_time_sec | text |
processing_time_microsec | text |
Indexes:
"pk_websvcs_logs_201102_id" PRIMARY KEY, btree (id)
Check constraints:
"request_dt" CHECK (request_dt >= '2011-02-01'::date AND request_dt < '2011-03-01'::date)
Inherits: rpt.websvcs_logs
spatial_data=# \d rpt.websvcs_logs_201103
Table "rpt.websvcs_logs_201103"
Column | Type | Modifiers
--------------------------+-----------------------------+---------------
id | bigint | not null
ins_ts | timestamp without time zone | default now()
server | text |
host | text |
request_dt | timestamp with time zone |
method | text |
url | text |
api_method | text |
api_key | text |
geo_type | text |
geo_name | text |
radius | text |
lat | text |
long | text |
id_param | text |
state | text |
max | text |
sort_by | text |
sort_dir | text |
rpp | text |
page | text |
ver | text |
output | text |
http_ver | text |
status | text |
size | text 开发者_运维技巧 |
x_forwarded_for | text |
referrer | text |
agent | text |
accept_encoding | text |
processing_time_sec | text |
processing_time_microsec | text |
Indexes:
"pk_websvcs_logs_201103_id" PRIMARY KEY, btree (id)
Check constraints:
"request_dt" CHECK (request_dt >= '2011-03-01'::date AND request_dt < '2011-04-01'::date)
Inherits: rpt.websvcs_logs
spatial_data=# SET constraint_exclusion = on;
SET
spatial_data=# EXPLAIN SELECT COUNT(*) FROM rpt.websvcs_logs WHERE request_dt = DATE '2011-03-05';
QUERY
PLAN
------------------------------------------------------------------------------------------------------
Aggregate (cost=85738875.50..85738875.52 rows=1 width=0)
-> Append (cost=0.00..85738236.41 rows=255636 width=0)
-> Seq Scan on websvcs_logs (cost=0.00..11.00 rows=1 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201002 websvcs_logs (cost=0.00..564425.36 rows=1387 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201003 websvcs_logs (cost=0.00..1546537.50 rows=4287 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201004 websvcs_logs (cost=0.00..2528697.60 rows=9248 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201005 websvcs_logs (cost=0.00..3164403.20 rows=12885 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201006 websvcs_logs (cost=0.00..4476196.10 rows=12035 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201007 websvcs_logs (cost=0.00..4470579.60 rows=9543 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201008 websvcs_logs (cost=0.00..4881312.70 rows=11071 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201009 websvcs_logs (cost=0.00..4433474.70 rows=11005 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201010 websvcs_logs (cost=0.00..5419184.20 rows=13605 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201011 websvcs_logs (cost=0.00..5562311.50 rows=15424 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201012 websvcs_logs (cost=0.00..5543114.80 rows=14961 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201101 websvcs_logs (cost=0.00..7320972.20 rows=23008 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201102 websvcs_logs (cost=0.00..7413710.90 rows=23898 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201103 websvcs_logs (cost=0.00..8754694.20 rows=27241 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201104 websvcs_logs (cost=0.00..9292596.80 rows=30848 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201105 websvcs_logs (cost=0.00..9148734.80 rows=30727 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201106 websvcs_logs (cost=0.00..1217213.25 rows=4456 width=0)
Filter: (request_dt = '2011-03-05'::date)
spatial_data=# SET constraint_exclusion = off;
SET
spatial_data=# EXPLAIN SELECT COUNT(*) FROM rpt.websvcs_logs WHERE request_dt = DATE '2011-03-05';
QUERY PLAN
------------------------------------------------------------------------------------------------------
Aggregate (cost=85738875.50..85738875.52 rows=1 width=0)
-> Append (cost=0.00..85738236.41 rows=255636 width=0)
-> Seq Scan on websvcs_logs (cost=0.00..11.00 rows=1 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201002 websvcs_logs (cost=0.00..564425.36 rows=1387 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201003 websvcs_logs (cost=0.00..1546537.50 rows=4287 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201004 websvcs_logs (cost=0.00..2528697.60 rows=9248 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201005 websvcs_logs (cost=0.00..3164403.20 rows=12885 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201006 websvcs_logs (cost=0.00..4476196.10 rows=12035 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201007 websvcs_logs (cost=0.00..4470579.60 rows=9543 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201008 websvcs_logs (cost=0.00..4881312.70 rows=11071 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201009 websvcs_logs (cost=0.00..4433474.70 rows=11005 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201010 websvcs_logs (cost=0.00..5419184.20 rows=13605 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201011 websvcs_logs (cost=0.00..5562311.50 rows=15424 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201012 websvcs_logs (cost=0.00..5543114.80 rows=14961 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201101 websvcs_logs (cost=0.00..7320972.20 rows=23008 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201102 websvcs_logs (cost=0.00..7413710.90 rows=23898 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201103 websvcs_logs (cost=0.00..8754694.20 rows=27241 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201104 websvcs_logs (cost=0.00..9292596.80 rows=30848 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201105 websvcs_logs (cost=0.00..9148734.80 rows=30727 width=0)
Filter: (request_dt = '2011-03-05'::date)
-> Seq Scan on websvcs_logs_201106 websvcs_logs (cost=0.00..1217213.25 rows=4456 width=0)
Filter: (request_dt = '2011-03-05'::date)
You must change the type of request_dt column to DATE, or change the check constraints to work with timestamp. In the documentation, you can see an example with a date type column.
精彩评论