Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable secondary indexes during table copying #108

Open
avit opened this issue Apr 30, 2015 · 0 comments
Open

Disable secondary indexes during table copying #108

avit opened this issue Apr 30, 2015 · 0 comments

Comments

@avit
Copy link

avit commented Apr 30, 2015

I don't think it makes sense to always check unique & foreign key constraints within the background copy operation, since all the inserts are done against the primary key. Copied data would already be consistent in the original table.

Something like this could speed things up, I'm seeing an improvement in my use case.

module Lhm
  class Chunker
    module DisableKeyChecks

      def without_key_checks
        connection.execute(
          "SET @old_unique_checks = @@unique_checks, " \
              "@old_foreign_key_checks = @@foreign_key_checks, " \
              "unique_checks = 0, " \
              "foreign_key_checks = 0;"
        )
        yield
      ensure
        connection.execute(
          "SET unique_checks = @old_unique_checks, " \
              "foreign_key_checks = @old_foreign_key_checks;"
        )
      end

      def execute_without_key_checks
        without_key_checks do
          execute_with_key_checks
        end
      end

      def self.included(m)
        m.class_eval do
          alias_method :execute_with_key_checks, :execute
          alias_method :execute, :execute_without_key_checks
        end
      end
    end

    include DisableKeyChecks

  end
end

This could be enabled with an option, or when it can be proven safe (when the migration doesn't add any new constraints). Thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant