Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
bugfix(product) Optymise product grid query building (#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
rprzedzik authored Jan 19, 2022
1 parent f029a8d commit a189851
Show file tree
Hide file tree
Showing 19 changed files with 250 additions and 85 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
- build [#1513](https://github.com/ergonode/backend/issues/1513) Removed dependency on `behat/symfony2-extension` (piotrkreft)

## CHANGELOG FOR 1.1.x
#### 1.1.8
- bugfix [#1848](https://github.com/ergonode/backend/issues/1848) Long response times for product grid endpoint (rprzedzik)
#### 1.1.7
- bugfix [#1816](https://github.com/ergonode/backend/issues/1816) Fixed product audits (rprzedzik)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
value AS "%s"
FROM value_translation vt
Expand All @@ -41,11 +44,28 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'
(
SELECT
product_id,
value AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code'],
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
value::DATE AS "%s"
FROM value_translation vt
Expand All @@ -41,11 +44,24 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'( SELECT product_id, value::DATE AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code']
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
Expand All @@ -42,11 +45,28 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'
(
SELECT
product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code'],
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,42 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
DISTINCT ON (product_id) product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
LEFT JOIN language_tree lt ON lt.code = vt.language
WHERE attribute_id = \'%s\'
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'
(
SELECT
product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code'],
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,42 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
DISTINCT ON (product_id) product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
LEFT JOIN language_tree lt ON lt.code = vt.language
WHERE attribute_id = \'%s\'
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'
(
SELECT
product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code'],
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,38 @@ public function supports(AbstractAttribute $attribute): bool
public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $attribute, Language $language): void
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
DISTINCT ON (product_id) product_id,
value::NUMERIC AS "%s"
FROM value_translation vt
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
value::NUMERIC AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
LEFT JOIN language_tree lt ON lt.code = vt.language
WHERE attribute_id = \'%s\'
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'(SELECT product_id, value::NUMERIC AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code']
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,38 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(SELECT
DISTINCT ON (product_id) product_id,
value::NUMERIC AS "%s"
FROM value_translation vt
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'
(
SELECT
DISTINCT ON (product_id) product_id,
value::NUMERIC AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
LEFT JOIN language_tree lt ON lt.code = vt.language
WHERE attribute_id = \'%s\'
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'(SELECT product_id, value::NUMERIC AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code']
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
{
$info = $this->query->getLanguageNodeInfo($this->resolver->resolve($attribute, $language));

$sql = sprintf(
'(
if ($attribute->getScope()->isLocal()) {
$sql = sprintf(
'(
SELECT
DISTINCT ON (product_id) product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
Expand All @@ -37,11 +38,28 @@ public function addSelect(QueryBuilder $query, string $key, AbstractAttribute $a
AND lt.lft <= %s AND lt.rgt >= %s
ORDER BY product_id, lft DESC NULLS LAST
)',
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
$key,
$attribute->getId()->getValue(),
$info['lft'],
$info['rgt'],
);
} else {
$sql = sprintf(
'
(
SELECT
product_id,
to_jsonb(regexp_split_to_array(value,\',\')) AS "%s"
FROM value_translation vt
JOIN product_value pv ON pv.value_id = vt.value_id
WHERE attribute_id = \'%s\'
AND language = \'%s\'
)',
$key,
$attribute->getId()->getValue(),
$info['code'],
);
}

$query->addSelect(sprintf('"%s"', $key));
$query->leftJoin('p', $sql, sprintf('"%s_JT"', $key), sprintf('"%s_JT".product_id = p.id', $key));
Expand Down
Loading

0 comments on commit a189851

Please sign in to comment.