DEV: Change categories#index loading strategy (#25232)

The old strategy used to load 25 categories at a time, including the
subcategories. The new strategy loads 20 parent categories and at most
5 subcategories for each parent category, for a maximum of 120
categories in total.
This commit is contained in:
Bianca Nenciu
2024-01-17 17:18:01 +02:00
committed by GitHub
parent ec1905cf6f
commit 4cfc0e231a
2 changed files with 41 additions and 2 deletions

View File

@@ -1,7 +1,8 @@
# frozen_string_literal: true
class CategoryList
CATEGORIES_PER_PAGE = 25
CATEGORIES_PER_PAGE = 20
SUBCATEGORIES_PER_CATEGORY = 5
include ActiveModel::Serialization
@@ -138,7 +139,11 @@ class CategoryList
if SiteSetting.lazy_load_categories
page = [1, @options[:page].to_i].max
query = query.limit(CATEGORIES_PER_PAGE).offset((page - 1) * CATEGORIES_PER_PAGE)
query =
query
.where(parent_category_id: nil)
.limit(CATEGORIES_PER_PAGE)
.offset((page - 1) * CATEGORIES_PER_PAGE)
end
query =
@@ -146,6 +151,20 @@ class CategoryList
@categories = query.to_a
if SiteSetting.lazy_load_categories
categories_with_rownum =
Category
.secured(@guardian)
.select(:id, "ROW_NUMBER() OVER (PARTITION BY parent_category_id) rownum")
.where(parent_category_id: @categories.map { |c| c.id })
@categories +=
Category.where(
"id IN (WITH cte AS (#{categories_with_rownum.to_sql}) SELECT id FROM cte WHERE rownum <= ?)",
SUBCATEGORIES_PER_CATEGORY,
)
end
if Site.preloaded_category_custom_fields.any?
Category.preload_custom_fields(@categories, Site.preloaded_category_custom_fields)
end