SQL VIEW Execution Timeout Expired

0

I am getting timeout issue for my view. But timeout is not consistent, it occurs only sometimes.

When I run the query I didn't notice any issue, I'm not very sure where to look in this case to fix the timeout issue.

ERROR:

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired

Here is my view:

CREATE VIEW view_student 
AS     
    SELECT *      
    FROM Student p WITH (NOLOCK, FORCESEEK)      
    WHERE trantype = 23      
      AND NOT EXISTS (SELECT 't' FROM attachments m (nolock) 
                      WHERE m.tranid = p.tranid)          
      AND 1 = (CASE 
                  WHEN EXISTS (SELECT 'T' FROM person(NOLOCK) c      
                              WHERE c.type IN (32, 33))      
                       AND EXISTS (SELECT 'T' FROM contract(NOLOCK) co      
                                   WHERE co.TransactionNumber = p.tranid      
                                     AND RIGHT(co.FieldValue, 1) IN (3, 4, 5))      
                     THEN 0    
                     ELSE 1    
               END)    
sql-server
query-optimization
asked on Stack Overflow Nov 13, 2020 by sal • edited Nov 13, 2020 by marc_s

1 Answer

0

suggestion to you until you provide the query plan:

  1. if you can get rid of nolock and other query hints, not a good practice

  2. add a non persisted computed column as RIGHT(co.FieldValue, 1) and make an index on it.

  3. you can change your query to below to simplify the query a little bit , it's not a huge performance improvement though:

CREATE VIEW view_student 
    AS     
        SELECT *      
        FROM Student p WITH (NOLOCK, FORCESEEK)      
        WHERE trantype = 23      
          AND NOT EXISTS (SELECT 't' FROM attachments m (nolock) 
                          WHERE m.tranid = p.tranid)          
          AND NOT EXISTS (SELECT 'T' FROM person(NOLOCK) c      
                                  WHERE c.type IN (32, 33))      
          AND NOT EXISTS (SELECT 'T' FROM contract(NOLOCK) co      
                                       WHERE co.TransactionNumber = p.tranid      
                                         AND RIGHT(co.FieldValue, 1) IN (3, 4, 5))

after looking at the query plan here is my advice:

  1. I see some of the statistics are off and as a result plan shows that you have spill data , so you have to work on those to improve, It might be simply be resolved by updating the stats for your tables , otherwise It not easy to give you an answer here.but try updating your stats on all the tables involved. UPDATE STATISTICS table_or_indexed_view_name
  2. also update Index stats by Rebuilding them to make sure you have the best stats , you can rebuild them.

start from there and paste here the plan again

answered on Stack Overflow Nov 17, 2020 by eshirvana • edited Nov 18, 2020 by eshirvana

User contributions licensed under CC BY-SA 3.0