mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user