【问题标题】:PostgreSQL slow sql update command on Amazon EC2Amazon EC2 上的 PostgreSQL 慢速 sql 更新命令
【发布时间】:2013-11-22 09:58:55
【问题描述】:

我在 Amazon EC2 介质实例(带有 EBS 存储)上运行 PostgreSQL 9.1 服务器。我正在尝试在大约 300 万行上运行一个简单的 UPDATE 语句。似乎需要永远。

这是更新声明:

UPDATE "ab_device" SET "last_seen" = "ab_device"."registered_at";

桌子:

                                   Table "public.ab_device"
    Column     |           Type           |                       Modifiers                        
---------------+--------------------------+--------------------------------------------------------
 id            | integer                  | not null default nextval('ab_device_id_seq'::regclass)
 uuid          | character varying(255)   | not null
 registered_at | timestamp with time zone | not null
 app_id        | integer                  | not null
 last_seen     | timestamp with time zone | not null
Indexes:
    "ab_tempdevice_pkey" PRIMARY KEY, btree (id)
    "ab_device_app_id_56238bcbd52372f_uniq" UNIQUE CONSTRAINT, btree (app_id, uuid)
    "ab_device_uuid" btree (uuid)
    "ab_tempdevice_app_id" btree (app_id)
Foreign-key constraints:
    "app_id_refs_id_9cb306ef" FOREIGN KEY (app_id) REFERENCES ab_app(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
    TABLE "ab_trial" CONSTRAINT "device_id_refs_id_050eed1f" FOREIGN KEY (device_id) REFERENCES ab_device(id) DEFERRABLE INITIALLY DEFERRED

请求的“解释”:

                                QUERY PLAN                                
--------------------------------------------------------------------------
 Update on ab_device  (cost=0.00..78210.65 rows=2761865 width=59)
   ->  Seq Scan on ab_device  (cost=0.00..78210.65 rows=2761865 width=59)
(2 rows)

运行请求时 CPU iowait 很高。请求花费大量时间是否正常?是不是因为机器的I/O不好?

感谢您的洞察力。

【问题讨论】:

  • Amazon 上的 IO 性能通常很糟糕 - 主要用于较长的任务。

标签: sql postgresql amazon-web-services amazon-ec2


【解决方案1】:

我认为这可能是因为 Postgresql 的 MVCC 实现。它使这种单一更新变得昂贵。在这些情况下,需要将每一行复制为一个新版本,而旧版本将被标记为已删除。

如果可以,请在更新前更改一些参数并执行 VACUUM FULL 以强制表重写。更多信息请参考:Slow simple update query on PostgreSQL database with 3 million rows

希望对你有帮助。

【讨论】:

  • 我不确定你提到的其他帖子。我不完全清楚该怎么做。它谈论 HOT,但没有展示如何使用它。
  • 你应该试试这个:stackoverflow.com/a/3100232/362298。 HOT 更新由 postgres 根据索引和更新类型自动完成。您可以通过发出命令检查它是否热更新:pg_stat_user_tables.n_tup_hot_upd。另外,如果您有兴趣,请参阅源文档了解详细信息:git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/…
猜你喜欢
  • 1970-01-01
  • 2017-05-09
  • 2016-03-02
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
相关资源
最近更新 更多