question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slow running query through node

See original GitHub issue

There is one particular query that takes 25seconds to run through node-mssql whereas if i run it directly on SSMS it takes 200ms

Expected behaviour:

Would expect the run time to be comparable

Running the queries and monitoring on SQL server profiler:

node/mssql

image

SSMS

image

Actual behaviour:

Not sure why but the query consistently takes 25seconds through node/mssql

Configuration:

Connection Configuration

const config = {
	user: DB_USER,
	password: DB_PASS,
	server: DB_HOST,
	requestTimeout: 300000,
	pool: {
		min: 5
	}
}

How I connect to run the query:

const dbc = new sql.connect(config);

const ClientDb = 'Client.dbo';
const StockDb = 'Stock.dbo';

export {
	dbc,
	sql,
	ClientDb,
	StockDb
}

import { dbc, sql, StockDb } from '../../config/db'

Then the actual query:

return dbc.then(pool => {
        return pool.request()
            .input('clientId', sql.Int, clientId)
            .input('startDate', sql.DateTime, new Date(startDate))
            .input('endDate', sql.DateTime, new Date(endDate))
            .query(`SELECT v.Id as VehicleId,
                            n.Name as Make, 
                            m.Name as Model, 
                            v.Variant as Variant,
                            v.Registration as Registration,
                            v.Price As Price,
                            v.OfferPrice as OfferPrice,
                            v.OfferDisplay as OfferDisplay,
                            d.Name as DealershipName,
                            v.StockDate as StockDate,
                            v.PurchaseStandIn as SIV,
                            s.Name as PurchaseSource,
                            DATEDIFF(day, StockDate, GETDATE()) as DaysInStock,
                            v.SoldDate as SoldDate,
                            v.SoldPrice as SoldPrice,
                            s1.Name as SoldSource
                    FROM  ${StockDb}.UsedVehicles v 
                    JOIN  ${StockDb}.Models m on m.Id = v.ModelId
                    JOIN  ${StockDb}.Makes n ON n.Id = m.MakeId
                    JOIN  ${StockDb}.Dealerships d on d.Id = v.DealershipId
                    LEFT JOIN ${StockDb}.UsedPriceSources s on s.Id = v.PurchasePriceSourceId
                    LEFT JOIN ${StockDb}.UsedPriceSources s1 on s1.Id = v.SoldPriceSourceId
                    WHERE v.Status = 3
                    AND v.ClientId = @clientId
                    AND v.SoldDate between @startDate AND @endDate
                    ORDER BY SoldDate ASC`)
    }).then(result => {
        return result.recordset;
    }).catch(err => {
        console.error(err);
    });

Software versions

  • NodeJS: 10
  • node-mssql: 6.2
  • SQL Server: 2017

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

1reaction
haswentcommented, Jun 19, 2020

Happy it’s not a node-mssql/tedious issue as when running directly in SSMS with StockDb.dbo. in front of the table names it does take 25seconds too

Interesting that the issue doesn’t occur with msnodesqlv8

The only difference i can see is with Tedious/node-mssql it runs as RPC and with msnodesqlv8 it runs as SQLBatch

1reaction
destinybonavitacommented, Jun 19, 2020

I’m actually running into the same issue as OP where things are running slow. Below is timings of events happening in my AWS Lambda:

Started  timer 2020-06-19T07:37:24.447Z
11 () @ Opening database
492 () @ Database Opened
495 () @ Executing query
877 () @ Executed query
879 () @ Closing database
887 () @ Database Closed

My database is a standard SQL database running with Azure. I don’t have any of these speed problems at all using Azure Data Studio. The query that was timed above takes 58 milliseconds with the data studio.

The query is an extremely basic “select” statement that is only querying 15 records with 5 columns.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Streaming query with mssql and node, very slow the first time
If I try to stream data from a query, using the node-mssql example , the first time I execute its very slow. It...
Read more >
Query running slow in one node - Oracle Communities
Hi, Execute your query on node where query is running very slow. Get SID and execute query above to see what is event...
Read more >
Factors affecting query performance - Amazon Redshift
More nodes means more processors and more slices, which enables your queries to process faster by running portions of the query concurrently across...
Read more >
[Node] Slow queries for PostgreSQL on NodeJS agent
I recently started using the NodeJS APM agent in my API, and would like to capture information about SQL queries that are executed....
Read more >
Identifying the Top Slow Queries - Couchbase Developer Portal
The duration of the query from when it started executing to when it completed. node, IP address and port of the query engine...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found