Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

View / stored procedure performance help needed!

Status
Not open for further replies.

emblewembl

Programmer
May 16, 2002
171
GB
Hi, I am having to do some calculations on some data in an sql database and I haven't had to do anything like this before so I am not sure that I am doing things the best way - in fact I'm sure I'm not! I'm hoping someone out there can give me some advice so that I can improve.

I need to analyse a table of horses, and compare their age to their racing performance. This first view gives me each runners age, their finishing position and a 1 in the wins column if it's a win.
Code:
ALTER       view ivFirst

as

select hp.horseid, hp.distancetext, 
	(case when hp.officialfinish = 1 then 1 else 0 end) as wins,
	DATEDIFF(MONTH, h.foalingDate, hp.date) AS ageInMonthsOfRunner

from h_past hp, horses h, ivr i

where hp.distancetext in (select distinct(distance) from ivr)
and h.horseid = hp.horseid
and hp.date >= dateadd(year, -2, getdate())
group by hp.horseid, hp.date, hp.distancetext, hp.officialfinish, h.foalingdate

This second view gives me a count of the total runs and wins at a given distance from all of the data being analysed.

Code:
ALTER      view ivSecond

as
	
select 	d.distancetext,
	count(d.distancetext) as runs,
	sum(d.wins) as wins
	
from ivFirst d
	
group by d.distancetext


The third view then calculates an overall performance indicator based on the wins at a given distance at a particular age compared to overall wins and runs at the given distance.


Code:
ALTER    view ivThird

as

select 	i.entryid,
	((convert(decimal, sum(d.wins), 2)/convert(decimal,t.wins,2)) /
	(convert(decimal,(case when count(d.ageInMonthsOfRunner) is not null then count(d.ageInMonthsOfRunner) else 0 end),2)/convert(decimal,t.runs,2))) as ivAgeDistanceGroup
		
from ivr i
	inner join ivFirst d
	on i.distance = d.distanceText
	and i.ageInMonths = d.ageInMonthsOfRunner

	inner join ivSecond t
	on i.distance = t.distancetext

group by i.entryid, t.runs, t.wins

This works but is so slow, and I am certain that this is a bad way of doing this!! Can anyone help?

i love chocolate
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top