Closed. This question is opinion-based . It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post .
Closed 12 hours ago.
Improve this question
I am currently working on a project that measures the execution time(Server-Side) of SQL statements and visualizes the result to compare it with other database. I am using EF Core 6 and SQL Server.
The problem is that the EF Core logs execution times does not match the times in any way, from SSMS and I wonder why? (factor > 10x)
I measured the execution time twice (to have a second value)
- Once with EF Core logger (
Microsoft.EntityFrameworkCore.Database.Command -> Executing DbCommand
) MS EF Core logging - And with SSMS (before/after the SQL statement with
set statistics time on/off
)
Example for one SQL statement:
- EF Core: 49-60 ms
- SSMS: 377-1551 ms
1条答案
按热度按时间dsekswqp1#
Simply running the same query twice guarantees invalid values. Databases cache data aggressively which means the second query will be faster than the first simply because the data is already loaded in memory. That's why raw execution time is rarely used to optimize performance.
On top of that, results depend on the actual code. Find actually caches the objects it loads so calling Find twice in the same context will return the already loaded object. If the code uses a singleton/shared DbContext, it's always using cached data. A DbContext is a Unit-of-Work, not a database connection.
Without the actual code one can only guess what's going on. The correct way to measure database performance is to first ensure any database buffers are cleared and then execute each test enough times to get statistically meaningful results. The common way to measure performance is to use BenchmarkDotNet to repeat each test enough times to ensure valid results.
Instead of using SSMS to measure "direct" performance, one should use ADO.NET directly. This allows running the test through code and eliminates differences due to application settings.
One possible benchmark could look like this: