Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server view takes a long time to alter but query itself finishes quickly?

I am trying to alter an existing view in my SQL Server database. When I run the query by itself it finishes in about 4 seconds. When I run the alter statement with the same query it runs and never finishes (waited 15 minutes before stopping it).

I do not have any indexes on the view I am trying to alter. Any ideas what would cause something like this to happen? Is there something I should be doing differently to speed things up?

like image 553
Abe Miessler Avatar asked Jun 03 '11 16:06

Abe Miessler


People also ask

Why is SQL view so slow?

When SQL Server processes a SELECT from a view, it evaluates the code in the view BEFORE it deals with the WHERE clause or any join in the outer query. With more tables joined, it will be slow compared to a SELECT from base tables with the same results.

How can I speed up SQL query execution?

Reduce nested views to reduce lags This nesting causes too many data returns for every single query, which either makes the database crawl, or completely give up and give no returns. Minimizing nesting is a simple way to make your SQl query efficient and significantly improve speeds.


1 Answers

Make sure there's no contention for that view. If something else is accessing it, or if there's a spid somewhere that's idle but has a connection to it, you may be blocked from the ALTER statement.

A simple sp_who2 active during the ALTER should give you the culprit.

like image 54
JNK Avatar answered Nov 08 '22 11:11

JNK