10

I'm weighing the potential performance impact of using one of three different methods of returning a single, scalar value from a stored procedure to my C# routine. Can anyone tell me which of these is "faster" and, most importantly, why?

Method 1:

CREATE PROCEDURE GetClientId 
    @DealerCode varchar(10)
AS
BEGIN
    SET NOCOUNT ON
    SELECT ClientId
        FROM Client
        WHERE ClientCode = @DealerCode
END
-- this returns null if nothing is found, 
-- otherwise it returns ClientId in a ResultSet

Method 2:

CREATE PROCEDURE GetClientId 
    @DealerCode varchar(10),
    @ClientValue int out
AS
BEGIN
    SET NOCOUNT ON
    set @ClientValue = -1
    set @ClientValue = (SELECT ClientId
        FROM Client
        WHERE ClientCode = @DealerCode)
END
-- this returns -1 for ClientValue if nothing is found,
-- otherwise it returns ClientId
-- the value for ClientValue is a scalar value and not a ResultSet

Method 3:

CREATE PROCEDURE GetClientId 
    @DealerCode varchar(10)
AS
BEGIN
    SET NOCOUNT ON
    declare @ClientValue int
    set @ClientValue = 
        (SELECT ClientId FROM Client WHERE ClientCode = @DealerCode)
    if @ClientValue is null or @ClientValue = 0
        return -1
    else
        return @ClientValue
END
-- this uses the return value of the stored procedure;
-- -1 indicates nothing found
-- any positive, non-zero value is the actual ClientId that was located
Randy Braze
  • 105
  • 2
  • 6

3 Answers3

8

Returning a scalar value is more efficient than a result set, the reason is result set carries lot more helper methods along with it, which makes it heavy thus increasing latency in transmission of the object from sql to C# code/routine.

In your method 3: You have used a variable to return the value this is more better than sending an out parameter since here you are cutting down on traverse of an object atleast in one route ( i.e., when invoking the stored procedure).

A result set is more flexible than an output parameter because it can return multiple rows (obviously), so if you need a result set then it's the only choice anyway.

To order the queries based on performance that goes as Method 3, Method 2 Method 1.

Hope this is helpful in understanding the concept.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Vinay Kumar.o
  • 316
  • 1
  • 10
  • 1
    Another precision. With Method 3 (RETURN), you are limited to INTEGER type. If you want to return another data type, you should use Method 2 or Method 1. – Matt Roy Sep 06 '13 at 14:33
  • I appreciate all the help. Personally, I was leaning toward #2. However, as I'm returning an INTEGER in both #2 and #3, I could see where #3 would have the best performance. I would use #2 if I had to return some type of string. Of course, as previously stipulated, I would use #1 only if I need a result set. – Randy Braze Sep 25 '13 at 18:58
1

In terms of performance penalty, method 3 (RETURN) is penalty-free. The reason being that SQL Server will always return an integer result code from a Stored Procedure. If you do not explicitly specify one, then it it will implicitly return 0 (SUCCESS).

DCW
  • 11
  • 1
0
CREATE PROCEDURE GetClientId 
   @DealerCode varchar(10)
AS
BEGIN
    SET NOCOUNT ON
    DECLARE @ClientValue INT=0;
    SELECT @ClientValue = ClientId FROM Client WHERE ClientCode = 
@DealerCode
    IF @ClientValue IS NULL OR @ClientValue = 0
        RETURN -1;
    ELSE
        RETURN @ClientValue;
END
-- this uses the return value of the stored procedure;
-- -1 indicates nothing found
-- any positive, non-zero value is the actual ClientId that was located