Skip to content

Commit

Permalink
Perf/sql (#40)
Browse files Browse the repository at this point in the history
* perf: flip SQL keyset expression for potential performance improvement

* fix single order-by
  • Loading branch information
priyadi authored Apr 5, 2024
1 parent 218b01e commit a25aafc
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# 0.7.2

* fix: next page skipping bug & lazy loading Pager
* perf: flip SQL keyset expression for potential performance improvement

# 0.7.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public function countOffsetItems(int $offset = 0, ?int $limit = null): int
* post t0
* WHERE
* (
* t0.date < '2024-03-23'
* OR (
* t0.date = '2024-03-23'
* AND t0.title > 'Commodi culpa magni.'
* t0.date <= '2024-03-26'
* AND NOT (
* t0.date = '2024-03-26'
* AND t0.title <= 'Saepe eos animi qui.'
* )
* OR (
* t0.date = '2024-03-23'
* AND t0.title = 'Commodi culpa magni.'
* AND t0.id > 251
* AND NOT (
* t0.date = '2024-03-26'
* AND t0.title = 'Saepe eos animi qui.'
* AND t0.id <= 115
* )
* )
* ORDER BY
Expand Down Expand Up @@ -177,40 +177,72 @@ private function getCriteria(
$expressions = [];

foreach ($properties as $property) {
if ($i === 0) {
if (\count($properties) === 1) {
if ($property['order'] === Order::Ascending) {
$expressions[] = Criteria::expr()->gt(
$property['property'],
$property['value']
);
} else {
$expressions[] = Criteria::expr()->lt(
$property['property'],
$property['value']
);
}

$i++;
continue;
}

if ($property['order'] === Order::Ascending) {
$expressions[] = Criteria::expr()->gte(
$property['property'],
$property['value']
);
} else {
$expressions[] = Criteria::expr()->lte(
$property['property'],
$property['value']
);
}

$i++;
continue;
}

$subExpressions = [];

foreach (\array_slice($properties, 0, $i) as $excludeProperty) {
foreach (\array_slice($properties, 0, $i) as $equalProperty) {
$subExpressions[] = Criteria::expr()->eq(
$excludeProperty['property'],
$excludeProperty['value']
$equalProperty['property'],
$equalProperty['value']
);
}

if ($property['order'] === Order::Ascending) {
$subExpressions[] = Criteria::expr()->gt(
$subExpressions[] = Criteria::expr()->lte(
$property['property'],
$property['value']
);
} else {
$subExpressions[] = Criteria::expr()->lt(
$subExpressions[] = Criteria::expr()->gte(
$property['property'],
$property['value']
);
}

if (\count($subExpressions) === 1) {
$subExpression = $subExpressions[0];
} else {
$subExpression = Criteria::expr()->andX(...$subExpressions);
}
$subExpression = Criteria::expr()->not(
Criteria::expr()->andX(...$subExpressions)
);

$expressions[] = $subExpression;

$i++;
}

if (\count($expressions) > 0) {
$criteria->andWhere(Criteria::expr()->orX(...$expressions));
$criteria->andWhere(Criteria::expr()->andX(...$expressions));
}

return $criteria;
Expand Down
72 changes: 60 additions & 12 deletions packages/rekapager-doctrine-orm-adapter/src/QueryBuilderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,26 +112,76 @@ private function getQueryBuilder(
$z = 1;

foreach ($properties as $property) {
if ($i === 0) {
if (\count($properties) === 1) {
if ($property['order'] === 'ASC') {
$expressions[] = $queryBuilder->expr()->gt(
$property['property'],
":rekapager_where_{$z}"
);
} else {
$expressions[] = $queryBuilder->expr()->lt(
$property['property'],
":rekapager_where_{$z}"
);
}

$queryBuilder->setParameter(
"rekapager_where_{$z}",
$property['value'],
// @phpstan-ignore-next-line
$this->getType($property['property'])
);

$i++;
continue;
}


if ($property['order'] === 'ASC') {
$expressions[] = $queryBuilder->expr()->gte(
$property['property'],
":rekapager_where_{$z}"
);
} else {
$expressions[] = $queryBuilder->expr()->lte(
$property['property'],
":rekapager_where_{$z}"
// $property['value']
);
}

$queryBuilder->setParameter(
"rekapager_where_{$z}",
$property['value'],
// @phpstan-ignore-next-line
$this->getType($property['property'])
);

$i++;
continue;
}

$subExpressions = [];

foreach (\array_slice($properties, 0, $i) as $excludeProperty) {
foreach (\array_slice($properties, 0, $i) as $equalProperty) {
$subExpressions[] = $queryBuilder->expr()->eq(
$excludeProperty['property'],
$equalProperty['property'],
":rekapager_where_{$z}"
);

$queryBuilder->setParameter(
"rekapager_where_{$z}",
$excludeProperty['value'],
$equalProperty['value'],
// @phpstan-ignore-next-line
$this->getType($excludeProperty['property'])
$this->getType($equalProperty['property'])
);

$z++;
}

if ($property['order'] === 'ASC') {
$subExpressions[] = $queryBuilder->expr()->gt(
$subExpressions[] = $queryBuilder->expr()->lte(
$property['property'],
":rekapager_where_{$z}"
);
Expand All @@ -145,7 +195,7 @@ private function getQueryBuilder(

$z++;
} else {
$subExpressions[] = $queryBuilder->expr()->lt(
$subExpressions[] = $queryBuilder->expr()->gte(
$property['property'],
":rekapager_where_{$z}"
);
Expand All @@ -160,19 +210,17 @@ private function getQueryBuilder(
$z++;
}

if (\count($subExpressions) === 1) {
$subExpression = $subExpressions[0];
} else {
$subExpression = $queryBuilder->expr()->andX(...$subExpressions);
}
$subExpression = $queryBuilder->expr()->not(
$queryBuilder->expr()->andX(...$subExpressions)
);

$expressions[] = $subExpression;

$i++;
}

if (\count($expressions) > 0) {
$queryBuilder->andWhere($queryBuilder->expr()->orX(...$expressions));
$queryBuilder->andWhere($queryBuilder->expr()->andX(...$expressions));
}

// adds the boundary values to the query
Expand Down

0 comments on commit a25aafc

Please sign in to comment.