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

FslMelodic has some very inefficient code #120

Open
prioux opened this issue Dec 5, 2017 · 0 comments
Open

FslMelodic has some very inefficient code #120

prioux opened this issue Dec 5, 2017 · 0 comments
Labels

Comments

@prioux
Copy link
Member

prioux commented Dec 5, 2017

Seen in fsl_melodic/portal/portal.rb

It takes minutes to generate a simple list of template files tagged by "template" using this code:

  def get_template_files
    template_files = []
    current_user = User.find(self.user_id)
    user_tags = current_user.available_tags.select {|t| t.name =~ /TEMPLATE/i}
    user_tags.each do |tag|
      user_files ||= Userfile.find_all_accessible_by_user(current_user)
      template_files.concat user_files.select { |u| u.tag_ids.include? tag.id }
    end
    return template_files.map { |f| [f.name,f.id.to_s]}
  end

A user generated over 100,000 DB request because of that.

My solution which is instantaneous:

  def get_template_files
    current_user = User.find(self.user_id)
    tag_ids = current_user.available_tags.where(["name like ?","%template%"]).pluck(:id)
    Userfile.find_all_accessible_by_user(current_user)
    .joins(:tags).where('tags.id' => tag_ids)
    .raw_rows('userfiles.name', 'userfiles.id')
  end

Careful, the IDs returned are no longer strings. Adjust if needed.

@prioux prioux added the bug label Dec 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant