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

Restructure new findaproject page for #4129 #4142

Open
wants to merge 10 commits into
base: development
Choose a base branch
from

Conversation

benwbrum
Copy link
Owner

Closes #4129

Copy link
Collaborator

@saracarl saracarl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change "More..." to "All Collections"

Let's remove the customer description from this view. I think Name is enough.

Uploading image.png…

These are just great big blocks of text. Can we indent the collection listing?

@WillNigel23 WillNigel23 self-assigned this Aug 28, 2024
Gemfile Outdated
gem 'memory_profiler'
gem 'meta_request'
gem 'rack-mini-profiler', require: false
gem 'stackprof'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are dev tools so I moved it here. We can move it back in case it is being used in prod>

unless session[:guest_user_id].nil?
User.where(id: session[:guest_user_id]).first
end
User.find_by(id: session[:guest_user_id])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exact same query in SQL, just shorter way of writing it. Will give nil if not present, so same function.

.includes({ random_collections: :tags }, { random_document_sets: { collection: :tags } })

@org_owners = users.findaproject_orgs.order(:display_name).distinct
@individual_owners = users.findaproject_individuals.order(:display_name).distinct
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will be subqueries from users

.includes(:owner, { next_untranscribed_page: :work })
.where(owner_user_id: @org_owners.select(:id) + @individual_owners.select(:id)).sample(5)
colls = Collection.carousel
.includes(:owner, { next_untranscribed_page: :work })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N+1 issue here. Checking the html we access next_untranscribed_page, and then it's work. Both of these have to be preloaded.

Also it seems, as long as we preload correct relationships, turning it to array afterwards will not cause more query

collections_query = Collection.search(search_key).unrestricted.includes(:owner)
document_sets_query = DocumentSet.search(search_key).unrestricted.includes(:owner)

collections_query + document_sets_query
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, we access owner from search_results so preload that too.

users = User.owners
.joins(:collections)
.left_outer_joins(:document_sets)
.includes({ random_collections: :tags }, { random_document_sets: { collection: :tags } })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit tricky. So we cannot preload queries with dynamic like random.

So what I did here is remove the random_sample scope in random_collections, then we sample later via array to SQL calls.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, in hindsight, this makes 'random_collection' and 'random_document_sets' scope feel like incorrect labels now.

-[@org_owners, @individual_owners].each do |owners|
-owners.each do |owner|
-projects = (owner.random_collections+owner.random_document_sets)
-projects = (owner.random_collections.sample(5) + owner.random_document_sets.sample(5))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We sample here as we can't preload with dynamic random query

@@ -59,12 +62,12 @@
-if owner.about.present?
.description
=owner.about
-tag_map = projects.map{|p| p.tags.canonical}.flatten.group_by{|t| t}.map{|k,v| [k.ai_text, v.size]}.sort_by{|k,v| v}.reverse
-tag_map = projects.flat_map(&:tags).group_by(&:canonical).map { |tag, tag_list| [tag.ai_text, tag_list.size] }.sort_by(&:last).reverse
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not 100% sure if this functions exactly how you intended it to be (so please correct me if I'm wrong here). Though based on my testing my approach produced less SQL queries (so no n+1 it seems). Not 100% sure

@WillNigel23
Copy link
Collaborator

All in all, in my local dev I was able to reduce from 150+ queries to 49-ish

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

Successfully merging this pull request may close these issues.

New FindAProject page performance problems
3 participants