Skip to content

Commit 4747256

Browse files
committed
[dbal] Optimize query so an index is used, not filesort.
1 parent 1b0c325 commit 4747256

3 files changed

Lines changed: 13 additions & 9 deletions

File tree

‎pkg/dbal/DbalConsumerHelperTrait.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?array
3232
->andWhere('delivery_id IS NULL')
3333
->andWhere('delayed_until IS NULL OR delayed_until <= :delayedUntil')
3434
->andWhere('queue IN (:queues)')
35-
->addOrderBy('priority', 'desc')
35+
->addOrderBy('priority', 'asc')
3636
->addOrderBy('published_at', 'asc')
3737
->setParameter('delayedUntil', $now, ParameterType::INTEGER)
3838
->setParameter('queues', array_values($queues), Connection::PARAM_STR_ARRAY)

‎pkg/dbal/DbalContext.php‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public function convertMessage(array $dbalMessage): DbalMessage
154154
$dbalMessageObj->setRedelivered((bool) $dbalMessage['redelivered']);
155155
}
156156
if (isset($dbalMessage['priority'])) {
157-
$dbalMessageObj->setPriority((int) $dbalMessage['priority']);
157+
$dbalMessageObj->setPriority((int) (-1 * $dbalMessage['priority']));
158158
}
159159
if (isset($dbalMessage['published_at'])) {
160160
$dbalMessageObj->setPublishedAt((int) $dbalMessage['published_at']);
@@ -233,14 +233,18 @@ public function createDataBaseTable(): void
233233
$table->addColumn('redeliver_after', Type::BIGINT, ['notnull' => false]);
234234

235235
$table->setPrimaryKey(['id']);
236-
$table->addIndex(['published_at']);
237-
$table->addIndex(['queue']);
238-
$table->addIndex(['priority']);
239-
$table->addIndex(['delayed_until']);
240-
$table->addIndex(['priority', 'published_at']);
241-
$table->addIndex(['redeliver_after']);
242236
$table->addUniqueIndex(['delivery_id']);
243237

238+
// try to select a message index
239+
$table->addIndex(['delivery_id, delayed_until, queue']);
240+
$table->addIndex(['priority', 'published_at']);
241+
242+
// redeliver failed messages
243+
$table->addIndex(['delivery_id', 'redeliver_after']);
244+
245+
// remove expired messages
246+
$table->addIndex(['time_to_live', 'delivery_id']);
247+
244248
$sm->createTable($table);
245249
}
246250
}

‎pkg/dbal/DbalProducer.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function send(Destination $destination, Message $message): void
8585
'body' => $body,
8686
'headers' => JSON::encode($message->getHeaders()),
8787
'properties' => JSON::encode($message->getProperties()),
88-
'priority' => $message->getPriority(),
88+
'priority' => -1 * $message->getPriority(),
8989
'queue' => $destination->getQueueName(),
9090
'redelivered' => false,
9191
'delivery_id' => null,

0 commit comments

Comments
 (0)