[英]Slow query with CROSS APPLY
I have the following SQL code which takes several seconds to execute: 我有以下SQL代码,需要花费几秒钟的时间来执行:
SELECT instance.startdate
,instance.enddate
FROM (select mr.id
FROM meeting_recurrence AS mr
inner JOIN meeting_details md ON md.id = mr.meeting_template_id
WHERE host_user_id=17) AS p
CROSS APPLY GetRecurrenceMeetingInstances(p.id,'2018-11-09 11:00:00','2018-11-09 15:00:00') AS instance
WHERE 21015 IS NOT NULL
AND instance.meetingid <> 21015
The internal select (select mr.id FROM meeting_recurrence...)
returns in the above example instantly, with two ids. 在上面的示例中,内部选择(select mr.id FROM meeting_recurrence...)
立即返回,带有两个ID。
Running GetRecurrenceMeetingInstances()
with those two ids separately, returns empty results instantly. GetRecurrenceMeetingInstances()
使用这两个ID运行GetRecurrenceMeetingInstances()
会立即返回空结果。
Why does running the entire statement take so long compared to running them separately? 与单独运行它们相比,为什么运行整个语句要花这么长时间?
Execution plan image: https://imgur.com/a/3Ydym 执行计划图片: https : //imgur.com/a/3Ydym
It seems CROSS APPLY was calling GetRecurrenceMeetingInstances() a lot more than i thought. 似乎CROSS APPLY打电话给GetRecurrenceMeetingInstances()比我想的要多得多。 I solved the problem by saving the inner select in a local variable first: 我先将内部选择保存在局部变量中解决了这个问题:
DECLARE @innerresult table(meetingid int)
INSERT INTO @innerresult(meetingid) (select mr.id FROM meeting_recurrence AS mr inner JOIN meeting_details md ON md.id = mr.meeting_template_id
WHERE host_user_id=17)
INSERT INTO @timeslots (startdate,enddate)
SELECT instance.startdate,instance.enddate
FROM (select meetingid FROM @innerresult) AS p
CROSS APPLY GetRecurrenceMeetingInstances(p.meetingid,'2018-11-09 11:00:00','2018-11-09 15:00:00') AS instance
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.