SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select statement of view stays 5 hours to return results......


Select statement of view stays 5 hours to return results......

Author
Message
JMSM
JMSM
SSChasing Mays
SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)

Group: General Forum Members
Points: 614 Visits: 978
Hello,

We've got created the following view (1.), in a database that have no more then 350Mb w00t but the result of the select statement (2.) stays among 5 hours to return results Crazy

I'm completely lost and i really dont understand what part of the code i must change Blush
Sorry!!! Can you help me or can you give me any idea so that i can change the code of this "crazy view".....

Happy new year
Thanks and regards,
JMSM ;-)

1.
------------- Create View Code ------------------
use [DBName]
GO
create view [dbo].[VwNAME] as
select [ID]
,[Login]
,FLPExp
,FLKHw
,PUACnt
,FLWRArt
,FLWRBPst
,FLWDnlds
,case when FLPExp = 1 and FLKHw = 1 and PUACnt > 1 then 100
when FLPExp = 1 and FLKHw = 1 and FLWRArt = 1 then 75
when FLPExp = 1 and FLKHw = 1 and FLWDnlds = 1 then 50
when FLPExp = 1 and FLKHw = 1 then 25
else 0
end 'PercentPrf'
from (
select
a.id as 'ID',
a.ACCName as 'Login',
case when count(distinct b.title)>0 then 1 else 0 end FLPExp,
case when count(distinct f.idknowhow)>0 then 1 else 0 end FLKHw,
count(distinct d.id)+count(distinct e.IDArtcl) PUACnt,
case when (count(distinct g.id) + count(distinct h.id))>0 then 1 else 0 end FLWRArt,
case when count(distinct i.id)>0 then 1 else 0 end FLWRBPst,
case when count(distinct j.CntDnlds)>0 then 1 else 0 end FLWDnlds
from Athr a
left join PstExp b on a.id = b.idAthr
left join Artcl d
on a.id = d.idAthrSubMtr
and d.status = 2 -- Aproved
left join (select e1.id, e2.idAthr, e2.IDArtcl
from Artcl e1,
ArtclcoAthr e2
where e1.id = e2.IDArtcl
and e1.status = 2) e -- CO Artcls
on a.id = e.idAthr
left join Athrknowhow f on a.id = f.idAthr
left join Artcl g -- Artcls less 30 days
on a.id = g.idAthrSubMtr
and datediff(d, g.PblDte, getdate()) < 30
and g.Status = 2
left join (select h1.id, h2.idAthr
from Artcl h1,
ArtclcoAthr h2
where h1.id = h2.IDArtcl
and datediff(d, h1.PblDte, getdate()) < 30
and h1.status = 2) h -- CO Artcls less 30 days
on a.id = h.idAthr
left join UsgWkiBlg i -- Posts less then 30 days
on a.id = i.idAthr
and i.ShPntTypSWeb = 1
and datediff(d, i.LstMdfation, getdate()) < 30
left join UsgeAthrArtcl j -- downloads
on a.id = j.idAthr
where a.locked = 0
group by a.id, a.ACCName ) a
go

---------------- End View Code ------------------

2.
---------------- Select Statement ---------------
SELECT [ID]
,[Login]
,FLPExp
,FLKHw
,PUACnt
,FLWRArt
,FLWRBPst
,FLWDnlds
,[PercentPrf]
FROM [DBName]. [dbo].[VwNAME]
GO
------------ End Select Statement ---------------
Koen Verbeeck
Koen Verbeeck
SSC-Dedicated
SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)SSC-Dedicated (34K reputation)

Group: General Forum Members
Points: 34515 Visits: 13270
It's hard to make something up from your create view statement as it doesn't have a proper lay-out, which makes it hard to read.

At first glance I see you have a lot of joins, including outer joins, with functions in the where clause. You should try to avoid functions in the where clause, as indexes will probably not be used. Also, the many COUNT DISTINCT aggregates can also slow things down.

If possible and necessary, replace the "on-the-fly" calculated columns from the where clauses with real persisted computed columns.
You should probably also check out if there are any indexes and if there are, if they are useful.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
JMSM
JMSM
SSChasing Mays
SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)

Group: General Forum Members
Points: 614 Visits: 978
Thanks a lot Koen.

I'll do the right checks and i'll tell you the solution.

Once more thanks.
Regards,
JMSM ;-)
LutzM
LutzM
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12931 Visits: 13559
There are several approaches you can use:
Most important:
instead of applying a function to your columns rewrite it as SARGable arguments:
datediff(d, i.LstMdfation, getdate()) < 30
-- would be
i.LstMdfation>dateadd(dd, -30, getdate())


Another option that usually helps is preaggregation.
Find the table that would be reduced the most by your where conditions and populate that table as an indexed temp table and use this table in your query.

Furthermore, you're calling Artcl at least four times. Maybe the query itself can be rewritten to be much more efficient.

And finally, there might be a missing index issue, too. Check the execution plan for best index usage.



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Grant Fritchey
Grant Fritchey
SSC Guru
SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)SSC Guru (55K reputation)

Group: General Forum Members
Points: 55229 Visits: 32797
I agree with what everyone else has said. I think they've caught the obvious stuff. Now, to keep going, can you post the execution plan from the query, preferably after you fix the functions that are preventing index use. That can help to figure out if you have good indexes in place and they're getting used properly.

----------------------------------------------------
The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood...
Theodore Roosevelt

The Scary DBA
Author of: SQL Server Query Performance Tuning and SQL Server Execution Plans
Product Evangelist for Red Gate Software
JMSM
JMSM
SSChasing Mays
SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)

Group: General Forum Members
Points: 614 Visits: 978
Thanks a lot LutzM.
I'll change the function as u tell me.
Regards,
JMSM ;-)
LutzM
LutzM
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12931 Visits: 13559
JMSM (1/9/2011)
Thanks a lot LutzM.
I'll change the function as u tell me.
Regards,
JMSM ;-)


The function issue is not the only problem (might not even be the most relevant one).
So, please follow Grants advice and post the actual execution plan.

And don't worry too much about Celko's post... If you'd take the time trying to read and understand what his real intention is, you'd probably have rewritten your code by then anyway... ;-)

Beside he really knows SQL stuff (at least what rumour tells) he's got that "special" online personality. The bottom line: He doesn't like the way you name your columns, tables, and views. Doesn't change performance at all, but would help readability and maintainability, I admitt. But that's not the major concern here, I guess.... ;-)

He's right about the DISTINCT issue though...



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
JMSM
JMSM
SSChasing Mays
SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)SSChasing Mays (614 reputation)

Group: General Forum Members
Points: 614 Visits: 978
Hello again,

I've attached the execution plan and the code of both views, the oldest and the new one.
I've change yet the column names, and the tablename on both views.

Thanks and regards,
JMSM ;-)
Attachments
AuthorProfilePerc.NEW.txt (27 views, 2.00 KB)
AuthorProfilePerc.OLD.txt (16 views, 2.00 KB)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search