Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何在Rails3中真正快速地将对象数组保存到数据库中?_Database_Ruby On Rails 3_Activerecord - Fatal编程技术网

Database 如何在Rails3中真正快速地将对象数组保存到数据库中?

Database 如何在Rails3中真正快速地将对象数组保存到数据库中?,database,ruby-on-rails-3,activerecord,Database,Ruby On Rails 3,Activerecord,我从CSV文件中读取了一个非常大的对象数组,并希望将其保存到数据库中。 这就是我正在做的: # Read CSV ... each do |values| new_value = Model.new ... # fill properties @new_values.push new_value # put it into the array end # now save them to the database @new_values.each do |new_value|

我从CSV文件中读取了一个非常大的对象数组,并希望将其保存到数据库中。 这就是我正在做的:

# Read CSV
... each do |values|
  new_value = Model.new
  ... # fill properties
  @new_values.push new_value # put it into the array
end

# now save them to the database
@new_values.each do |new_value| 
    new_value.save :validate => false
    @added_counter += 1
end
但这确实很慢,因为它将为数组中的每个元素生成一个语句。如何快速正确地完成这项工作?

可能对您有用

它允许执行以下操作:

books = []
10.times do |i| 
  books << Book.new(:name => "book #{i}")
end
Book.import books
books=[]
10.我做了多少次
书籍“书籍{i}”)
结束
进口书
也许对你有用

它允许执行以下操作:

books = []
10.times do |i| 
  books << Book.new(:name => "book #{i}")
end
Book.import books
books=[]
10.我做了多少次
书籍“书籍{i}”)
结束
进口书

这是一个基于的评论的解决方案,mu太短了(谢谢!)。 我使用手工制作的SQL语句并执行它

  insert_statement = "INSERT INTO table_name (custom_property_from_parameter"
  # the properties from the model are fetched by a method, to keep it a little bit generic, loop trough them and give their names to the statement
  Model.parameter_names.each do |parameter|
    insert_statement += "," + parameter
  end
  insert_statement += ") VALUES "
  @new_values.each do |new_value|
    insert_statement += "(" + params[:custom_property]
    Model.parameter_names.each do |parameter|
      # Rails' nil has to be converted into NULL for SQL
      if nil == new_value.send(parameter)
        insert_statement += ",NULL"
      else
        insert_statement += "," + new_value.send(parameter).to_s
      end
    end
    insert_statement += "),"
    @added_counter += 1
  end
  # Remove the last , with ;
  insert_statement = insert_statement[0..-2]
  insert_statement += ";"
  # now execute the statement
  ActiveRecord::Base.connection.execute insert_statement

此解决方案大约需要三分之一的时间。但对我来说这似乎有点像黑客。

这是一个基于的评论的解决方案,mu太短了(谢谢!)。 我使用手工制作的SQL语句并执行它

  insert_statement = "INSERT INTO table_name (custom_property_from_parameter"
  # the properties from the model are fetched by a method, to keep it a little bit generic, loop trough them and give their names to the statement
  Model.parameter_names.each do |parameter|
    insert_statement += "," + parameter
  end
  insert_statement += ") VALUES "
  @new_values.each do |new_value|
    insert_statement += "(" + params[:custom_property]
    Model.parameter_names.each do |parameter|
      # Rails' nil has to be converted into NULL for SQL
      if nil == new_value.send(parameter)
        insert_statement += ",NULL"
      else
        insert_statement += "," + new_value.send(parameter).to_s
      end
    end
    insert_statement += "),"
    @added_counter += 1
  end
  # Remove the last , with ;
  insert_statement = insert_statement[0..-2]
  insert_statement += ";"
  # now execute the statement
  ActiveRecord::Base.connection.execute insert_statement

此解决方案大约需要三分之一的时间。但对我来说,这似乎有点像黑客攻击。

我使用MySQL,创建模型,然后从CSV文件中获取大量权限(每个大约30个整数或浮点数)。您是否考虑过不使用AR,直接使用SQL,甚至让MySQL自己导入CSV?AR并不是一个速度恶魔。我需要在读取CSV文件时验证数据,因为它是由用户提供的。但构建自定义sql语句似乎是一种可行的方法。我会试试。你可以导入一个临时表,在数据库中进行验证,然后批量复制到真实的表中。有点难看,不太干燥,但如果速度是你的首要要求,那么你就去做你必须做的事情;OTOH,我和Ruby一样熟悉SQL。您还可以尝试将NoSQL解决方案作为临时数据库来存储验证前的数据,并转移到实际的生产使用数据库我使用MySQL创建模型,然后从CSV文件中获取大量数据(每个大约30个整数或浮点数).您是否考虑过不使用AR,直接使用SQL,甚至让MySQL自己导入CSV?AR并不是一个速度恶魔。我需要在读取CSV文件时验证数据,因为它是由用户提供的。但构建自定义sql语句似乎是一种可行的方法。我会试试。你可以导入一个临时表,在数据库中进行验证,然后批量复制到真实的表中。有点难看,不太干燥,但如果速度是你的首要要求,那么你就去做你必须做的事情;OTOH,我和Ruby一样熟悉SQL。您还可以尝试将NoSQL解决方案作为临时数据库来存储验证前的数据,并转移到实际的生产使用数据库。比用手做这个好多了。啊,太好了。这比手工操作要好得多。这是activerecord导入用来帮助避免编写的代码。它根据AR模型中的列定义智能地处理正在导入的数据的转换。它还将确保它为RDBMS(如MySQL)执行最少的导入语句,否则您可能会得到一个最大数据包允许超出错误。这是activerecord导入用于帮助避免编写的代码类型。它根据AR模型中的列定义智能地处理正在导入的数据的转换。它还将确保为RDBMS(如MySQL)执行最少的导入语句,否则您可能会得到一个最大允许数据包错误。