postgresql:垂直分区不会带来性能提升

时间:2014-11-05 15:38:01

标签: performance postgresql query-optimization vertical-partitioning

我很难通过垂直分区来优化表的性能。应该从postgre imho:

更加优化以下Select语句
SELECT 
  "ProductView".name, 
  "ProductView".price, 
  "ProductView".pid
FROM 
  "DefaultSchema"."ProductView";

我的架构如下所示:

tables:
ProductA(**pid**, name, price)
ProductB(**pid**, desc)
view:
Product(**pid**,name, price, desc)

SQL:

CREATE TABLE "DefaultSchema"."ProductA"
(
  pid integer NOT NULL,
  price integer,
  name text,
  CONSTRAINT pk_pa PRIMARY KEY (pid)
)

CREATE TABLE "DefaultSchema"."ProductB"
(
  pid integer NOT NULL,
  "desc" text,
  CONSTRAINT "PK_PB" PRIMARY KEY (pid),
  CONSTRAINT "FK_PID" FOREIGN KEY (pid)
      REFERENCES "DefaultSchema"."ProductA" (pid) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)

CREATE OR REPLACE VIEW "DefaultSchema"."ProductView" AS 
 SELECT p1.pid,
    p1.price,
    p1.name,
    p2."desc"
   FROM "DefaultSchema"."ProductA" p1
     JOIN "DefaultSchema"."ProductB" p2 ON p1.pid = p2.pid;

所以你可能会认识到我并不真的需要ProductB来进行选择查询。然而,正如你在这里看到的那样,它在执行过程中加入了。

"Hash Join  (cost=36.10..74.61 rows=1160 width=40) (actual time=0.090..0.105 rows=7 loops=1)"
"  Hash Cond: (p2.pid = p1.pid)"
"  ->  Seq Scan on "ProductB" p2  (cost=0.00..22.30 rows=1230 width=4) (actual time=0.022..0.027 rows=7 loops=1)"
"  ->  Hash  (cost=21.60..21.60 rows=1160 width=40) (actual time=0.030..0.030 rows=7 loops=1)"
"        Buckets: 1024  Batches: 1  Memory Usage: 1kB"
"        ->  Seq Scan on "ProductA" p1  (cost=0.00..21.60 rows=1160 width=40) (actual time=0.010..0.017 rows=7 loops=1)"
"Total runtime: 0.299 ms"

我的问题是如何强制postgre仅扫描ProductA?我是否需要附加约束,编辑配置文件或者是否无法通过postgre中的垂直分区获得性能优势?非常感谢提前。 :)

1 个答案:

答案 0 :(得分:1)

PostgreSQL的查询计划程序尚未对内部联接进行连接删除。

您可以查询" ProductA"单独或重写视图以使用左外连接。 PostgreSQL 9.0 + 确实在左外连接上进行连接删除。

CREATE OR REPLACE VIEW "DefaultSchema"."ProductView" AS 
 SELECT p1.pid,
    p1.price,
    p1.name,
    p2."desc"
   FROM "DefaultSchema"."ProductA" p1
   LEFT JOIN "DefaultSchema"."ProductB" p2 ON p1.pid = p2.pid;

explain analyze
SELECT "ProductView".name, "ProductView".price, "ProductView".pid
FROM "ProductView";
QUERY PLAN
--
Seq Scan on "ProductA" p1  (cost=0.00..20.00 rows=1000 width=41) (actual time=0.008..0.225 rows=1000 loops=1)

重写使用左外连接在每个应用程序中都不安全,但我认为它对您的特定问题是安全的。