|
This query takes more than 1 hour
(comments are locked)
|
|
Without the execution plan, it's hard to know what might be going wrong, but a couple of things jump right out. First off, you're joining views to views. This can lead to major performance bottlenecks. Sometimes SQL Server can be very smart about how it deals with views, breaking down the view query itself and identifying which parts of it are actually applicable to the query you're working on. Other times, especially as things get more complex, as this query is, you'll simply see SQL Server treat the entire view as a unit and process every table referenced within the view, whether it's applicable to the query you're running against it or not. Execution plans will help here, but as a general rule, I don't recommend joining views to views if you can avoid it. The next thing that jumps out are several instances of runnning functions against columns:
This will lead to scans. You can't get good index seeks out of this code, regardless of how good your indexes may be. You need to either drop the functions, or clean up the data in the tables so that these functions are not needed. With the functions in place, you'll get scans and slow performance, no matter what else you do. That's about all I can see or suggest without a look at the execution plans, your table structures, the views, indexes and some sample data. +1 So easy to do as a beginner though or even later on in your experience. I've done it so many time to run quick queries which then snowballed.
Feb 15 '10 at 10:34 AM
Ian Roke
(comments are locked)
|
|
I reformatted your query so it was a bit more readable, as Ian mentioned, looking at the execution plan may help you find out why the query is performing slowly. One of the things that I noticed that would definitely have an impact is the number of functions and concatenations that are being used in both join criteria and the where clause. Using functions, forces the query to table scan since it must apply the function to every single row in the table, so you loose the benefit of an index seek. Other than that, after looking at the execution plan, make sure that you have the appropriate indexes on the the tables, at least covering the join and filtering criteria of the query +1 Nicely formatted Jay! ;-)
Feb 15 '10 at 10:33 AM
Ian Roke
(comments are locked)
|
|
I know it's an old post but the first thing I would do is read Grant's answer... especially the part about have forumlas in the columns in the WHERE clause and in the columns in the ON clause. The make any chance of high performance virtually non existant because they force scans instead of seeks just like Grant said.
(comments are locked)
|
|
Looking at the code I can see a lot of views joined together in this query. You really need to run the Profiler on this code and also the Query Analyzer to truely find out what is running slow. Possibly you are missing indexes on various tables referenced by the views that could speed things up. The Query Analyzer will suggest what you could do far better than the naked eye due to us not being able to see the full data model, amount of data and so on.
(comments are locked)
|


Don't forget to indent code with four spaces to allow the system to format it correctly. Makes it easier for my poor eyes to read too! :-)
formatted the code, but still not nice - maybe Jay could add his nicer version.....
Kev, I was originally going to try and edit the OP and update the code, but I couldn't seee how to edit the origianl post, which is why I posted the code in an answer - +1 for Red Gates SQL Refactor...
Jay : ah sorry - you need 2000 rep to edit other peoples posts. I could lift it for you if you want?
Sure, if you don't mind lifting it, I can help out editing occasionally.