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.
| Author |
Topic |
|
jn-at-uk
Starting Member
20 Posts |
Posted - 2005-05-10 : 10:54:30
|
| Hi,The structure of my sp using cursors is farely simple to understand but it takes too long. I need to change it urgently. What do i use instead of cursors?I read numerous articles on cursor performance & they were not good. Im regretting going the cursor route as I am experiencing major performace issues.create table #t1(TMP1 nvarchar(10),TMP2 nvarchar(10),TMP3 nvarchar(100)) declare @engine varchar(50) Declare C000 cursor for SELECT OriginalRef_url AS EngineName FROM Table1 WHERE hostid = @Hostid AND (originalref_url IS NOT NULL) AND (OriginalRef_url IN (SELECT enginename FROM tblengines)) GROUP BY originalref_url OPEN C000 FETCH NEXT From C000 INTO @Engine While (@@Fetch_Status = 0) Begin insert #t1(TMP1,TMP2,TMP3) select a.sessionid, b.[key], c.value from tb_session_ref a, tb_hostdetails b, tb_trackings c, tb_session_pages d where a.sessionid = c.sessionid and a.sessionid = d.sessionid and c.hostid = b.hostid and c.hostid = @HostId and a.[key] = 'q' AND c.originalref_url = @Engine and AbsTimeStamp BETWEEN convert(varchar(12),@StartDate,120) AND convert(varchar(12),@EndDate,120) order by c.originalref_url FETCH NEXT From C000 INTO @Engine End Close C000 Deallocate C000 select TMP2 as [Keyword], count(TMP1) as [Visits], case when isnumeric(TMP3) = 1 then TMP3 ELSE '0.00' END [Revenue], CAST(case when isnumeric(TMP3) = 1 then (cast(TMP3 as numeric)/count(TMPsessionid) * 100/1) else 0 End as numeric) as [% Conv]from #t1 group by TMP2,TMP1, TMP3 drop table #t1 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-05-10 : 11:15:26
|
This is your cursor-less, loop less query....SELECT TMP2 AS [Keyword] , COUNT(TMP1) AS [Visits] , CASE WHEN ISNUMERIC(TMP3) = 1 THEN TMP3 ELSE '0.00' END AS [Revenue] , CAST(CASE WHEN ISNUMERIC(TMP3) = 1 THEN (CAST(TMP3 AS numeric)/COUNT(TMPsessionid) * 100/1) ELSE 0 END AS numeric) AS [% Conv] FROM ( SELECT a.sessionid, b.[key], c.value FROM tb_session_ref a INNER JOIN tb_trackings c ON a.sessionid = c.sessionid INNER JOIN ( SELECT DISTINCT l.OriginalRef_url AS EngineName FROM Table1 l INNER JOIN tblengines r ON l.OriginalRef_url = r.enginename WHERE l.hostid = @Hostid) As ReplaceCursor ON c.originalref_url = ReplaceCursor.EngineName INNER JOIN tb_session_pages d ON a.sessionid = d.sessionid INNER JOIN tb_hostdetails b ON c.hostid = b.hostid WHERE c.hostid = @HostId AND a.[key] = 'q' AND AbsTimeStamp >= convert(varchar(12),@StartDate,120) AND AbsTimeStamp <= convert(varchar(12),@EndDate,120) ) AS XXXGROUP BY TMP2,TMP1, TMP3 Does it make any sense to me?NoBut it is, I beleieve, exactly what you haveBrett8-) |
 |
|
|
jn-at-uk
Starting Member
20 Posts |
Posted - 2005-05-10 : 11:53:36
|
| hi brett,thanks for that. i have one query. is it ok, that i create the #t1. also i was not sure about )As XXX.wrote the sp the #temp table way.was that what u explained. it does work but im not sure if u used temptables to speedup the processcreate table #t1(TMP1 nvarchar(10),TMP2 nvarchar(10),TMP3 nvarchar(100))insert #t1(TMP1,TMP2,TMP3) (SELECT a.sessionid, a.[key], a.value FROM tb_session_ref a INNER JOIN tb_trackings c ON a.sessionid = c.sessionid INNER JOIN ( SELECT DISTINCT l.OriginalRef_url AS EngineName FROM tb_trackings l INNER JOIN tblengines r ON l.OriginalRef_url = r.enginename WHERE l.hostid = @hostId) As ReplaceCursor ON c.originalref_url = ReplaceCursor.EngineName INNER JOIN tb_session_pages d ON a.sessionid = d.sessionid INNER JOIN tb_hostdetails b ON c.hostid = b.hostid WHERE c.hostid = @HostId AND a.[key] = 'q' ) select TMP1 as [Keyword], count(TMP2) as [Visits], case when isnumeric(TMP3) = 1 then TMP3 ELSE '0.00' END [Revenue], CAST(case when isnumeric(TMP3) = 1 then (cast(TMP3 as numeric)/count(TMP1) * 100/1) else 0 End as numeric) as [% Conv]from #t1 group by TMP1,TMP2,TMP3 |
 |
|
|
nathans
Aged Yak Warrior
938 Posts |
Posted - 2005-05-10 : 14:54:05
|
| a good overview:[url]http://www.sqljunkies.com/How%20To/0C726943-1F5D-430F-A5A5-617089E7B84E.scuk[/url] |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-05-10 : 15:00:17
|
| Did you try the whole thing in one shot?) AS XXX is a derived table that allows you to combine your temp table solutuin...which is kinda what a derived table does...I believe with less overhead...Brett8-) |
 |
|
|
|
|
|
|
|