Note that there are some explanatory texts on larger screens.

plurals
  1. POSequelize querying a lot slower than Knex
    primarykey
    data
    text
    <p>I have the following code:</p> <pre><code>var apiLogger = require(__dirname + '/../configurations/logger').api; var Knex = require('knex'); var microtime = require('microtime'); var Sequelize = require("sequelize"); database = new Sequelize('MYDATABASE', 'MYUSERNAME', 'MYPASSWORD', { host: "MYHOST", port: 3306, dialect: 'mysql', pool: { maxConnections: 5, maxIdleTime: 30 } }); Knex.Initialize({ client: 'mysql', connection: { host : 'MYHOST', user : 'MYUSERNAME', password : 'MYPASSWORD', database : 'MYDATABASE', charset : 'utf8' } }); exports.test = function(req, res){ apiLogger.info('Request made to /api/test'); var start = microtime.nowDouble(); database.query('SELECT id, username FROM Users ORDER BY RAND() LIMIT 10').then(function(data) { console.log('query time : ' + (microtime.nowDouble() - start)); res.json(data); }, function(data) { res.json(data); }); }; exports.test2 = function(req, res){ apiLogger.info('Request made to /api/test2'); var start = microtime.nowDouble(); Knex.Raw('SELECT id, username FROM Users ORDER BY RAND() LIMIT 10').then(function(data) { console.log('query time : ' + (microtime.nowDouble() - start)); res.json(data); }, function(data) { res.json(data); }); }; </code></pre> <p>If I execute the code in <code>exports.test</code> (an api call of <code>/api/test</code>) about 5 seconds apart from each other, the average response is about 430ms and this is the Sequelize code.</p> <p>If I run the code from <code>exports.test2</code> (and api call of <code>/api/test2</code>) 5 seconds apart, the first call is 430ms but the call after that are about 100ms and that is Knex.</p> <p>I should mention if I run the <code>exports.test</code> code less than a second apart from each other, I get the about 100ms response time and with the <code>exports.test2</code> code, I can wait 2-3 minutes between calls and still get the 100ms response time.</p> <p>Is there some sort of configuration I am missing with Sequelize that is causing it to be much slower compared to Knex? Why would requests made minutes apart with Knex be much faster than the same request made seconds apart with Sequelize? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload