Postgresql Update命令太慢了

时间:2013-06-14 07:43:25

标签: postgresql postgresql-8.4 postgresql-performance

我有以下postgresql表:

CREATE TABLE "initialTable" (
    "paramIDFKey" integer,
    "featAIDFKey" integer,
    "featBIDFKey" integer,
    "featAPresent" boolean,
    "featBPresent" boolean,
    "dataID" text
);

我通过以下命令更新此表:

UPDATE "initalTable" 
SET "dataID" = "dataID" || '#' || 'NEWDATA' 
where 
    "paramIDFKey" = parameterID 
    and "featAIDFKey" = featAIDFKey 
    and "featBIDFKey" = featBIDFKey 
    and "featAPresent" = featAPresent 
    and "featBPresent" = featBPresent

如您所见,我正在更新每一行的dataID。此更新用作附加。它将新数据附加到以前的数据。

这太慢了。特别是当“dataID”列变大时。

以下是“解释”结果:

"Bitmap Heap Scan on "initialTable"  (cost=4.27..8.29 rows=1 width=974)"
"  Recheck Cond: (("paramIDFKey" = 53) AND ("featAIDFKey" = 0) AND ("featBIDFKey" = 95))"
"  Filter: ("featAPresent" AND (NOT "featBPresent"))"
"  ->  Bitmap Index Scan on "InexactIndex"  (cost=0.00..4.27 rows=1 width=0)"
"        Index Cond: (("paramIDFKey" = 53) AND ("featAIDFKey" = 0) AND ("featBIDFKey" = 95) AND ("featAPresent" = true) AND ("featBPresent" = false))"

解释分析:

"Bitmap Heap Scan on "Inexact2Comb"  (cost=4.27..8.29 rows=1 width=974) (actual time=0.621..0.675 rows=1 loops=1)"
"  Recheck Cond: (("paramIDFKey" = 53) AND ("featAIDFKey" = 0) AND ("featBIDFKey" = 95))"
"  Filter: ("featAPresent" AND (NOT "featBPresent"))"
"  ->  Bitmap Index Scan on "InexactIndex"  (cost=0.00..4.27 rows=1 width=0) (actual time=0.026..0.026 rows=1 loops=1)"
"        Index Cond: (("paramIDFKey" = 53) AND ("featAIDFKey" = 0) AND ("featBIDFKey" = 95) AND ("featAPresent" = true) AND ("featBPresent" = false))"
"Total runtime: 13.780 ms"

和版本:

"PostgreSQL 8.4.14, compiled by Visual C++ build 1400, 32-bit"

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

首先,我完全不相信这是一个真正的问题。如果15ms对于单个查询来说太长,您需要启动并询问您是否过早优化以及它是否真的是瓶颈。如果是,您可能想重新考虑如何使用数据库。请记住,查询的执行速度比解释分析的更快(我已经看到一些查询在EXPLAIN ANALYZE下运行速度慢了4倍)。首先,分析您的应用程序并寻找真正的瓶颈。

话虽如此,如果您确实发现这是一个瓶颈,您可以仔细查看索引内容。索引太多会降低写入操作的速度,并且会影响更新查询。这可能意味着在更新中添加包含所有列的新索引,并根据需要删除其他索引。但是,我真的不瘦,你会从这个查询中获得更多。