question

AndrewHB-6266 avatar image
0 Votes"
AndrewHB-6266 asked AndrewHB-6266 answered

Very slow database creation within Azure function project deployed to Premium plan

Something I have discovered whilst investigating some performance issues is that it is taking entity framework between 6 and 7 seconds to perform the first database operation (the database has 152 tables) when deployed into an Azure Premium Function Plan. Our project references Microsoft.NETCore.App 3.1.0 and Microsoft.EntityFrameworkCore 3.1.1 and the database context is added via dependency injection during Startup.cs using the following code: -

builder.Services.AddDbContext<OltpContext>(
                    options => options.UseLazyLoadingProxies()
                        .UseSqlServer(
                            oltpContext,
                            sqloptionsbuilder => { sqloptionsbuilder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null); }),
                    ServiceLifetime.Scoped,
                    ServiceLifetime.Scoped);


Our database context service lifetimes are Scoped because we need to ensure that each request gets it's own copy of the database context because we are using global filters to restrict access to data which can potentially change from request to request.

Surely 6-7 seconds is not acceptable, our synchronisation process calls the API endpoint 10 times for different data sets and this equates to 60 seconds before we've even done any work.

If the answer is to use database contexts with service lifetimes of Singleton how can you use global filters to filter data between requests and maintain integrity of the calls and scale?

azure-functionsdotnet-entity-framework-core
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AndrewHB-6266 avatar image
0 Votes"
AndrewHB-6266 answered

actually sorry ignore this, the 6/7 seconds only seems to apply the very first time the app is used from a new deployment, subsequent calls seem instant!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.