Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Statement in SQL procedure should affect Performance?

I am using SQL Server procedures and I have a habit of using of Print statements in the stored procedures to differentiate the code of procedure.

I have almost 200-250 procedures in my DB. Should print statement affect the performance? I am working on multi-user Windows application.

like image 869
KuldipMCA Avatar asked Sep 11 '10 06:09

KuldipMCA


People also ask

Do SQL prints slow down?

A handful of PRINT statements will have a negligible effect on performance - PRINT s in loops that are executed many thousands of times, however, may well cause performance issues.

What affects SQL query performance?

Table size: If your query hits one or more tables with millions of rows or more, it could affect performance. Joins: If your query joins two tables in a way that substantially increases the row count of the result set, your query is likely to be slow. There's an example of this in the subqueries lesson.

Is it possible to print out a message from a function procedure?

Stored procedures can't "show messages". Then can return either an OUTPUT parameter OR a record set. It's up to your application to determine what to do with the results. That said, you can "print" a message which is similar to simply selecting some text: see stackoverflow.com/questions/6912102/…


2 Answers

I found when running the below on my desktop that commenting out the print knocked about 15 seconds off the execution time meaning the average impact was 15µs in my simple test. RAISERROR WITH NOWAIT added an average of just over double that.

DECLARE @date DATETIME2

DECLARE 
  @count INT

SET @count = 1

SET @date = SYSUTCDATETIME()

WHILE @count < 1000000
BEGIN
--RAISERROR ('%d',0,1, @count) WITH NOWAIT
--PRINT @count
  SET @count = @count + 1
END

SELECT DATEDIFF(MICROSECOND, @date, SYSUTCDATETIME()) / 1000000. 
like image 91
Martin Smith Avatar answered Sep 23 '22 09:09

Martin Smith


A handful of PRINT statements will have a negligible effect on performance - PRINTs in loops that are executed many thousands of times, however, may well cause performance issues.

It's unlikely that if you're experiencing performance problems with your queries that PRINT is the culprit - however, if in doubt, try some experiments!

like image 42
Will A Avatar answered Sep 25 '22 09:09

Will A