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)
 Query

Author  Topic 

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2011-11-06 : 21:47:46
The below view had a performance issue.Please help how to improve performance.
The table student had 150 million records with index on stu_date



create view dbo.vw_student_prct as

select TOP(100) percent a.totalstudents,
b.totalscience students,
cast(cast((case when b.totalsciencesstudents is null then 0 else b.totalsciencestudents end)as decimal(8,2))
/ (case when a.totalstudents is null then 1 else a.totalstudents end )* 100 as decimal(8,2)) as scienceprnct,

--
--
--
b.st_date

from
(
select st_date,count(student_id) as totalstudents
from student as studnt1
where (ds_name like '%Doere%')
group by st_date) as a inner join
(
select st_date,count(student_id) as totalsciencestudents
from student as studnt
where (ss_name like '%physical%')
group by st_date) as b on a.student_date = b.student_date inner join
(
select st_date,count(student_id) as totalMathstudents
from student as studnt2
where (ss_name like '%math%')
group by st_date) as c on c.student_date = b.student_date inner join
(
select st_date,count(student_id) as totalenglishstudents
from student as studnt1
where (ts_name like '%eng%')
group by st_date) as d on b.student_date = d.student_date inner join

(
select st_date,count(student_id) as totalspecialstudystudents
from student as studnt1
where (ts_name like '%speci;%') OR (ts_name like '%speci;%') OR (ts_name like '%speci') OR (ts_name like '%speciA%')
OR (ts_name like '%speciD%') OR (ts_name like '%speciTo%') OR (ts_name like '%speciK%')
group by st_date) as e on b.student_date = b.student_date inner join

(
select st_date,count(student_id) as totalkainistudents
from student as studnt3
where (Ks_name like '%kaini%') and (Lstatus ='Y'AND (kstatus ='Y')
OR (ss_name like '%speci;%') and (lstatus = 'Y') AND (kstatus ='Y')
OR (ks_name like '%seci') OR (ts_name like '%VteciA%')
group by st_date) as f on b.student_date = f.student_date
)
order by b.st_date


Lewie
Starting Member

42 Posts

Posted - 2011-11-07 : 01:38:55
Keep an eye on your

Ks_name like '%kaini%') and (Lstatus ='Y'AND (kstatus ='Y')
OR (ss_name like '%speci;%') and (lstatus = 'Y') AND (kstatus ='Y')
OR (ks_name like '%seci') OR (ts_name like '%VteciA%')

will the AND be performed first or the OR's. Perhaps add a few for brackets.
However, this will not solve your problem.
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2011-11-07 : 08:18:17
Thanks !

I will add the parenthesis.

Is there any suggestion to improve the performance..
Go to Top of Page

Lewie
Starting Member

42 Posts

Posted - 2011-11-07 : 08:26:28
Well you can try a wild shot at splitting each part and putting them into separate temp tables. Add an index to the temp tables on the st_date and include the other field. Then do a select with all the temp table joining the st_date
Go to Top of Page

sqlfresher2k7
Aged Yak Warrior

623 Posts

Posted - 2011-11-07 : 09:33:10
Thanks i will try your suggestion
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-11-07 : 09:49:48
Duplicate post. No replies to this thread please. Direct replies to:[url]http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=167581[/url]

--
Gail Shaw
SQL Server MVP
Go to Top of Page
   

- Advertisement -