![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Optimizer doesn't behave well when the tables are partitioned. Here is the problem: news=# explain select max("document#") from moreover_documents_y2010m08; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------ Result (cost=0.15..0.16 rows=1 width=0) InitPlan 1 (returns $0) -> Limit (cost=0.00..0.15 rows=1 width=8) -> Index Scan Backward using pk_moreover_documents_y2010m08 on moreover_documents_y2010m08 (cost=0.00..2169871.61 rows=14615132 width=8) Filter: ("document#" IS NOT NULL) (5 rows) Time: 31.191 ms news=# explain select max("document#") from moreover_documents; QUERY PLAN ----------------------------------------------------------------------------------------------------------------------- Aggregate (cost=4227019.40..4227019.41 rows=1 width=8) -> Append (cost=0.00..4145103.32 rows=32766432 width=8) -> Seq Scan on moreover_documents (cost=0.00..10.20 rows=20 width=8) -> Seq Scan on moreover_documents_y2010m06 moreover_documents (cost=0.00..236523.53 rows=1856853 width=8) -> Seq Scan on moreover_documents_y2010m07 moreover_documents (cost=0.00..2073778.27 rows=16294327 width=8) -> Seq Scan on moreover_documents_y2010m08 moreover_documents (cost=0.00..1834740.32 rows=14615132 width=8) -> Seq Scan on moreover_documents_y2010m09 moreover_documents (cost=0.00..10.20 rows=20 width=8) -> Seq Scan on moreover_documents_y2010m10 moreover_documents (cost=0.00..10.20 rows=20 width=8) -> Seq Scan on moreover_documents_y2010m11 moreover_documents (cost=0.00..10.20 rows=20 width=8) -> Seq Scan on moreover_documents_y2010m12 moreover_documents (cost=0.00..10.20 rows=20 width=8) -> Seq Scan on moreover_documents_y2011m01 moreover_documents (cost=0.00..10.20 rows=20 width=8) (11 rows) Time: 31.961 ms news=# I have several partitions for the table, 3 of them are loaded. When I search for the maximum value of the primary key column on a single partition, the access path is as it should be, using the PK index. Each of the partitions has the same PK. When I try selecting the maximum on the entire table, the indexes on the partitions are not used, Postgres does the full scan instead. The difference in timing is drastic: news=# select max("document#") from moreover_documents_y2010m08; max ------------ 1175107508 (1 row) Time: 56.778 ms news=# select max("document#") from moreover_documents; max ------------ 1175107508 (1 row) Time: 200490.228 ms news=# |
#3
| |||
| |||
|
|
Hi, how did you define constraints in child tables? |
#4
| |||
| |||
|
|
On Wed, 01 Sep 2010 09:30:49 +0200, Anselmo Canfora wrote: Hi, how did you define constraints in child tables? Here it is, directly from PgAdmin: ALTER TABLE moreover.moreover_documents_y2010m09 ADD CONSTRAINT moreover_documents_y2010m09_created_at_check CHECK (created_at>= '2010-09-01 00:00:00'::timestamp without time zone AND created_at< '2010-10-01 00:00:00'::timestamp without time zone); ALTER TABLE moreover.moreover_documents_y2010m09 ADD CONSTRAINT pk_moreover_documents_y2010m09 PRIMARY KEY(document_id); There are monthly partitions, all identically defined, ranging from June 2010 to January 2011. There is also the primary key constraint on document_id on the empty master table. |
#5
| |||
| |||
|
|
You have to insert the column on which is defined the check in the "where" (or "on" with joins) condition of the select in order to enable the planner to locate the correct partition on which shrink the search, for example, assuming that your document_id is always ascending: select max(document#) from moreover_documents where date_trunc('month', created_at)>=date_trunc('month', current_timestamp); |
#6
| |||
| |||
|
|
On Thu, 02 Sep 2010 12:10:01 +0200, Anselmo Canfora wrote: You have to insert the column on which is defined the check in the "where" (or "on" with joins) condition of the select in order to enable the planner to locate the correct partition on which shrink the search, for example, assuming that your document_id is always ascending: select max(document#) from moreover_documents where date_trunc('month', created_at)>=date_trunc('month', current_timestamp); Anselmo, what I am complaining about is the fact that optimizer doesn't use available indexes in partitioned tables the same way it uses them in non-partitioned tables. |
#7
| |||
| |||
|
|
The PG optimizer it is not so smart in this case |
#8
| |||
| |||
|
|
On Thu, 02 Sep 2010 15:18:03 +0200, Anselmo Canfora wrote: The PG optimizer it is not so smart in this case Exactly! Voila! That is what I am complaining about. It should be smarter. |
#9
| |||
| |||
|
|
It suffices to say postgres could easily be smarter and would better compete with other products like Oracle if it were smarter. Equally, because postgresql is an open source database, you could easily make it smarter. |
#10
| |||
| |||
|
|
On Thu, 02 Sep 2010 10:38:07 -0300, Bob Badour wrote: It suffices to say postgres could easily be smarter and would better compete with other products like Oracle if it were smarter. Equally, because postgresql is an open source database, you could easily make it smarter. Yes, that is true. I downloaded the Postgresql source and am looking into it, but it's very hard to follow, especially for someone who is not a C programmer, like me. I would really be grateful for any commentary/ instructions about the code structure.... |
![]() |
| Thread Tools | |
| Display Modes | |
| |