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)
suggestion to you until you provide the query plan:
if you can get rid of nolock
and other query hints, not a good practice
add a non persisted computed column as RIGHT(co.FieldValue, 1)
and make an index on it.
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:
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
start from there and paste here the plan again
User contributions licensed under CC BY-SA 3.0