Search code examples
node.jsperformancetedious

Is there a reason a call to a simple stored procedure via Tedious is taking so long?


I have noticed that when calling a simple stored procedure CreateStatusReport that simply does an INSERT into a table, it often takes an unusually long time to execute which I am (for better or worse) assuming has to do with some TDS overhead or perhaps the way I'm invoking it.

Here's the TDS code that's called within a Node.js REST API:

function createReport(req, res) {
    let isReleased = false;

    let connection = new Connection(config);  

    connection.on('connect', function(err) {  
        let sql = "CreateStatusReport";

        if (err) console.log("**CN-ERROR", err);

        let request = new Request(sql, (err)=>{
            if (err) {
                console.log("**RQ-ERROR**", err);
            }
        });

        // Check to see if user clicked the "Release" button...
        if (typeof req.body.release != 'undefined')
            isReleased = true;

        request.addParameter('empid', TYPES.NVarChar, req.body.empid);
        request.addParameter('userid', TYPES.NChar, req.body.userid);
        request.addParameter('firstname', TYPES.NVarChar, req.body.fname);
        request.addParameter('lastname', TYPES.NVarChar, req.body.lname);
        request.addParameter('title', TYPES.NVarChar, req.body.title);
        request.addParameter('cabinet', TYPES.NVarChar, req.body.cabinet);
        request.addParameter('office', TYPES.NVarChar, req.body.office);
        request.addParameter('division', TYPES.NVarChar, req.body.division);
        request.addParameter('branch', TYPES.NVarChar, req.body.branch);
        request.addParameter('perstart', TYPES.VarChar, req.body.periodStart);
        request.addParameter('perend', TYPES.VarChar, req.body.periodEnd);
        request.addParameter('status', TYPES.NVarChar, req.body.status);
        request.addParameter('needs', TYPES.NVarChar, req.body.needs);
        request.addParameter('goals', TYPES.NVarChar, req.body.goals);
        request.addParameter('concerns', TYPES.NVarChar, req.body.concerns);
        request.addParameter('released', TYPES.Bit, isReleased);

        connection.callProcedure(request);

        request.on("requestCompleted", ()=> {
            res.redirect("/kudos")    
        });
    });  
}

The stored procedure is very simple so I'll just put the "guts" here:

INSERT INTO [status-report] (empid, [userid], firstname, lastname, title,[cabinet], [office], [division], [branch], [period-start], [period-end], [date-posted], [status], needs, goals, concerns, released) 
VALUES (@empid, @userid, @firstname, @lastname, @title, @cabinet, @office, @division, @branch, @perstart, @perend, GETDATE(), @status, @needs, @goals, @concerns, @released)

It can take up to several seconds for the record to actually get created into the system and I've no idea why since the system is on a high-performance production system.

Are there some things here I'm overlooking? Any suggestions as to how I can find the root cause?

Thanks in advance!

UPDATE

USE [kudos]
GO

/****** Object:  StoredProcedure [dbo].[GetList]    Script Date: 5/1/2019 4:39:50 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[GetList]
AS
BEGIN
    SET NOCOUNT ON

    SELECT 
        [id],
        [empid],
        [lastname] + ', ' + [firstname] AS [name],
        LTRIM(RTRIM([userid])) AS [userid],
        [title],
        [cabinet],
        [office],
        [division],
        [branch],
        CONVERT(nvarchar(10), [period-start], 110) AS [periodstart],
        CONVERT(nvarchar(10), [period-end], 110) AS [periodend], 
        CONVERT(nvarchar(10), [date-posted], 110) AS [dateposted],
        IIF(released = 1, 'Released', 'In Progress') AS [released]
    FROM 
        [status-report] 
    WHERE 
        [date-posted] BETWEEN GETDATE() - 21 AND GETDATE() 
    ORDER BY 
        [date-posted] DESC
END
GO

Solution

  • This was an ID10T error (mine). There's load balancer in production. Sometimes the call would get made, sometimes not because the code wasn't in sync on both systems.