Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

How do I debug a laggard query in SQL Server?

$
0
0

Hi!

I have been learning how to useCommon Table Expressions (CTEs) and Window Functions in SQL Server. I wrote a pivot query against the Sakila Sample Database that fetches the average amount spent per customer on movie rentals, broken down by month. While the query returns the desired data, it takes a whopping minute and a half to execute!

There must be a way to optimize the query to get it to run significantly faster than that. I would hope that I could get it down to a couple of seconds. Can anyone offer some suggestions on how to debug the query?

Here is the SQL statement:

SQL
SELECTcustomer_id,last_name,first_name,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec,avg_rental_amtFROM(SELECTpayment.amountASamount,customer.customer_idAScustomer_id,customer.last_nameASlast_name,customer....

Viewing all articles
Browse latest Browse all 4871

Trending Articles