From 593639907829931ba33e39ae4771d67c02f44be7 Mon Sep 17 00:00:00 2001 From: Akash Kava <39438041+ackava@users.noreply.github.com> Date: Mon, 31 Jul 2023 09:46:49 +0530 Subject: [PATCH] Query improved --- .../EternitySqlStorage.cs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/NeuroSpeech.Eternity.SqlStorage/EternitySqlStorage.cs b/NeuroSpeech.Eternity.SqlStorage/EternitySqlStorage.cs index 23627bb..bc04c9a 100644 --- a/NeuroSpeech.Eternity.SqlStorage/EternitySqlStorage.cs +++ b/NeuroSpeech.Eternity.SqlStorage/EternitySqlStorage.cs @@ -56,7 +56,7 @@ public async Task> DequeueAsync(int max, DateTimeOffset utc var now = utcNow.UtcDateTime; var futureLock = now.AddMinutes(1); var query = TemplateQuery.New(@$" - SELECT TOP ({max}) * + SELECT TOP ({max}) nID FROM [{schemaName}].[{tableName}] WHERE UtcETA < {now} @@ -64,24 +64,20 @@ FROM [{schemaName}].[{tableName}] AND (QueueTTL IS NULL OR QueueTTL<{now}) ORDER BY Priority DESC"); var list = await db.FromSqlAsync(query, ignoreUnmatchedProperties: true); - var rlist = new List(); - var ids = new List(); - foreach(var item in list) - { - rlist.Add(item); - ids.Add(item.NID); - } + var ids = list.Select((x) => x.NID); if (ids.Any()) { - await db.ExecuteNonQueryAsync(TemplateQuery.New(@$" + var result = await db.FromSqlAsync(TemplateQuery.New(@$" UPDATE [{schemaName}].[{tableName}] SET QueueTTL={futureLock} + OUTPUT INSERTED.* WHERE nID IN ({ids}) - ")); + "), ignoreUnmatchedProperties: true); + return result.OfType().OrderBy(x => x.UtcETA).ToList(); } - return rlist; + return new List(); } public async Task GetAsync(string? id)