Skip to content

Commit c814c00

Browse files
committed
AC-13358: RestApi request '/rest/default/V1/categories?searchCriteria%5Bpage_size%5D=1' fails with timeout error
Added a plugin for CategoryManagementInterface::getTree. The plugin sets depth to 1 to prevent timeouts. Added performance indexes to catalog_category_entity, catalog_category_entity_int and catalog_category_product tables
1 parent 4f82be7 commit c814c00

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Plugin;
9+
10+
use Magento\Catalog\Api\CategoryManagementInterface;
11+
use Magento\Catalog\Api\Data\CategoryTreeInterface;
12+
13+
/**
14+
* Performance optimizer plugin for CategoryManagement
15+
*/
16+
class CategoryManagementPerformanceOptimizer
17+
{
18+
private const DEFAULT_MAX_DEPTH = 1; // Limit depth to prevent timeouts
19+
20+
/**
21+
* Optimize getTree method with depth limits to prevent timeouts
22+
*
23+
* @param CategoryManagementInterface $subject
24+
* @param int|null $rootCategoryId
25+
* @param int|null $depth
26+
* @return array
27+
*/
28+
public function beforeGetTree(
29+
CategoryManagementInterface $subject,
30+
$rootCategoryId = null,
31+
$depth = null
32+
): array {
33+
// Limit depth to prevent performance issues
34+
$depth = $depth ?? self::DEFAULT_MAX_DEPTH;
35+
return [$rootCategoryId, $depth];
36+
}
37+
}

app/code/Magento/Catalog/etc/db_schema.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@
285285
<index referenceId="CATALOG_CATEGORY_ENTITY_PATH" indexType="btree">
286286
<column name="path"/>
287287
</index>
288+
<!-- Performance optimization index for efficient tree traversal -->
289+
<index referenceId="CATALOG_CATEGORY_ENTITY_PARENT_LEVEL_POSITION" indexType="btree">
290+
<column name="parent_id"/>
291+
<column name="level"/>
292+
<column name="position"/>
293+
</index>
288294
</table>
289295
<table name="catalog_category_entity_datetime" resource="default" engine="innodb"
290296
comment="Catalog Category Datetime Attribute Backend Table">
@@ -401,6 +407,11 @@
401407
<index referenceId="CATALOG_CATEGORY_ENTITY_INT_STORE_ID" indexType="btree">
402408
<column name="store_id"/>
403409
</index>
410+
<!-- Performance optimization index for is_active attribute filtering -->
411+
<index referenceId="CATALOG_CATEGORY_ENTITY_INT_STORE_ACTIVE" indexType="btree">
412+
<column name="store_id"/>
413+
<column name="value"/>
414+
</index>
404415
</table>
405416
<table name="catalog_category_entity_text" resource="default" engine="innodb"
406417
comment="Catalog Category Text Attribute Backend Table">
@@ -506,6 +517,12 @@
506517
<index referenceId="CATALOG_CATEGORY_PRODUCT_PRODUCT_ID" indexType="btree">
507518
<column name="product_id"/>
508519
</index>
520+
<!-- Performance optimization index for product count queries -->
521+
<index referenceId="CATALOG_CATEGORY_PRODUCT_CATEGORY_PRODUCT_COVERING" indexType="btree">
522+
<column name="category_id"/>
523+
<column name="product_id"/>
524+
<column name="position"/>
525+
</index>
509526
</table>
510527
<table name="catalog_category_product_index" resource="default" engine="innodb"
511528
comment="Catalog Category Product Index">

app/code/Magento/Catalog/etc/db_schema_whitelist.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@
157157
},
158158
"index": {
159159
"CATALOG_CATEGORY_ENTITY_LEVEL": true,
160-
"CATALOG_CATEGORY_ENTITY_PATH": true
160+
"CATALOG_CATEGORY_ENTITY_PATH": true,
161+
"CATALOG_CATEGORY_ENTITY_PARENT_LEVEL_POSITION": true
161162
},
162163
"constraint": {
163164
"PRIMARY": true
@@ -216,7 +217,8 @@
216217
"index": {
217218
"CATALOG_CATEGORY_ENTITY_INT_ENTITY_ID": true,
218219
"CATALOG_CATEGORY_ENTITY_INT_ATTRIBUTE_ID": true,
219-
"CATALOG_CATEGORY_ENTITY_INT_STORE_ID": true
220+
"CATALOG_CATEGORY_ENTITY_INT_STORE_ID": true,
221+
"CATALOG_CATEGORY_ENTITY_INT_STORE_ACTIVE": true
220222
},
221223
"constraint": {
222224
"PRIMARY": true,
@@ -276,7 +278,8 @@
276278
"position": true
277279
},
278280
"index": {
279-
"CATALOG_CATEGORY_PRODUCT_PRODUCT_ID": true
281+
"CATALOG_CATEGORY_PRODUCT_PRODUCT_ID": true,
282+
"CATALOG_CATEGORY_PRODUCT_CATEGORY_PRODUCT_COVERING": true
280283
},
281284
"constraint": {
282285
"PRIMARY": true,

app/code/Magento/Catalog/etc/webapi_rest/di.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@
4747
<type name="Magento\Catalog\Model\CategoryRepository">
4848
<plugin name="format_category_url_key_rest_api" type="Magento\Catalog\Plugin\Model\CategoryRepositoryPlugin" />
4949
</type>
50+
<!-- Essential performance optimizations for category tree operations -->
51+
<type name="Magento\Catalog\Api\CategoryManagementInterface">
52+
<plugin name="categoryManagementPerformanceOptimizer"
53+
type="Magento\Catalog\Plugin\CategoryManagementPerformanceOptimizer" />
54+
</type>
5055
</config>

0 commit comments

Comments
 (0)