【问题标题】:Pause MySQL indexing while importing from CSV or EXCEL file从 CSV 或 EXCEL 文件导入时暂停 MySQL 索引
【发布时间】:2013-09-27 19:40:08
【问题描述】:

如何在将数据导入数据库时​​停止 mysql 索引。我有一个 Rails 应用程序,用户可以在其中从文件中导入数据。我添加了一些索引以获得更快 搜索电话号码和电子邮件地址的结果。

我的联系人模型与其他模型有更多的关系,为了更简单的例子,我将其删除:

  create_table "contacts", :force => true do |t|
    t.integer  "user_id"
    t.integer  "status"
    t.integer  "gender",                :default => 0,     :null => false
    t.string   "salutation",                               :null => false
    t.string   "title"
    t.string   "first_name",                               :null => false
    t.string   "last_name",                                :null => false
    t.binary   "phone"
    t.binary   "email"
    t.datetime "created_at",                               :null => false
    t.datetime "updated_at",                               :null => false
    :
    :
  end

  add_index "contacts", ["phone"], :name => "index_contacts_on_phone"
  add_index "contacts", ["email"], :name => "index_contacts_on_email"

导入需要很长时间。我可以在导入数千个联系人时阻止 MySQL 建立索引吗?导入后我将启用索引。有没有首选的方法来做到这一点?

谢谢,丹尼尔

【问题讨论】:

    标签: mysql ruby-on-rails indexing


    【解决方案1】:

    实际上,您可以同时执行这两项任务。 你听说过sidekiq吗?它在后台运行您想要的进程。所以后台的mysql索引。您必须为索引创建一个进程工作者,因此它不会中断您的文件导入。

    https://github.com/mperham/sidekiq

    只需阅读他们的基础知识,您就会明白其中的要点。

    【讨论】:

    • 我使用 sidekiq 进行导入。用户应该将那里的文件加载到服务器并开始导入过程。 Sidekiq 导入所有数据。但是这个过程很慢,并且数据库(服务器)上的负载非常高。我的想法是在导入开始时停止 sidekiq 工作进程中的索引,并在导入完成时启动并重新索引它。
    • 在你正在导入数据的worker中,创建一些临时变量/对象来保存数据,每次创建一定数量的时候才将它们保存到数据库中。它将减少数据库负载。
    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2016-01-30
    • 2017-03-04
    • 2021-12-05
    • 1970-01-01
    相关资源
    最近更新 更多