Search code examples
sqlsql-serverjoinleft-joinquery-optimization

Join multiple tables by multiple grouping


We have a passing control system and every pass action is stored Event table in MSSQL Server. We want to join multiple tables with the Event table according to their relations as shown on the image below. However, I am not sure if the grouping approach that I used is correct or not because the query takes a lot of time. Could you please clarify me oh how to join these tables by multiple grouping? Here is the JOIN clause I used:

SELECT t.CardNo, t.EventTime, t1.EmployeeName, 
    t1.Status, t2.EventCH, t3.DoorName, t4.JobName, t5.DeptName 
FROM Event t 

LEFT JOIN Employee AS t1 ON t.EmployeeID = t1.ID 
LEFT JOIN EventType AS t2 ON t.EventTypeID = t2.ID 
LEFT JOIN Door AS t3 ON t.DoorID = t3.ID 
LEFT JOIN Job AS t4 ON t1.JobID = t4.ID 
LEFT JOIN Department AS t5 ON t1.DepartmentID = t5.ID

ORDER BY t.EventID Desc

enter image description here

Update: Posted execution plan below:

enter image description here


Solution

  • Hey have you tried creating two CTE's to group the joins?

    So in one CTE create a join for Employee, Department and Job. For the other CTE create a join for Event, Eventtype and Door. Then in the end, join the two CTE's using Employee ID and ID.

    Aggregating the joined tables together might be quicker than doing the joins all in one go. By the way, hows the unique constraint for each table?

    ;WITH a AS
    
    (
    SELECT
        t1.ID
       ,t1.EmployeeName
       ,t1.Status
       ,t4.JobName
       ,t5.DeptName
    FROM
        Employee t1
    LEFT JOIN Job t4
        ON t1.JobID = t4.ID
    LEFT JOIN Department t5
        ON t1.DepartmentID = t5.ID
    )
    ,b AS
    (
    SELECT
        t.EmployeeID
       ,t.CardNo
       ,t.EventTime
       ,t2.EventCH
       ,t3.DoorName
    FROM
        [Event] t
    LEFT JOIN EventType t2
        ON t.EventTypeID = t2.ID
    LEFT JOIN Door t3
        ON t.DoorID = t3.ID 
    )
    
    SELECT
        *
    FROM
        b
    LEFT JOIN a
        ON b.EmployeeID = a.ID