Skip to content

Commit

Permalink
Merge branch 'next-34023/slow-query' into 'trunk'
Browse files Browse the repository at this point in the history
NEXT-34023 - Slow query with criteria term

See merge request shopware/6/product/platform!13146
  • Loading branch information
OliverSkroblin committed Feb 27, 2024
2 parents 8741de3 + 63af636 commit 86c3443
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Slow query with criteria term
issue: NEXT-34023
author: oskroblin Skroblin
author_email: [email protected]
---

# Core
* Removed `SearchRanking` from `product.categories` and `product.tags` association to improve search performance when providing a criteria term via API
4 changes: 2 additions & 2 deletions src/Core/Content/Product/ProductDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,13 @@ protected function defineFields(): FieldCollection

(new ManyToManyAssociationField('properties', PropertyGroupOptionDefinition::class, ProductPropertyDefinition::class, 'product_id', 'property_group_option_id'))->addFlags(new ApiAware(), new CascadeDelete(), new Inherited()),

(new ManyToManyAssociationField('categories', CategoryDefinition::class, ProductCategoryDefinition::class, 'product_id', 'category_id'))->addFlags(new ApiAware(), new CascadeDelete(), new Inherited(), new SearchRanking(SearchRanking::ASSOCIATION_SEARCH_RANKING)),
(new ManyToManyAssociationField('categories', CategoryDefinition::class, ProductCategoryDefinition::class, 'product_id', 'category_id'))->addFlags(new ApiAware(), new CascadeDelete(), new Inherited()),

(new ManyToManyAssociationField('streams', ProductStreamDefinition::class, ProductStreamMappingDefinition::class, 'product_id', 'product_stream_id'))->addFlags(new ApiAware(), new CascadeDelete()),

(new ManyToManyAssociationField('categoriesRo', CategoryDefinition::class, ProductCategoryTreeDefinition::class, 'product_id', 'category_id'))->addFlags(new ApiAware(), new CascadeDelete(false), new WriteProtected()),

(new ManyToManyAssociationField('tags', TagDefinition::class, ProductTagDefinition::class, 'product_id', 'tag_id'))->addFlags(new CascadeDelete(), new Inherited(), new SearchRanking(SearchRanking::ASSOCIATION_SEARCH_RANKING), new ApiAware()),
(new ManyToManyAssociationField('tags', TagDefinition::class, ProductTagDefinition::class, 'product_id', 'tag_id'))->addFlags(new CascadeDelete(), new Inherited(), new ApiAware()),

(new ManyToManyAssociationField('customFieldSets', CustomFieldSetDefinition::class, ProductCustomFieldSetDefinition::class, 'product_id', 'custom_field_set_id'))->addFlags(new CascadeDelete(), new Inherited()),

Expand Down
45 changes: 45 additions & 0 deletions tests/unit/Core/Content/Product/ProductDefinitionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Shopware\Tests\Unit\Core\Content\Product;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Product\ProductDefinition;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\EntityWriteGateway;
use Shopware\Core\Framework\DataAbstractionLayer\Field\Flag\SearchRanking;
use Shopware\Core\Test\Stub\DataAbstractionLayer\StaticDefinitionInstanceRegistry;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* @internal
*/
#[CoversClass(ProductDefinition::class)]
class ProductDefinitionTest extends TestCase
{
public function testSearchFields(): void
{
// don't change this list, each additional field will reduce the performance

$registry = new StaticDefinitionInstanceRegistry(
[ProductDefinition::class],
$this->createMock(ValidatorInterface::class),
$this->createMock(EntityWriteGateway::class)
);

$definition = $registry->getByEntityName('product');

$fields = $definition->getFields();

$searchable = $fields->filterByFlag(SearchRanking::class);

$keys = $searchable->getKeys();

// NEVER add an association to this list!!! otherwise, the API query takes too long and shops with many products (more than 1000) will fail
$expected = ['customSearchKeywords', 'productNumber', 'manufacturerNumber', 'ean', 'name'];

sort($expected);
sort($keys);

static::assertEquals($expected, $keys);
}
}

0 comments on commit 86c3443

Please sign in to comment.