Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 index view on query with case statement?

Author  Topic 

ldc0618
Starting Member

3 Posts

Posted - 2010-05-13 : 12:48:46
Hi,

I am having a major performance issue with the below view. The view has a case statement that determines if the current record has children. It takes about 20 seconds to run and I have about 60k records in that table.

My question is; Can I create an indexed view that could dramatically increase performance or should I go another route? I don't have any experience with indexed view, but I am wondering if that is the best route.


SELECT TabID, TabOrder, PortalID, TabName, IsVisible, ParentId, [Level], CASE WHEN LEFT(LOWER(T .IconFile), 6) = 'fileid' THEN
(SELECT Folder + FileName
FROM dbo.Files
WHERE 'fileid=' + CONVERT(varchar, dbo.Files.FileID) = T .IconFile) ELSE T .IconFile END AS IconFile, DisableLink, Title, Description, KeyWords, IsDeleted,
SkinSrc, ContainerSrc, TabPath, StartDate, EndDate, Url, CASE WHEN EXISTS
(SELECT 1
FROM dbo.Tabs T2
WHERE T2.ParentId = T .TabId) THEN 'true' ELSE 'false' END AS HasChildren, RefreshInterval, PageHeadText, IsSecure
FROM dbo.Tabs AS T

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-13 : 12:51:49
can you try replacing the subqueries with joins?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ldc0618
Starting Member

3 Posts

Posted - 2010-05-13 : 13:11:37
quote:
Originally posted by visakh16

can you try replacing the subqueries with joins?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Tried that and it actually took longer, more like 37 seconds:


SELECT T.TabID, T.TabOrder, T.PortalID, T.TabName, T.IsVisible, T.ParentId, T.[Level], T.DisableLink, T.Title, T.Description, T.KeyWords,
T.IsDeleted, T.SkinSrc, T.ContainerSrc, T.TabPath, T.StartDate, T.EndDate, T.Url, T.RefreshInterval, T.PageHeadText, T.IsSecure, dbo.Tabs.ParentId AS Expr1
FROM dbo.Tabs AS T LEFT OUTER JOIN
dbo.Tabs ON T.TabID = dbo.Tabs.ParentId


Thanks!
Go to Top of Page

Asken
Starting Member

38 Posts

Posted - 2010-05-14 : 02:23:30
It's definately the view that's the problem. You can either try to create indexes on the tables in the view to improve performance or you can create an indexed view. Run the query including the execution plan to find the issues.


Reporting & Analysis Specialist
Helping others helps me get better...
Go to Top of Page

ldc0618
Starting Member

3 Posts

Posted - 2010-05-14 : 16:44:26
ok, so I decided to just add the haschildren field to my table and then update it as needed. but I am now having a problem with mu update statement. Shouldn't this work?

UPDATE tabs SET haschildren = CASE WHEN EXISTS
(SELECT 1
FROM dbo.Tabs T2
WHERE T2.ParentId = TabId)
THEN 1
ELSE 0 END
GO

I don't get any error, but it never changes haschildren to 1

Thanks in advance!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-05-15 : 03:59:01
quote:
Originally posted by ldc0618

ok, so I decided to just add the haschildren field to my table and then update it as needed. but I am now having a problem with mu update statement. Shouldn't this work?

UPDATE tabs SET haschildren = CASE WHEN EXISTS
(SELECT 1
FROM dbo.Tabs T2
WHERE T2.ParentId = TabId)
THEN 1
ELSE 0 END
GO

I don't get any error, but it never changes haschildren to 1

Thanks in advance!



it should be


UPDATE t1
SET t1.haschildren = 1
FROM tabs t1
CROSS APPLY (SELECT COUNT(1) AS Cnt
FROM tabs
WHERE ParentId = t1.TabId)t2
WHERE t2.Cnt > 0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -