Search code examples
sqlsql-serverviewtimeoutexecution

SSMS executing view timeout


I'm having an issue with a SQL Execution Error. I'm trying to execute a view and I'm getting an "Execution Timeout Expired" error. The code I am running is below. I need to run this as a view, because these tables are linked to a dbo that automatically updates, and I need this view to be as current as that when an MS Access report uses it. (from my understanding views don't need to be executed manually to be current)

I have tried to change all the timeout locations including in Tools->Options, within the login connection Options, and within Registry Editor but I'm still getting the error.

It times out after 30seconds, so where else can I change this? Is this not working because I don't have full "IT admin" rights (even though it looks like my changes saved)? And/Or is there a better way to write this code so it works?

Basically the information in table 1 is also in table 2. I want all the information from table 2 EXCEPT the information that matches in table 1.

Thank you so much in advance!!!

SELECT        TOP (100) PERCENT dbo.vwCOVIDLocations2.Hospital, dbo.vwCOVIDLocations2.Department, dbo.vwCOVIDLocations2.MRN, dbo.vwCOVIDLocations2.[Patient Name], dbo.vwCOVIDLocations2.[Order Date], 
                         dbo.vwCOVIDLocations2.Result
FROM            dbo.vwCOVIDDetected2 LEFT OUTER JOIN
                         dbo.vwCOVIDLocations2 ON dbo.vwCOVIDDetected2.MRN <> dbo.vwCOVIDLocations2.MRN AND dbo.vwCOVIDDetected2.[Order Date] <> dbo.vwCOVIDLocations2.[Order Date] AND 
                         dbo.vwCOVIDDetected2.Result <> dbo.vwCOVIDLocations2.Result
GROUP BY dbo.vwCOVIDLocations2.Hospital, dbo.vwCOVIDLocations2.Department, dbo.vwCOVIDLocations2.MRN, dbo.vwCOVIDLocations2.[Patient Name], dbo.vwCOVIDLocations2.[Order Date], dbo.vwCOVIDLocations2.Result
ORDER BY dbo.vwCOVIDLocations2.[Patient Name]

Solution

  • I'm not sure if it helps but I wouldn't increase the timeout. I would rewrite the query for faster execution, especially the left outer join. You can try the following:

    SELECT TOP (100) PERCENT dbo.vwCOVIDLocations2.Hospital
        ,dbo.vwCOVIDLocations2.Department
        ,dbo.vwCOVIDLocations2.MRN
        ,dbo.vwCOVIDLocations2.[Patient Name]
        ,dbo.vwCOVIDLocations2.[Order Date]
        ,dbo.vwCOVIDLocations2.Result
    FROM dbo.vwCOVIDLocations2
    WHERE NOT EXISTS (
        SELECT 1 FROM dbo.vwCOVIDDetected2 
        WHERE
            dbo.vwCOVIDDetected2.MRN = dbo.vwCOVIDLocations2.MRN
            AND dbo.vwCOVIDDetected2.[Order Date] = dbo.vwCOVIDLocations2.[Order Date]
            AND dbo.vwCOVIDDetected2.Result = dbo.vwCOVIDLocations2.Result
        )
    GROUP BY dbo.vwCOVIDLocations2.Hospital
        ,dbo.vwCOVIDLocations2.Department
        ,dbo.vwCOVIDLocations2.MRN
        ,dbo.vwCOVIDLocations2.[Patient Name]
        ,dbo.vwCOVIDLocations2.[Order Date]
        ,dbo.vwCOVIDLocations2.Result
    ORDER BY dbo.vwCOVIDLocations2.[Patient Name]