Skip to content

Commit

Permalink
optimize fromCategories event store queries
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Jul 10, 2017
1 parent 84928c9 commit b2782d8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 47 deletions.
19 changes: 4 additions & 15 deletions src/Projection/PdoEventStoreProjector.php
Expand Up @@ -13,7 +13,6 @@
namespace Prooph\EventStore\Pdo\Projection;

use ArrayIterator;
use CachingIterator;
use Closure;
use DateTimeImmutable;
use DateTimeZone;
Expand Down Expand Up @@ -861,25 +860,15 @@ private function prepareStreamPositions(): void
}

if (isset($this->query['categories'])) {
$it = new CachingIterator(new ArrayIterator($this->query['categories']), CachingIterator::FULL_CACHE);

$where = 'WHERE ';
$params = [];

foreach ($it as $name) {
$where .= 'real_stream_name LIKE ?';
if ($it->hasNext()) {
$where .= ' OR ';
}
$params[] = $name . '-%';
}
$rowPlaces = implode(', ', array_fill(0, count($this->query['categories']), '?'));

$sql = <<<EOT
SELECT real_stream_name FROM $this->eventStreamsTable $where;
SELECT real_stream_name FROM $this->eventStreamsTable WHERE category IN ($rowPlaces);
EOT;
$statement = $this->connection->prepare($sql);

try {
$statement->execute($params);
$statement->execute($this->query['categories']);
} catch (PDOException $exception) {
// ignore and check error code
}
Expand Down
20 changes: 4 additions & 16 deletions src/Projection/PdoEventStoreQuery.php
Expand Up @@ -12,8 +12,6 @@

namespace Prooph\EventStore\Pdo\Projection;

use ArrayIterator;
use CachingIterator;
use Closure;
use Iterator;
use PDO;
Expand Down Expand Up @@ -382,25 +380,15 @@ private function prepareStreamPositions(): void
}

if (isset($this->query['categories'])) {
$it = new CachingIterator(new ArrayIterator($this->query['categories']), CachingIterator::FULL_CACHE);

$where = 'WHERE ';
$params = [];

foreach ($it as $name) {
$where .= 'real_stream_name LIKE ?';
if ($it->hasNext()) {
$where .= ' OR ';
}
$params[] = $name . '-%';
}
$rowPlaces = implode(', ', array_fill(0, count($this->query['categories']), '?'));

$sql = <<<EOT
SELECT real_stream_name FROM $this->eventStreamsTable $where;
SELECT real_stream_name FROM $this->eventStreamsTable WHERE category IN ($rowPlaces);
EOT;
$statement = $this->connection->prepare($sql);

try {
$statement->execute($params);
$statement->execute($this->query['categories']);
} catch (PDOException $exception) {
// ignore and check error code
}
Expand Down
20 changes: 4 additions & 16 deletions src/Projection/PdoEventStoreReadModelProjector.php
Expand Up @@ -12,8 +12,6 @@

namespace Prooph\EventStore\Pdo\Projection;

use ArrayIterator;
use CachingIterator;
use Closure;
use DateTimeImmutable;
use DateTimeZone;
Expand Down Expand Up @@ -811,25 +809,15 @@ private function prepareStreamPositions(): void
}

if (isset($this->query['categories'])) {
$it = new CachingIterator(new ArrayIterator($this->query['categories']), CachingIterator::FULL_CACHE);

$where = 'WHERE ';
$params = [];

foreach ($it as $name) {
$where .= 'real_stream_name LIKE ?';
if ($it->hasNext()) {
$where .= ' OR ';
}
$params[] = $name . '-%';
}
$rowPlaces = implode(', ', array_fill(0, count($this->query['categories']), '?'));

$sql = <<<EOT
SELECT real_stream_name FROM $this->eventStreamsTable $where;
SELECT real_stream_name FROM $this->eventStreamsTable WHERE category IN ($rowPlaces);
EOT;
$statement = $this->connection->prepare($sql);

try {
$statement->execute($params);
$statement->execute($this->query['categories']);
} catch (PDOException $exception) {
// ignore and check error code
}
Expand Down

0 comments on commit b2782d8

Please sign in to comment.