読者です 読者をやめる 読者になる 読者になる

New-Village

月間ブログ。だいたい1カ月に1回は更新しているようです。

Updateとupdate_attributeの比較

UpdateとUpdate_attributeはともに、単一のカラムを更新する際に利用するメソッドである。その違いは、モデルによるヴァリエーションが適用されるのがUpdateで、適用されないものがUpdate_attributeである。

プロセス及び処理時間にどんな違いが出るのか、今後の為にログをメモしておいた。

 

■ @main.update(:read_flg => "t")

Started GET "/main.2" for 106.188.15.212 at 2014-09-07 07:29:08 +0000
Processing by MainsController#show as
Main Load (0.2ms) SELECT "mains".* FROM "mains" WHERE "mains"."feed_id" = 2 ORDER BY created_at DESC LIMIT 1
(0.1ms) begin transaction
Main Exists (0.2ms) SELECT 1 AS one FROM "mains" WHERE ("mains"."feed_id" = 2 AND "mains"."id" != 2 AND "mains"."user_id" = 1) LIMIT 1
SQL (0.4ms) UPDATE "mains" SET "read_flg" = ?, "updated_at" = ? WHERE "mains"."id" = 2 [["read_flg", "t"], ["updated_at", "2014-09-07 07:29:08.994999"]]
(8437.0ms) commit transaction
Feed Load (0.2ms) SELECT "feeds".* FROM "feeds" WHERE "feeds"."id" = ? LIMIT 1 "id", 2
Redirected to http://slashdot.jp/story/14/09/06/212225/
Completed 302 Found in 8550ms (ActiveRecord: 8439.3ms)

 

■ @main.update_attribute(:read_flg,"t")

Started GET "/main.8" for 106.188.15.212 at 2014-09-07 07:30:02 +0000
Processing by MainsController#show as
Main Load (0.4ms) SELECT "mains".* FROM "mains" WHERE "mains"."feed_id" = 8 ORDER BY created_at DESC LIMIT 1
(0.1ms) begin transaction
SQL (0.5ms) UPDATE "mains" SET "read_flg" = ?, "updated_at" = ? WHERE "mains"."id" = 8 [["read_flg", "t"], ["updated_at", "2014-09-07 07:30:03.039579"]]
(6466.5ms) commit transaction
Feed Load (0.3ms) SELECT "feeds".* FROM "feeds" WHERE "feeds"."id" = ? LIMIT 1 "id", 8
Redirected to http://developers.slashdot.jp/story/14/09/05/2043239/
Completed 302 Found in 6635ms (ActiveRecord: 6467.7ms)

 

今回の件とは関係ないが、nitrous上のsqliteのコミットが非常に遅くて困ってます…