2016-01-12 146 views
1

我有一個forEach循環遍歷多個對象,然後在數據庫中爲每個項目創建一條記錄。雖然這確實有效,但速度很慢(有些電影有50多個演職員,這是對數據庫的很多發佈請求)。有沒有更好的方法將多個對象保存到數據庫中?如何一次將多條記錄保存到數據庫中

這是我演員控制器的創造功能,

def create 
    Actor.find_or_create_by(movie_id: params[:movie_id], name: params[:name], character: params[:character]) 
    redirect_to :root 
end 

*更新*

登錄從我的鐵軌控制檯,

Started POST "/actors.json" for 127.0.0.1 at 2016-01-12 16:15:35 +0100 
Processing by ActorsController#create as JSON 
    Parameters: {"_json"=>[{"name"=>"John Travolta", "character"=>"Vincent Vega", "movie_id"=>680}, {"name"=>"Samuel L. Jackson", "character"=>"Jules Winfield", "movie_id"=>680}, {"name"=>"Uma Thurman", "character"=>"Mia Wallace", "movie_id"=>680}, {"name"=>"Bruce Willis", "character"=>"Butch Coolidge", "movie_id"=>680}, {"name"=>"Ving Rhames", "character"=>"Marsellus Wallace", "movie_id"=>680}, {"name"=>"Harvey Keitel", "character"=>"Wolf", "movie_id"=>680}, {"name"=>"Eric Stoltz", "character"=>"Lance", "movie_id"=>680}, {"name"=>"Tim Roth", "character"=>"Pumpkin", "movie_id"=>680}, {"name"=>"Amanda Plummer", "character"=>"Honey Bunny", "movie_id"=>680}, {"name"=>"Maria de Medeiros", "character"=>"Fabienne", "movie_id"=>680}, {"name"=>"Quentin Tarantino", "character"=>"Jimmie Dimmick", "movie_id"=>680}, {"name"=>"Christopher Walken", "character"=>"Captain Koons", "movie_id"=>680}, {"name"=>"Rosanna Arquette", "character"=>"Jody", "movie_id"=>680}, {"name"=>"Peter Greene", "character"=>"Zed", "movie_id"=>680}, {"name"=>"Duane Whitaker", "character"=>"Maynard", "movie_id"=>680}, {"name"=>"Angela Jones", "character"=>"Esmarelda Villalobos", "movie_id"=>680}, {"name"=>"Phil LaMarr", "character"=>"Marvin", "movie_id"=>680}, {"name"=>"Steve Buscemi", "character"=>"Buddy Holly", "movie_id"=>680}, {"name"=>"Bronagh Gallagher", "character"=>"Trudi", "movie_id"=>680}, {"name"=>"Laura Lovelace", "character"=>"Waitress", "movie_id"=>680}, {"name"=>"Frank Whaley", "character"=>"Brett", "movie_id"=>680}, {"name"=>"Burr Steers", "character"=>"Roger", "movie_id"=>680}, {"name"=>"Paul Calderon", "character"=>"Paul", "movie_id"=>680}, {"name"=>"Jerome Patrick Hoban", "character"=>"Ed Sullivan", "movie_id"=>680}, {"name"=>"Michael Gilden", "character"=>"Page for Phillip Morris", "movie_id"=>680}, {"name"=>"Gary Shorelle", "character"=>"Ricky Nelson", "movie_id"=>680}, {"name"=>"Susan Griffiths", "character"=>"Marilyn Monroe", "movie_id"=>680}, {"name"=>"Eric Clark", "character"=>"James Dean", "movie_id"=>680}, {"name"=>"Joseph Pilato", "character"=>"Dean Martin", "movie_id"=>680}, {"name"=>"Brad Blumenthal", "character"=>"Jerry Lewis (as Brad Parker)", "movie_id"=>680}, {"name"=>"Lorelei Leslie", "character"=>"Mamie van Doren", "movie_id"=>680}, {"name"=>"Emil Sitka", "character"=>"Hold Hands You Lovebirds (archive footage)", "movie_id"=>680}, {"name"=>"Brenda Hillhouse", "character"=>"Mrs. Coolidge - Butch's Mother", "movie_id"=>680}, {"name"=>"Chandler Lindauer", "character"=>"Young Butch", "movie_id"=>680}, {"name"=>"Sy Sher", "character"=>"Klondike", "movie_id"=>680}, {"name"=>"Robert Ruth", "character"=>"Sportscaster #1 - Coffee Shop", "movie_id"=>680}, {"name"=>"Rich Turner", "character"=>"Sportscaster #2", "movie_id"=>680}, {"name"=>"Don Blakely", "character"=>"Wilson's Trainer", "movie_id"=>680}, {"name"=>"Carl Allen", "character"=>"Dead Floyd Wilson", "movie_id"=>680}, {"name"=>"Karen Maruyama", "character"=>"Gawker #1", "movie_id"=>680}, {"name"=>"Kathy Griffin", "character"=>"Hit-and-run Witness", "movie_id"=>680}, {"name"=>"Venessia Valentino", "character"=>"Pedestrian/Bonnie Dimmick", "movie_id"=>680}, {"name"=>"Linda Kaye", "character"=>"Shot Woman", "movie_id"=>680}, {"name"=>"Stephen Hibbert", "character"=>"The Gimp", "movie_id"=>680}, {"name"=>"Alexis Arquette", "character"=>"Man No. 4", "movie_id"=>680}, {"name"=>"Julia Sweeney", "character"=>"Raquel", "movie_id"=>680}, {"name"=>"Lawrence Bender", "character"=>"Long Hair Yuppy Scum", "movie_id"=>680}, {"name"=>"Cie Allman", "character"=>"Winston Wolfe's Girlfriend At Party (uncredited)", "movie_id"=>680}, {"name"=>"Rene Beard", "character"=>"Bar Tender (uncredited)", "movie_id"=>680}, {"name"=>"Lori Pizzo", "character"=>"Lucky Lady (uncredited)", "movie_id"=>680}, {"name"=>"Glendon Rich", "character"=>"Drug Dealer (uncredited)", "movie_id"=>680}, {"name"=>"Devan Richardson", "character"=>"Hopalong Cassidy (uncredited)", "movie_id"=>680}, {"name"=>"Ani Sava", "character"=>"Woman in Bathroom (uncredited)", "movie_id"=>680}], "actor"=>{}} 
    User Load (2.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
    (0.1ms) begin transaction 
    SQL (0.2ms) INSERT INTO "actors" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2016-01-12 15:15:35.503383"], ["updated_at", "2016-01-12 15:15:35.503383"]] 
    (11.9ms) commit transaction 
Redirected to http://localhost:3000/ 
Completed 302 Found in 31ms (ActiveRecord: 15.0ms) 
+1

你爲什麼不做一個會建立數據的循環,每個20或30個演員發送1個大插入查詢? – Gal

+0

@Gal會導致1個記錄被創建?那麼每個演員的名字和角色價值將被存儲在一個記錄中?現在的結果不是多個記錄? – alucardu

+0

SQL Server示例'INSERT INTO表(Column1,Column2)VALUES (Value1,Value2),(Value1,Value2)'這會插入多條記錄 – Malkus

回答

3

您已經標記使用Ruby的問題在Rails上,所以我假設你正在使用ActiveRecord。所以,建立一個API批量插入如下。

您的角度代碼看起來應該像下面

var actors = []; 
angular.forEach($scope.movieCredits.credits.cast, function(item){ 
    actors.push({ 
    name:  item.name, 
    character: item.character, 
    movie_id: movieRecordID[0].id, 
    }); 
}); 
createActor.create(actors); 

而在你的軌道控制器

def create 
    actors = Actor.create(params[:actors]) 
    .. 
    render json: actors 
end 

這是活動記錄如何處理插入多條記錄,因爲create方法接受一個數組過如下

Model.create([{name: "John Doe", character: "The Hobbit", movie_id: 1}, {name: "Christian Bale", character: "Batman", movie_id: 2}]) 

這將執行O NE記錄的N個SQL查詢,而不是N個記錄的N個SQL查詢。

+0

所以你通過.push填充空的演員陣列,這會將每個項目推入演員陣列中?然後使用該數組發佈使用'createActor'服務?我已經嘗試過了,在我的Rails日誌中,我看到了所有演員等,但我的演員表仍然是空的。我認爲這是因爲我的導軌控制器中的創建功能。我已經將它添加到了我的問題中,也許你可以看看我的hunge是否正確?無論如何,謝謝你的洞察力。 – alucardu

+0

'find_or_create_by'查找或創建單個記錄並且不能接受數組。您必須使用'create'方法並通過驗證處理重複項。 –

+0

好的,但是如何將參數添加到'create'方法?我現在已經有了'actors = Actor.create(actor_params)'和'private def actor_params params.permit(:name,:character,:movie_id)end',這裏有新的記錄,但是它是空的。 – alucardu

相關問題