0

I have a query with 4 Select Queries joined by Union All to return one resultset, they are pretty complex select queries but no joins are involved, they actually select from 2 different views. I am just looking into optimising it but not really sure where I should start. The "Actual Execution Plan" is saying the "Estimated subtree cost" is 2.12357, I always assumed if it was over 1 then it's a pretty slow query? is that a correct assumption? I have also checked the entire plan and there are no table scans, only clustered Index Scans and Clustered Index Seeks which again I am assuming are ok? There is however a lot of Hash Matches. Anyway this query will be getting ran a lot of times hence why i'd like it optimised as best as possible. Hope it makes some sort of sense.

Ok, the SQL looks like this

CREATE PROCEDURE [dbo].[CompareFiles] -- Add the parameters for the stored procedure here
@VersionID int, 
@SyncRequestID int,     
@RegionID int, 
@LanguageID int 
AS
BEGIN

SELECT 'Delete' AS Action, Type, SUBSTRING(FullPath,2,LEN(FullPath)-1) AS FullPath, FileNameOnServer, FileSize FROM View1
WHERE NOT EXISTS(SELECT 1 FROM View2 where View2.FullPath = View1.FullPath 
AND View2.VersionId = @VersionID
AND View2.RegionID = @RegionID 
AND View2.LanguageID = @LanguageID
)
AND View1.SyncRequestID = @SyncRequestID

UNION ALL 

SELECT 'Add' AS Action, Type, SUBSTRING(FullPath,2,LEN(FullPath)-1) AS FullPath, FileNameOnServer,FileSize FROM View2  
WHERE NOT EXISTS(SELECT 1 FROM View1 where View1.FullPath = View2.FullPath AND View1.SyncRequestID = @SyncRequestID)
AND 
View2.VersionId = @VersionID
AND View2.RegionID = @RegionID
AND View2.LanguageID = @LanguageID

UNION ALL 

SELECT 'Delete' AS Action, Type, SUBSTRING(FullPath,2,LEN(FullPath)-1) AS FullPath, FileNameOnServer, FileSize FROM View2 
WHERE EXISTS
(
    SELECT 1 FROM View2 fd2 
    WHERE 
        View2.FullPath = fd2.FullPath AND fd2.VersionID = @VersionID
AND 
    View2.FileNameOnServer <> fd2.FileNameOnServer 
AND 
    fd2.RegionID = @RegionID
AND 
    fd2.LanguageID = @LanguageID 
)
AND 
    View2.VersionId = (SELECT VersionID FROM SyncRequest WHERE SyncRequestID = @SyncRequestID)
AND 
    View2.RegionID = @RegionID
AND 
    View2.LanguageID = @LanguageID

UNION ALL

SELECT 'Add' AS Action, Type, SUBSTRING(FullPath,2,LEN(FullPath)-1) AS FullPath, FileNameOnServer, FileSize FROM View2 
WHERE EXISTS
(
SELECT 1 FROM View2 fd2 
WHERE 
    View2.FullPath = fd2.FullPath AND fd2.VersionID = (SELECT VersionID FROM SyncRequest WHERE SyncRequestID = @SyncRequestID)
AND 
    View2.FileNameOnServer <> fd2.FileNameOnServer 
AND 
    fd2.RegionID = @RegionID
AND 
    fd2.LanguageID = @LanguageID 
)
AND 
    View2.VersionId = @VersionID
AND 
    View2.RegionID = @RegionID
AND 
    View2.LanguageID = @LanguageID

END

View 1 Definition

SELECT DISTINCT fqp.Path + '/' + ISNULL(f.Name, '') + ISNULL(f.Extension, '') AS FullPath, fl.LanguageID, fr.RegionID, f.FileID, dbo.CIT_APD_Versions.ApID, f.FileNameOnServer, dbo.Versions.VersionID, (CASE WHEN FileNameOnServer IS NULL THEN 'Folder' ELSE 'File' END) AS Type, f.FileSize
FROM dbo.FileRegions AS fr 
RIGHT OUTER JOIN dbo.Files AS f 
    INNER JOIN dbo.FilePaths AS fp ON fp.FileID = f.FileID ON fr.FileID = f.FileID 
    LEFT OUTER JOIN dbo.FileLanguages AS fl ON fl.FileID = f.FileID 
    LEFT OUTER JOIN dbo.RoleFiles AS fro ON fro.FileID = f.FileID 
    RIGHT OUTER JOIN dbo.Versions 
        INNER JOIN dbo.View3 AS fqp ON dbo.VersionID = fqp.VersionID ON fqp.PathID = fp.PathID

View 2 Definition

SELECT     '//' + dbo.CIT_APD_SyncRequestFiles.FullPath AS FullPath, NULL AS LanguageID, NULL AS RegionID, NULL AS FileID, NULL AS ApID, 
                  dbo.SyncRequest.VersionID, '' AS FileNameOnServer, '' AS Type, NULL AS FileSize, dbo.SyncRequestFiles.SyncRequestID
FROM dbo.SyncRequestFiles 
INNER JOIN dbo.CIT_APD_SyncRequest ON dbo.SyncRequestFiles.SyncRequestID = dbo.SyncRequest.SyncRequestID

View 3 Definition

WITH Parent AS (SELECT PathID, ParentPathID, VersionID, CONVERT(varchar(128), Path) AS Path
FROM dbo.Paths AS ParentPaths
WHERE (ParentPathID = 0)
UNION ALL
SELECT ChildPaths.PathID, ChildPaths.ParentPathID, ChildPaths.VersionID, CONVERT(varchar(128), Parent.Path + '/' + ChildPaths.Path) AS Path
FROM dbo.Paths AS ChildPaths 
INNER JOIN Parent ON Parent.PathID = ChildPaths.ParentPathID)

SELECT PathID, ParentPathID, VersionID, Path
FROM Parent

Thanks

6
  • 2
    not enough information to answer... Nov 9, 2010 at 1:05
  • Presumably your views contain joins. A view without joins has little value (it's a table!) except perhaps in certain specific circumstances such as hiding columns (authorisation) Nov 9, 2010 at 1:10
  • Apologies, you're right, i'll clean up the SQL and post it asap
    – Coesy
    Nov 9, 2010 at 1:25
  • That comment's a little broad, Mitch. A view that horizontally partitions your data is highly valuable for performance reasons. Likewise, I pivot and unpivot data in views all the time for use in odbc linked spreadsheets.
    – DataWriter
    Nov 9, 2010 at 1:48
  • @Coesy: Great, but where are the view definitions?
    – OMG Ponies
    Nov 9, 2010 at 2:14

3 Answers 3

2

...they are pretty complex select queries but no joins are involved, they actually select from 2 different views.

But are there joins in the views? And do the views have any tables in common?

Without seeing the query, and the queries the views stand in for, there's very little anyone can provide that will be of value.

1

"Estimated subtree cost" is 2.12357, I always assumed if it was over 1 then it's a pretty slow query

The subtree cost cannot be used independently to evaluate performance. It is intended for use to compare different queries that perform the same task. So, it is not reliable to assume that a high cost indicates a poor performing query.

You have to answer the question "Is the query returning results in an acceptable amount of time?"

With all that said, and to loosen up on the rhetoric, the higher subtree cost might support your thought that maybe the query can be improved.

there are no table scans, only clustered Index Scans

What may be a bigger opportunity for improving the query, is to look at why there are clustered index scans. The clustered index scan is equivalent to the table scan. The entire clustered index, which includes the table data, is being scanned. Must all rows in the table be evaluated, or is this an indication of a performance-improvement opportunity?

0

Run the query through the index tuning wizard and that could tell you what indexes could help.

Really it'd be better to analyze and identify sql improvements, but you didn't post the sql, so we can't help you with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.