Amazon EC2上的PostgreSQL slow sql update命令

时间:2013-11-22 09:58:56

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

我在Amazon EC2媒体实例(带有EBS存储)上运行PostgreSQL 9.1服务器。我试图在大约3百万行上运行一个简单的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很差?

感谢您的见解。

1 个答案:

答案 0 :(得分:0)

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

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

希望它有所帮助。