Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

2 tables combined left join to primary key of a 3 third table

$
0
0
Hi AllLooking to improve performance of the following code.It basically generates future days for each dog. So there is a dog table and a day table with every day.These 2 table cross join and then fill in missing rows. As time moves i will fill in further future dates but will need the initial insert to be a reasonable query.All columns are covered by index's but the queries at the end take quite a long time. I would hope for index scan to just point out the missing rows especially on the final query.My query analyzing isn't the best in the world and would appreciate some advise on how to make the last query as fast as possible. [code="sql"]IF OBJECT_ID('dbo.[AllDates]', 'U') IS NOT NULL DROP TABLE dbo.[AllDates]CREATE TABLE dbo.[AllDates] ( [Date] date not null PRIMARY KEY);WITH Dates AS( SELECT CAST('2010-01-01' as date) as [Date] UNION ALL SELECT DATEADD(DAY,1,[Date]) as [Date] FROM [Dates] WHERE DATEADD(DAY,1,[Date]) < '2020-01-01')INSERT INTO dbo.[AllDates]SELECT *FROM DatesOPTION (MAXRECURSION 0)IF OBJECT_ID('dbo.[Dogs]', 'U') IS NOT NULL DROP TABLE dbo.[Dogs]CREATE TABLE dbo.[Dogs] ( [DogId] INT IDENTITY(1,1) not null PRIMARY KEY, [Born] date not null, [Died] date null)CREATE INDEX IX_DoggieDaysJoin on dbo.[Dogs](DogId,Born,[Died]);WITH DogCTE AS( SELECT 1 as [Num] UNION ALL SELECT [Num] + 1 as [Num] FROM DogCTE WHERE [Num] < 30000)INSERT INTO dbo.DogsSELECT '2010-01-01' as [Born], '2019-01-01' as [Died]FROM DogCTEOPTION (MAXRECURSION 0)IF OBJECT_ID('dbo.[DoggieDays]', 'U') IS NOT NULL DROP TABLE dbo.DoggieDays CREATE TABLE dbo.DoggieDays ( [Date] DATE not null, [DogId] INT not null, CONSTRAINT PK_DoggieDays PRIMARY KEY ([Date],[DogId]))--CREATE INDEX IX_DoggieDaysJoin2 on dbo.DoggieDays([DogId])DECLARE @START1 as DATETIMEDECLARE @END1 AS DATETIMEDECLARE @START2 as DATETIMEDECLARE @END2 AS DATETIMESET @START1 = GETDATE()INSERT INTO dbo.DoggieDays([Date],DogId)SELECT da.[Date], do.DogIdFROM dbo.[Dogs] doJOIN dbo.AllDates da on da.[Date] between do.Born and do.Died and da.[Date] between '2012-01-01' and '2015-01-01'LEFT JOIN dbo.DoggieDays dd on da.[Date] = dd.[Date] and do.DogId = dd.DogIdWHERE dd.DogId is nullSET @END1 = GETDATE()SET @START2 = GETDATE()INSERT INTO dbo.DoggieDays([Date],DogId)SELECT da.[Date], do.DogIdFROM dbo.[Dogs] doJOIN dbo.AllDates da on da.[Date] between do.Born and do.Died and da.[Date] between '2012-01-01' and '2015-01-01'LEFT JOIN dbo.DoggieDays dd on da.[Date] = dd.[Date] and do.DogId = dd.DogIdWHERE dd.DogId is nullSET @END2 = GETDATE()SELECT datediff(SECOND,@START1,@END1) AS [RUN1], datediff(SECOND,@START2,@END2) AS [RUN2][/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles