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

Performance issues when adding or updating tags #838

Open
bduran82 opened this issue Jun 21, 2017 · 0 comments
Open

Performance issues when adding or updating tags #838

bduran82 opened this issue Jun 21, 2017 · 0 comments

Comments

@bduran82
Copy link
Contributor

Running on Rails 4.2.8 and gem version 5.0.0 we experience a poor performance when updating the tags of a model with big number of tags.

In our project we need to handle models with sometimes big number of tags (up to around 300). We have experienced performance issues in our database when updating tags in those models that even led to request timeouts. The source seems to be the huge amount of database queries like the following:

SELECT `tags`.* FROM `tags` WHERE (name = BINARY 'editor' OR name = BINARY 'funding' OR name = BINARY 'transfers' OR name = BINARY 'role' OR name = BINARY 'editorial' OR ...

It looks like in ActsAsTaggableOn::Tag find_or_create_all_with_like_by_name all the tags attached to the model are repeatedly reloaded for every tag to avoid collisions in the database. Shouldn't the tags be loaded once in the beginning of the method and then only refreshed again in case there is a collision?

  def self.find_or_create_all_with_like_by_name(*list)
      list = Array(list).flatten

      return [] if list.empty?

      list.map do |tag_name|
        begin
          tries ||= 3

          existing_tags = named_any(list)
          comparable_tag_name = comparable_name(tag_name)
          existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
          existing_tag || create(name: tag_name)
        rescue ActiveRecord::RecordNotUnique
          if (tries -= 1).positive?
            ActiveRecord::Base.connection.execute 'ROLLBACK'
            retry
          end

          raise DuplicateTagError.new("'#{tag_name}' has already been taken")
        end
      end
    end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant