代码之家  ›  专栏  ›  技术社区  ›  Onichan

使用ActiveImport慢速构建树的祖先

  •  0
  • Onichan  · 技术社区  · 7 年前

    active import 导入注释和 Ancestry id 尚未创建?

    目前,我正在导入所有的注释,然后在每个注释之间循环以添加父注释,即使使用索引也非常慢:

    comments_to_import = []
    import_data.each do |comment|
        comments_to_import << Comment.new(is_parent: comment.parent_uuid.nil?, uuid: comment.uuid, parent_uuid: comment.parent_uuid, body: comment.body)
    end
    Comment.import comments_to_import
    
    # build comment tree
    Comment.where(is_parent: false, ancestry: nil).all.each do |c|
        parent = Comment.find_by(uuid: c.parent_uuid)
        c.update(parent: parent)
    end
    

    我在用三元索引加速 like

    class AddTrigramToComments < ActiveRecord::Migration
      def up
        execute "CREATE INDEX index_comments_on_ancestry_trigram ON comments USING gin (ancestry gin_trgm_ops)"
      end
    
      def down
        execute "DROP INDEX index_comments_on_ancestry_trigram"
      end
    end
    
    0 回复  |  直到 7 年前