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 2005 Forums
 Transact-SQL (2005)
 Need help in stored procedure

Author  Topic 

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-11-16 : 08:35:36
Hi Team,
I wrote one stored procedure as per my .net application requirement. when i run the application it works fine if the table has small amount of data(i.e. 50000 records). If the table has 20 million records, i am getting time out errors.

The zipcode column in table has unquie clustered index.

I want to know which line of stored procedure taking more time to execute. Just go thorugh my procedure.which u feel generally takes more time.


CREATE Procedure [dbo].[Tbl_SP_GetVotercationData]
(
@Race Varchar(50),
@Zcode Int
)
As
Begin
Declare @colnames varchar(max),
@result varchar(max),
@delqry varchar(500)

set @delqry = 'Drop table Tbl_TempVoterData'

IF OBJECT_ID ('MyDB.dbo.Tbl_TempVoterData','U') IS NOT NULL
Exec(@delqry)

If(@Race) is not null
Begin
select @colnames = stuff((select ', '+ column_name from information_schema.columns where table_name = 'Tbl_VoterData'
and column_name like @Race +'%' and column_name <> @Race +'Income' and column_name <> 'Zipcode' for xml path('')),1,1,'')

If (@colnames) is not null
exec('select Zipcode, '+@colnames+' into Tbl_TempVoterData from Tbl_VoterData where Zipcode = ' + @Zcode)
End
End


developer :)

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-11-16 : 08:52:07


Why in the WORLD would you need to do this with dynamic sql? Follow the HOW TO ASK link in my signature and provide the information it asks for, and i am sure we can come up with a good answer for you.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2009-11-16 : 09:42:33
The thing is, 'lines' in SQL don't take time, queries take time, so it looks like you have only 2 possibilities there (ie. because there are only 2 queries).

I would guess it is the XML that is taking too long. You may be able to test this by taking the XML out of the query and then see how long it takes. Another possibility is how many rows are being inserted, you mention something about 20 million records though I am assuming that is from your source table, but that is a little unclear. If you are inserting a huge amount of records that may be a speed issue if those inserts are being logged.

This query should tell you which query is taking the longest, but it your situation, I don't think it will narrow down the problem too much for you. You run this query after you have executed your Stored Proc:

SELECT TOP 1000
a.last_execution_time,
cast(last_elapsed_time/1000000.0 as decimal(10, 3)) Last_Elasped_Seconds,
Object_Name(ObjectID) Object_Name,
DB_Name(dbID) Database_Name,
SUBSTRING(b.text, (a.statement_start_offset/2) + 1,
((CASE statement_end_offset WHEN -1 THEN DATALENGTH(b.text) ELSE a.statement_end_offset END
- a.statement_start_offset)/2) + 1) AS statement_text,
total_worker_time as CPU_time,
*
FROM
sys.dm_exec_query_stats a CROSS APPLY
sys.dm_exec_sql_text (a.sql_handle) AS b
WHERE
last_execution_time > '2009-11-16 0:00' -- Filter by Time
ORDER BY
a.last_execution_time desc
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-11-16 : 10:17:22
How many voters are in a zip code.....10's of thousands?

And you want to return all of that data to your .NET application

That's your problem

In ANY case

LOSE THE DYNAMIC SQL



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2009-11-16 : 10:29:35
quote:

In ANY case

LOSE THE DYNAMIC SQL



... or at least explain why you are using it. Or at least comment the Stored Proc. I don't know if it is just me but I read your code at least 5 times and I still scratch my head.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2009-11-16 : 10:44:20
I think I see now, why you are using Dynamic SQL and with the XML, I think you are saying return all columns except ... I wasn't familiar with that approach.

So maybe an acceptable use of dynamic SQL? But I still think there should have been something clarifying that in the Stored Proc or in the Post.

So I take back that part about the XML being the slowness problem, it's only called once in the Stored Proc, so not a problem. Originally, I thought the XML was going to be called once per record.

Are you sure it is the Stored Proc and not the web page that is taking so long? Did you time the WebPage call or the Stored Proc call on its own?
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-11-16 : 14:07:47
return all columns except? so, run the query set based, non dynamic, as a subquery, and just select the columns you want to send back to the .net application.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page

denis_the_thief
Aged Yak Warrior

596 Posts

Posted - 2009-11-16 : 14:48:29
quote:
Originally posted by DonAtWork

return all columns except? so, run the query set based, non dynamic, as a subquery, and just select the columns you want to send back to the .net application.




I'm sure that's an option. But maybe the Poster's game plan is to write it in such a way that it won't have to be modified if additional columns are added to the table. Not saying it's good or bad.
Go to Top of Page

DonAtWork
Master Smack Fu Yak Hacker

2167 Posts

Posted - 2009-11-17 : 07:30:18
Since he wont supply all the necessary data, that is my best guess.

http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx
How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

For ultra basic questions, follow these links.
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp
Go to Top of Page
   

- Advertisement -