为什么使用Mysql本机驱动程序为Nodejs执行相同的查询需要大约300毫秒,即使使用或不使用“创建池”选项? 请参阅下面附带截图中的突出显示部分 同样对于本机驱动程序执行时间,请参见
请参阅下面附带截图中的突出显示部分
同样对于本机驱动程序执行时间,请参见下面附带的屏幕截
node.js的代码库Mysql Native驱动程序
db.js
var mysql = require('mysql'); var connectionpool = mysql.createPool({ connectionLimit: 100, //important host: 'localhost', user: config.development.username, password: config.development.password, database: config.development.database, multipleStatements: true, }); exports.getConnection = function(callback) { connectionpool.getConnection(callback); };
emp.js
var connections = require('../config/db'); var pre_query = new Date().getTime(); var sqlstatements = "select * from employees where recordstate<>'raw'and DATE(createdAt) " + "between ('1982-03-24') and ('2017-04-23') order by employeesID desc limit 20 offset 0;" + "select COUNT(*) as count from employees where recordstate<>'raw'and " + "DATE(createdAt) between ('1982-03-24') and ('2017-04-23') order by employeesID desc"; connections.getConnection(function(err, c) { console.log(sqlstatements) c.query(sqlstatements, function(err, projects) { console.log(err); if (!err) { c.release(); var red = new Object(); red.rows = JSON.parse(JSON.stringify(projects[0])); red.count = JSON.parse(JSON.stringify(projects[1]))[0].count; var post_query = new Date().getTime(); // calculate the duration in seconds var duration = (post_query - pre_query) / 1000; console.log(duration) // console.log(red); res.json(red); } }) })您在JS中的测量包括连接设置和结果的所有处理. MySQL Workbench(和MySQL终端客户端)中报告的时间只是服务器报告的内容(运行查询和结果传输).连接设置可能需要300ms的大部分时间.尝试在运行实际查询之前将pre_query init移动到该行.并在此之后直接结束时间测量(在console.log(错误)调用之前.这提供了与其他客户端工具报告的结果相当的结果.