多个connection.query-s是否会减慢node.js / mysql中的连接?

问题描述

我有这个网站反复使用相同的查询。我想到了创建小的函数,将连接和其他参数传递给它,然后重用这些函数的方法。
例如,这是使用多个语句的典型方法:

pool.getConnection((err,connection) => {
    if(err) return console.log(err);

    connection.query("SELECT * FROM customers; SELECT * FROM orders;",(err,rows) => {
        if(err) return console.log(err);

        const customers = rows[0];
        const orders = rows[1];

        return {
            customers,orders
        }
    })
    connection.release()
})

但是,假设我将一遍又一遍地重复使用相同的查询。因此,我可以做这样的事情:

const getCustomers = (connection,callback) => {
    connection.query("SELECT * FROM customers;",customers) => {
        if(err) return callback(err,null);

        return callback(null,customers)
    })
}

const getOrders = (connection,callback) => {
    connection.query("SELECT * FROM orders;",orders) => {
        if(err) return callback(err,orders)
    })
}

pool.getConnection((err,connection) => {
    if(err) return console.log(err);

    getCustomers(connection,customers) => {
        if(err) return console.log(err);

        getOrders(connection,orders) => {
            if(err) return console.log(err);

            return {
                customers,orders
            }
        })
    })
    connection.release()
})

那么使用多个连接会降低服务器的速度吗?而是1个具有多个查询的连接?另外,这是使用this库的正确方法吗? connection.release()是否正确使用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)