好的,我有一个名为HashtagMapper.com的rails应用程序,可以找到成千上万的推文并映射它们。我遇到的问题是地理编码1000多个查询之间最多需要10秒钟,然后.create()
调用大约需要10毫秒,1000个查询需要10秒。
如何一次在rails中创建1000多个DB对象?现在我打电话
SearchResult.create([ { :2D => array }, { :of => SearchResults } ])
对于1000多个对象。纯SQL会更快吗?怎么样?
答案 0 :(得分:0)
来自数组或哈希:
Book.bulk_insert(:title, :author) do |worker|
# specify a row as an array of values...
worker.add ["Eye of the World", "Robert Jordan"]
# or as a hash
worker.add title: "Lord of Light", author: "Roger Zelazny"
end
默认为数据库列
class Book < ActiveRecord::Base
end
book_attrs = ... # some array of hashes, for instance
Book.bulk_insert do |worker|
book_attrs.each do |attrs|
worker.add(attrs)
end
end