Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Improve perf of token cleanup #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

feanz
Copy link

@feanz feanz commented May 13, 2016

resolves #89

Use entity framework extended to run the token clean up process. This
pevent the cleanup code pulling the data back before deleting it.

Add an index to the expiry field to improve performance of selection.

This current solution does not include @tbarcelon proposed plan to move the clustered index to expiry. It just adds an extra non clustered index.

Note: I've included a test that I would remove before merging this code it just so you can run it quickly run it to check it works.

I've profiled the sql that gets executed by entityframework extended.

DELETE [dbo].[Tokens]
FROM [dbo].[Tokens] AS j0 INNER JOIN (
SELECT 
    1 AS [C1], 
    [Extent1].[Key] AS [Key], 
    [Extent1].[TokenType] AS [TokenType]
    FROM [dbo].[Tokens] AS [Extent1]
    WHERE [Extent1].[Expiry] < '2016-05-13 14:19:03.6501655 +00:00'
) AS j1 ON (j0.[Key] = j1.[Key] AND j0.[TokenType] = j1.[TokenType])

Included execution plan
image

Less than ideal compared to the alternative but probably good enough.

DELETE FROM [dbo].[Tokens] WHERE [Expiry] < '2016-05-13 14:19:03.6501655 +00:00'

image

Use entity framework extended to run the token clean up process.  This
pevent the cleanup code pulling the data back before deleting it.

Add an index to the expiry field to improve performance of selection.
@dnfclas
Copy link

dnfclas commented May 13, 2016

Hi @feanz, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution!

In order for us to evaluate and accept your PR, we ask that you sign a contribution license agreement. It's all electronic and will take just minutes. I promise there's no faxing. https://cla2.dotnetfoundation.org.

TTYL, DNFBOT;

@dnfclas
Copy link

dnfclas commented May 13, 2016

@feanz, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR.

Thanks, DNFBOT;

@brockallen brockallen self-assigned this May 25, 2016
@@ -136,6 +138,8 @@ public static void ConfigureConsents(this DbModelBuilder modelBuilder, string sc
public static void ConfigureTokens(this DbModelBuilder modelBuilder, string schema)
{
modelBuilder.Entity<Token>().ToTable(EfConstants.TableNames.Token, schema);
modelBuilder.Entity<Token>().Property(x => x.Expiry)
.HasColumnAnnotation("Index", new IndexAnnotation(new IndexAttribute("Expiry") { IsUnique = false}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I guess this is technically a schema change? Well, an index... that's schema-ish, right? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, then, what do migrations look like -- do they know how to include this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep this is defo a schema change. For migrations it depends how consumers are using this package. When I pulled in IdentityServer3.EntityFramework I explicitly created migrations based on the model. You end up with different migrations sets for

  • ScopeConfigurationDbContext
  • ClientConfigurationDbContext
  • OperationalDbContext

But consumers could setup auto migrations which means ef will run this for you.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, so I wonder how to go about releasing this. Do we do a point release (e.g. 2.6.0) or do we wait for IdSvr release/build 3.0.0 which we know will introduce schema changes. I think I'd rather wait for IdSvr release/build 3.0.0.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think that makes sense pull it in in three. As long as there is a current issue that allows users to find the details about then it's not a massive issue. You can add the index yourself.

@brockallen
Copy link
Member

Just got around to this now -- I like the idea and sorry for the delay.

@ghost ghost removed the cla-signed label Dec 7, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Table scans on idsrv.Tokens table when querying for expired tokens
3 participants