简体   繁体   English

为什么针对node.js的Mysql Native驱动程序的查询执行时间如此之长? 还有其他选择吗?

[英]Why is query execution time so high for Mysql Native driver for node.js? Any alternatives?

Why does same query take around 300 ms more than actual execution time when using Mysql native driver for Nodejs , even with or without using "create pool" options? 为什么将Mysql本机驱动程序用于Node.js时,即使使用或不使用“创建池”选项,相同的查询都比实际执行时间多花费300毫秒

Please refer highlighted section in below attached screenshot 请参考以下屏幕截图中突出显示的部分

MySQL工作台执行

Also for native driver execution time, please see below attached screenshot: 另外,有关本机驱动程序的执行时间,请参见以下附件的屏幕截图:

在此处输入图片说明

Codebase for node.js Mysql Native driver Node.js Mysql本机驱动程序的代码库

db.js 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 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);

                }
            })
        })

Your measurement in JS includes connection setup and all the processing of the results. 您在JS中的测量包括连接设置和结果的所有处理。 The times reported in MySQL Workbench (and the MySQL terminal client) are only what the server reports (running a query and result transmission). MySQL Workbench(和MySQL终端客户端)中报告的时间仅是服务器报告的时间(运行查询和结果传输)。 Alone the connection setup probably takes most of the 300ms. 仅连接建立可能要花费300ms的大部分时间。 Try moving the pre_query init to the line right before running the actual query. 在运行实际查询之前,请尝试将pre_query初始化移至该行。 And end the time measurement directly after that (before the console.log(err) call. This delivers a result comparable to that reported by other client tools. 并在此之后立即结束时间测量(在console.log(err)调用之前。这提供的结果可与其他客户端工具报告的结果相提并论)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM