Open qsdfplkj opened 2 years ago
Note from triage: this could potentially be done in cases where there are no Includes, or for no-tracking queries with independent Includes.
I was thinking is there any performance difference between
context.Customers.TemporalAsOf(utcPointInTime).ToList()
vs.
context.Customers.TemporalAll().Where(x => EF.Property<DateTime>(x, "PeriodStart") <= utcPointInTime && utcPointInTime < EF.Property<DateTime>(x, "PeriodEnd")).ToList();
as both should return the same records.
@qsdfplkj Intuitively, I would expect the TemporalAsOf
version to be be faster, but the only way to tell for sure is to measure it.
I did some testing. The query plans look the same. I did some timing with SET STATISTICS TIME ON but it's not really conclusive.
One thing I did notice is that only the history has an index for the periodstart and periodend which shows up as a index scan.
The measurement query:
`update statistics [Orders].[dbo].[Customers] update statistics [Orders].[dbo].[CustomersHistory] GO SET STATISTICS TIME ON GO
SELECT C.Id, C.Name, C.PeriodStart, C.PeriodEnd FROM [Orders].[dbo].[Customers] FOR SYSTEM_TIME ALL AS C WHERE C.PERIODSTART <= '2022-09-15 14:24:57.7212388' AND '2022-09-15 14:24:57.7212388' < C.PERIODEND
SELECT C.Id, C.Name, C.PeriodStart, C.PeriodEnd FROM [Orders].[dbo].[Customers] FOR SYSTEM_TIME AS OF '2022-09-15 14:24:57.7212388' AS C
SET STATISTICS TIME OFF GO`
The queryplan:
No index on customers:
For my original question (The delta between two temporalAsOf():
`SELECT C.Id, C.Name, C.PeriodStart, C.PeriodEnd FROM [Orders].[dbo].[Customers] FOR SYSTEM_TIME ALL AS C WHERE C.PERIODSTART <= '2022-09-15 14:24:57.7212388' AND '2022-09-15 14:24:57.7212388' < C.PERIODEND EXCEPT SELECT C.Id, C.Name, C.PeriodStart, C.PeriodEnd FROM [Orders].[dbo].[Customers] FOR SYSTEM_TIME ALL AS C WHERE C.PERIODSTART <= '2022-09-15 14:23:57.7212388' AND '2022-09-15 14:23:57.7212388' < C.PERIODEND
SELECT C.Id, C.Name, C.PeriodStart, C.PeriodEnd FROM [Orders].[dbo].[Customers] FOR SYSTEM_TIME AS OF '2022-09-15 14:24:57.7212388' AS C EXCEPT SELECT C.Id, C.Name, C.PeriodStart, C.PeriodEnd FROM [Orders].[dbo].[Customers] FOR SYSTEM_TIME AS OF '2022-09-15 14:23:57.7212388' AS C ` This gives the queryplan:
My conclussion for now is that I think if I just use TemporalAll() to calculate the delta I get the same query plan (and performance) but I'm still worried about the index scan. Should I add it myself or is this something that SQL server or EF can/should handle?
I've a temporal table: Customers
I would like to get the delta between t1 and t2 using the TemporalAsOf . (I need new or modified items created since t1).
context.Customers.TemporalAsOf(t2).Except(context.Customers.TemporalAsOf(t1)).ToList();
Which I would expect to be translated to something like:
But I get the following exception:
Stacktrace:
EF Core version: 7.0.0-preview.7.22376.2 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .net 7.0 Operating system: Windows 10 IDE: (Visual Studio 2022 Version 17.4.0 Preview 1.0