English 中文(简体)
在我的案件中插入数据库表的新记录
原标题:new records insertion into database table in my case

我正在开发一个Rails v2.3应用程序,该应用程序是a服务,用于搜索项目信息,项目信息储存在数据库中。

数据库现有<代码>项目表如下:

“enterography

For the sake of satisfying customer’s requirement, this table needs to insert new data in the mid-night everyday.

www.un.org/Depts/DGACM/index_spanish.htm 创建这些新记录的原因是,铁路申请除了用全称搜索外,还能够用一个字搜索项目。

www.un.org/Depts/DGACM/index_spanish.htm 例如,如果用“portal”一词搜索,则两者兼有。 铁路应用中应当找到汽车租赁门户Position道门户记录。 <> 代码>项目名称

因此,我计划通过<>分配中的价值>生成这些新记录。 (上述<条码>项目表>)改为“,然后将每一字作为 >,新纪录<条码>项目_,同时保持其他栏目不变。

www.un.org/Depts/DGACM/index_spanish.htm 例如,上表上的第1个记录有 项目_ 姓名Car le Portal”,I gonna的意思是将这一座标分为3个字和,将以下3个新记录插入表格:

“entergraph

To achieve this. I tried to make a rake task which gets all records from the original projects table, and for each record, the rake task splits the string value of project_name column into words, then construct the new records with words and insert into the table. My rake task looks like the code below:

all_records = ActiveRecord::Base.execute("select * from projects;") 
all_records.each do |record|
     user_id = record[0]
     project_name=record[1]
     department = record[2]
     other = record[3]

     words=project_name.split()

     words.each do |word|
         sql = "insert into project values (#{user_id},#{word},#{department},#{other});"
         ActiveRecord::Base.execute(sql)
     end
end

The rake task works well, it creates the expected new records and inserted into the projects table, BUT the problem is it takes 36 hours to complete!

之所以可以理解,是因为原表非常庞大,如果将插图与文字分开,并创造新记录,那么它就会产生3倍的较大表格(每个字面上>_name/code>有3字)。

www.un.org/Depts/DGACM/index_french.htm

  • Could some Rails experts suggest me some more efficient way to achieve the new record insertion thing I described above?

  • 或者在我的案件中采用任何新的方法进行单独一字搜索? (没有使用我设计的将每一字储存在数据库中的方式。)

问题回答

为了更快地进口,您希望使用activerecord-import,它将加快执行力度。

columns = [:title, :project_name, :department, :other]
values = all_records.inject([]) do |values_arr, record|

    user_id, project_name, department, other = record

    project_name.split.each do |name|
        values_arr << [user_id, name, department, other]
    end
    values_arr
end

class TempModel < ActiveRecord::Base; set_table_name "projects"; end
TempModel.import columns, values, :validate=>false




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...