简体   繁体   English

PostgreSQL:垂直分区不会提高性能

[英]postgresql: vertical partitioning brings no performance improvement

I struggle with optimizing the performance of my table with vertical partitioning. 我很难通过垂直分区来优化表的性能。 The following Select Statement should be more optimized from postgre imho: 下面的Select语句应该从postgre imho中得到更好的优化:

SELECT 
  "ProductView".name, 
  "ProductView".price, 
  "ProductView".pid
FROM 
  "DefaultSchema"."ProductView";

My Schema looks like this: 我的架构如下所示:

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

The SQL: 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;

So you might recognise I do not really need ProductB for the select query. 因此,您可能会认识到,对于选择查询,我确实不需要ProductB。 Nevertheless it is joined during the process of execution, as you can see here. 不过,正如您在此处看到的那样,它是在执行过程中加入的。

"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"

My Question is how can I force postgre to scan only ProductA? 我的问题是如何强制Postgre只扫描ProductA? Do I need an addional constraint, edit the config file or is it not possible to gain a performance advantage through vertical partitioning in postgre? 我是否需要附加约束,编辑配置文件,或者是否无法通过Postgre中的垂直分区获得性能优势? Thanks a lot in advance. 非常感谢。 :) :)

PostgreSQL's query planner does not yet do join removal on inner joins. PostgreSQL的查询计划程序尚未对内部联接删除联接。

You can either query "ProductA" alone, or rewrite the view to use a left outer join. 您可以单独查询“ ProductA”,也可以重写视图以使用左外部联接。 PostgreSQL 9.0+ does do join removal on left outer joins. 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)

Rewriting to use a left outer join isn't safe in every application, but I think it's safe for your particular problem. 使用左外部联接进行重写并非在每个应用程序中都是安全的,但我认为这对于您的特定问题是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM