Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

order by predicate to prevent table scan #5092

Merged

Conversation

edmacdonald
Copy link
Contributor

See #5091

This block is resulting in a full scan during clean up:

var expiredGrants = await _persistedGrantDbContext.PersistedGrants
.Where(x => x.Expiration < DateTime.UtcNow)
.OrderBy(x => x.Key)
.Take(_options.TokenCleanupBatchSize)
.ToArrayAsync();

Sorting by the same column used in the predicate fixes it

                var expiredGrants = await _persistedGrantDbContext.PersistedGrants
                    .Where(x => x.Expiration < DateTime.UtcNow)
                    .OrderBy(x => x.Expiration)
                    .Take(_options.TokenCleanupBatchSize)
                    .ToArrayAsync();

@edmacdonald
Copy link
Contributor Author

@brockallen here's a comparison of the query plans if that helps. Changing the Order By makes it about 100 times more efficient. Note that this was taken during a "quiet time" so the 60k rows read gets much worse for us when it's busy.
https://www.brentozar.com/pastetheplan/?id=H1d80r93D

@leastprivilege leastprivilege merged commit 3ff3b46 into IdentityServer:main Apr 7, 2021
@leastprivilege
Copy link
Member

Hi

thanks! Sorry, took a while.

@leastprivilege leastprivilege added this to the 4.1.3 milestone Apr 7, 2021
@github-actions
Copy link

github-actions bot commented Apr 8, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants