SQL查询非常昂贵

时间:2019-06-01 21:58:51

标签: sql sql-server search

我在SQL Server中有一个查询,该查询搜索可用的服务提供商以进行以下预定:

  1. 我有很多与服务表相关的服务提供商
  2. 服务提供商还与职员表相关联很多
  3. 与StaffMemberService表中的staffMember相关的服务表
  4. 我有一个与staffMember表相关的约会详细信息表

您可以在此图中看到详细信息:

enter image description here

该查询对于找出哪些服务具有开放时隙非常昂贵

--select * from ServiceProvider where Id > = 636 and CityId = 4 
DECLARE @longx      NVARCHAR(20);
DECLARE @laty       NVARCHAR(20);
DECLARE @userCityId INT;
DECLARE @userLocation GEOMETRY;
Declare @serviceName  NVARCHAR(50);
declare @startDate AS DATE;
declare @endDate   AS DATE;

SET @longx = '24.72977896594770';
SET @laty = '46.82470379239910';
SET @userCityId = 4;
SET @serviceName = '%hair%'
SET @userLocation = geometry::STPointFromText('POINT(' + @laty + ' ' + @longx + ')',0);
SET @startDate = '2019-06-01';
SET @endDate = '2019-06-05';

DECLARE @compareParts AS INT;
SET @compareParts = DATEDIFF(DAY, @startDate, @endDate) * 40 ;

-- get all services like serviceName
-- find out which SP offer them
-- find out if sp has availble time in the specific dates 
WITH Serv AS
(
    SELECT s.* 
    FROM Services s 
    INNER JOIN ServiceProvider sp ON sp.Id = s.ServiceProviderId
    WHERE sp.CityId = @userCityId 
      AND s.Name LIKE @serviceName 
),
StaffServices AS
(
     SELECT s.* 
     FROM StaffMembers s 
     INNER JOIN StaffMemberService sms ON sms.StaffMemberId = s.Id
     INNER JOIN Serv ON sms.ServiceId = Serv.Id
),
StaffMemberAppointment AS
(
    SELECT ad.StaffMemberId, ad.Id, ad.Duration,a.ServiceProviderId 
    FROM AppointmentDetails ad 
    INNER JOIN Appointments a ON ad.AppointmentId = a.Id
    INNER JOIN ServiceProvider sp ON sp.Id = a.ServiceProviderId
    INNER JOIN StaffMemberService sms ON sms.StaffMemberId = ad.StaffMemberId
    INNER JOIN Serv ON Serv.Id = sms.ServiceId
    WHERE a.StartDt BETWEEN @startDate AND @endDate
),
appointmentParts AS
(
    SELECT 
        sa.StaffMemberId, sa.ServiceProviderId,  
        SUM(Datepart(minute, sa.duration) + DATEPART(hour, sa.duration) * 60 ) /15 AS appointmentsparts
    FROM
        StaffMemberAppointment sa
    GROUP BY
        sa.StaffMemberId, sa.ServiceProviderId
)
SELECT 
    sm.*,
    ap.appointmentsparts 
FROM
    StaffMembers sm
INNER JOIN
    StaffServices ss ON ss.Id = sm.Id
LEFT JOIN 
    appointmentParts ap ON sm.Id = ap.StaffMemberId

我想要查找服务名称(如提供的名称)的基本思想,然后试图查找谁可以提供服务,并检查在特定日期范围内已预定的预约时间量然后找到包含服务的服务提供商。

IO统计信息a

(5 row(s) affected)
Table 'Appointments'. Scan count 0, logical reads 12, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'AppointmentDetails'. Scan count 5, logical reads 16, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'ServiceProvider'. Scan count 0, logical reads 20, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'StaffMemberService'. Scan count 3, logical reads 28, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Services'. Scan count 6, logical reads 18, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'StaffMembers'. Scan count 0, logical reads 10, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

(1 row(s) affected)

时隙是1 15分钟。所以30分钟的约会是2个时隙。 我找不到一种简化此查询的方法,任何人都可以帮忙。

1 个答案:

答案 0 :(得分:0)

如果您删除CTE并使用带有OUTER APPLY的单级联接来计算约会空位数量,您会得到什么样的执行计划?

SELECT  sm.Id, sm.Name, sm.ServiceProviderId, sp.Name, appt.AppointmentSlots
FROM    Services svc 
        INNER JOIN ServiceProvider sp ON sp.Id = svc.ServiceProviderId
        INNER JOIN StaffMemberService sms ON sms.ServiceId = svc.Id
        INNER JOIN StaffMembers sm ON sm.Id = sms.StaffMemberId
        OUTER APPLY 
        (
            SELECT  SUM( Datepart(minute, ad.duration) 
                        + Datepart(hour, ad.duration)*60 
                    ) / 15 AS AppointmentSlots
            FROM    Appointments a INNER JOIN AppointmentDetails ad 
                        ON  ad.AppointmentId = a.Id
            WHERE   a.ServiceProviderId = sp.ID 
            AND     ad.StaffMemberId = sms.StaffMemberId
            AND     a.StartDt between @startDate and @endDate
        ) appt
WHERE   sp.CityId = @userCityId 
AND     svc.Name like @serviceName 
AND (   AppointmentSlots < @maxSlots 
OR      AppointmentSlots IS NULL
    )