[英]Improve performance of slow sub query in SQL Server
I have a table with 1.3 million rows.我有一张有 130 万行的表。
Query #1 takes 29 seconds to run in SQL Server 2016 Management Studio.查询 #1 在 SQL Server 2016 Management Studio 中运行需要 29 秒。
Query #1:查询 #1:
select
*,
(select Count(*)
from [dbo].[Results] t2
where t2.RaceDate < t1.RaceDate
and t1.HorseName = t2.HorseName
and t2.Position = '1'
and t1.CourseName = t2.CourseName
and t2.CountryCode = 'GB') as [CourseDistanceWinners]
from
[dbo].[Results] t1
But query #2 takes takes several hours with the only difference being t1.HorseName = t2.HorseName
vs t1.TrainerName = t2.TrainerName
.但是查询 #2 需要几个小时,唯一的区别是
t1.HorseName = t2.HorseName
与t1.TrainerName = t2.TrainerName
。 There will be many more matches but on TrainerName than HorseName but I wasn't expecting several hours.会有更多的比赛,但在 TrainerName 上的比赛比在 HorseName 上的比赛要多,但我没想到会有几个小时。
Query #2:查询 #2:
select
*,
(select Count(*)
from [dbo].[Results] t2
where t2.RaceDate < t1.RaceDate
and t1.TrainerName = t2.TrainerName
and t2.Position = '1'
and t1.CourseName = t2.CourseName
and t2.CountryCode = 'GB') as [CourseDistanceWinners]
from
[dbo].[Results] t1
I've managed to get the query down to 15 minutes using the techniques below but I still think this is a very long time.我已经设法使用下面的技术将查询缩短到 15 分钟,但我仍然认为这是一个很长的时间。 Is there anything else I can do to improve performance of Query2 or a way to rewrite it for performance?
我还能做些什么来提高 Query2 的性能或重写它以提高性能吗?
What I have tried so far到目前为止我尝试过的
I've changed [TrainerName] [nvarchar](255) NULL,
to [TrainerName] [nvarchar](50) NULL,
我已将
[TrainerName] [nvarchar](255) NULL,
为[TrainerName] [nvarchar](50) NULL,
I've added a composite index and several non clustered indexes我添加了一个复合索引和几个非聚集索引
CREATE INDEX idx_HorseName ON [dbo].[Results] (HorseName); CREATE INDEX idx_TrainerName ON [dbo].[Results] (TrainerName); CREATE INDEX idx_CourseName ON [dbo].[Results] (CourseName); CREATE INDEX idx_Position ON [dbo].[Results] (Position); CREATE INDEX idx_JockeyName ON [dbo].[Results] (JockeyName); CREATE INDEX idx_RaceDate ON [dbo].[Results] (RaceDate); CREATE INDEX idx_TrainerComposite ON [dbo].[Results] (TrainerName, RaceDate, CourseName);
Further info:更多信息:
Table structure:表结构:
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Results]
(
[CountryCode] [NVARCHAR](50) NULL,
[CourseName] [NVARCHAR](50) NULL,
[HorseName] [NVARCHAR](50) NOT NULL,
[HorseSuffix] [NVARCHAR](5) NOT NULL,
[JockeyName] [NVARCHAR](255) NULL,
[OwnerName] [NVARCHAR](255) NULL,
[Position] [NVARCHAR](255) NULL,
[PublishedTime] [NVARCHAR](6) NOT NULL,
[RaceDate] [DATETIME] NOT NULL,
[RaceTitle] [NVARCHAR](255) NULL,
[StallPosition] [NVARCHAR](255) NULL,
[TrainerName] [NVARCHAR](50) NULL,
[Rating] [INT] NULL,
CONSTRAINT [PK_Results_1]
PRIMARY KEY CLUSTERED ([HorseName] ASC,
[HorseSuffix] ASC,
[PublishedTime] ASC,
[RaceDate] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
Query #1 execution plan:查询 #1 执行计划:
Query #2 execution plan:查询 #2 执行计划:
Use a window function!使用 window 函数!
select r.*,
sum(case when position = 1 and country_code = 'GB' then 1 else 0 end) over
(partition by horsename, coursename
order by racedate
rows between unbounded preceding and 1 preceding
) as CourseDistanceWinners
from [dbo].[Results] r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.