From d028ebfdf1cc2f80663bf177dee4e6db0db86fec Mon Sep 17 00:00:00 2001 From: Nicolas Dorier Date: Sat, 30 Apr 2022 12:54:44 +0900 Subject: [PATCH] Improve performance of Invoice count (#3688) --- .../Services/Invoices/InvoiceRepository.cs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/BTCPayServer/Services/Invoices/InvoiceRepository.cs b/BTCPayServer/Services/Invoices/InvoiceRepository.cs index 9f875447f..83620fe1d 100644 --- a/BTCPayServer/Services/Invoices/InvoiceRepository.cs +++ b/BTCPayServer/Services/Invoices/InvoiceRepository.cs @@ -646,8 +646,18 @@ namespace BTCPayServer.Services.Invoices if (queryObject.StoreId != null && queryObject.StoreId.Length > 0) { - var stores = queryObject.StoreId.ToHashSet().ToArray(); - query = query.Where(i => stores.Contains(i.StoreDataId)); + if (queryObject.StoreId.Length > 1) + { + var stores = queryObject.StoreId.ToHashSet().ToArray(); + query = query.Where(i => stores.Contains(i.StoreDataId)); + } + // Big performant improvement to use Where rather than Contains when possible + // In our test, the first gives 720.173 ms vs 40.735 ms + else + { + var storeId = queryObject.StoreId.First(); + query = query.Where(i => i.StoreDataId == storeId); + } } if (!string.IsNullOrEmpty(queryObject.TextSearch))