Skip to content

[5.x]: N+1 Query Issue: Duplicate commerce_plans queries on CP load #4183

@wmdhosting

Description

@wmdhosting

What happened?

Description:
I am experiencing a performance degradation in the Control Panel due to duplicated queries related to Subscription Plans.

Even though I am not actively using Subscriptions ), the system runs the exact same query for commerce_plans multiple times (10+) on every single page load of the Control Panel. It appears that the result of this query is not being memoized/cached during the request, causing it to run repeatedly—likely once for every item in the Sidebar navigation.

Steps to reproduce:
Enable the Debug Toolbar (or look at web.log profiling).

Load any page in the Control Panel (e.g., the Dashboard or Entries index).

Inspect the Database Queries.

Observe that the following query is repeated multiple times (identical execution):

SQL
SELECT p.dateArchived, p.dateCreated, p.dateUpdated, p.enabled, p.gatewayId, p.handle, p.id, p.isArchived, p.name, p.planData, p.planInformationId, p.reference, p.sortOrder, p.uid
FROM commerce_plans p
LEFT JOIN commerce_gateways g ON g.id = p.gatewayId
WHERE g.isArchived=FALSE
ORDER BY sortOrder
Additional info:

Impact: This results in 10-20 extra database queries per page load, contributing to slower CP load times, especially on sites with complex navigation or many plugins hooking into the sidebar.

Craft CMS version

5.8.20

Craft Commerce version

5.5.0

PHP version

8.3

Operating system and version

Windows 11

Database type and version

MySQL

Image driver and version

No response

Installed plugins and versions

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions