我有一个按月分区的表,用于保存 apache 日志信息。当我为一个简单查询运行 EXPLAIN 时,该查询在与分区 CHECK 相同的字段上有一个 WHERE 子句,无论 constraint_exclusion 是开还是关,我都会得到相同的计划。以下是 2010 年 2 月和 3 月的主表信息和两个子表信息示例。总体而言,从 2010 年 2 月到 2011 年 6 月,每个月都有一个子表。平均每个子表有大约 100M 条记录。下面还有一个简单查询的 EXPLAIN 输出,一次打开 constraint_exclusion,一次关闭它。不幸的是,没有使用 CHECK 约束来减小计划大小。这是因为 request_dt 数据类型是 TIMESTAMP WITH TIME ZONE 但 CHECK 约束是 DATES 吗?还有其他想法吗?我还没有在 request_dt 上创建索引,但是根据没有必要的文档。我将添加它们,但我认为它们不会影响 CHECK 约束的使用。
我正在使用 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)
最佳答案
您必须将 request_dt 列的类型更改为 DATE,或更改检查约束 以使用时间戳。在documentation ,您可以看到一个带有日期类型列的示例。
关于postgresql - 为什么计划者没有利用我对子表的 CHECK CONSTRAINTS 来减小计划的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6537968/