@@ -190,6 +190,10 @@ public async Task UpdateUserIndicatorOnEndpoints(List<UpdateUserIndicator> userI
190190 . Where ( document => document . SanitizedName . In ( updates . Keys ) || document . EndpointId . Name . In ( updates . Keys ) ) ;
191191
192192 var documents = await query . ToListAsync ( cancellationToken ) ;
193+
194+ // Collect sanitized names needing sibling propagation to avoid issuing a query per document in the loop below.
195+ var sanitizedNameToUserIndicator = new Dictionary < string , string > ( ) ;
196+
193197 foreach ( var document in documents )
194198 {
195199 if ( updates . TryGetValue ( document . SanitizedName , out var newValueFromSanitizedName ) )
@@ -199,14 +203,25 @@ public async Task UpdateUserIndicatorOnEndpoints(List<UpdateUserIndicator> userI
199203 else if ( updates . TryGetValue ( document . EndpointId . Name , out var newValueFromEndpoint ) )
200204 {
201205 document . UserIndicator = newValueFromEndpoint ;
202- //update all that match this sanitized name
203- var sanitizedMatchingQuery = session . Query < EndpointDocument > ( )
204- . Where ( sanitizedDocument => sanitizedDocument . SanitizedName == document . SanitizedName && sanitizedDocument . EndpointId . Name != document . EndpointId . Name ) ;
205- var sanitizedMatchingDocuments = await sanitizedMatchingQuery . ToListAsync ( cancellationToken ) ;
206+ sanitizedNameToUserIndicator [ document . SanitizedName ] = newValueFromEndpoint ;
207+ }
208+ }
206209
207- foreach ( var matchingDocumentOnSanitizedName in sanitizedMatchingDocuments )
210+ if ( sanitizedNameToUserIndicator . Count > 0 )
211+ {
212+ // One batched query for all sibling documents, instead of one query per document.
213+ var sanitizedNames = sanitizedNameToUserIndicator . Keys . ToList ( ) ;
214+ var alreadyLoadedIds = documents . Select ( d => d . Id ) . ToHashSet ( ) ;
215+
216+ var siblingDocuments = await session . Query < EndpointDocument > ( )
217+ . Where ( d => d . SanitizedName . In ( sanitizedNames ) )
218+ . ToListAsync ( cancellationToken ) ;
219+
220+ foreach ( var sibling in siblingDocuments . Where ( d => ! alreadyLoadedIds . Contains ( d . Id ) ) )
221+ {
222+ if ( sanitizedNameToUserIndicator . TryGetValue ( sibling . SanitizedName , out var indicator ) )
208223 {
209- matchingDocumentOnSanitizedName . UserIndicator = newValueFromEndpoint ;
224+ sibling . UserIndicator = indicator ;
210225 }
211226 }
212227 }
0 commit comments