Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize fromCategories event store queries #100

Merged
merged 1 commit into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions src/Projection/PdoEventStoreProjector.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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