简体   繁体   English

Node-mysql连接中的多个查询

[英]Node-mysql multiple queries in a connection

I am using node-mysql in my project. 我在我的项目中使用node-mysql。 I have a scenario where the execution of the second query will be based on the result returned from the first. 我有一个场景,其中第二个查询的执行将基于从第一个查询返回的结果。 For example, first query, select * from table where user=x; 例如,第一个查询,从user = x的表中选择*; then based on the existence I will run another query. 然后根据存在情况,我将运行另一个查询。 I am using pool.getConnection(). 我正在使用pool.getConnection()。 May I know whether I should create two connections or use one connection? 我可以知道应该创建两个连接还是使用一个连接?

Option a) 选项a)

 pool.getConnection(function(err, connection) {
     // Use the connection
     connection.query( 'SELECT something FROM sometable', function(err, rows) {
         //store the result here as a var a; 
         connection.release();
     });
 });
 //check var a and start another connection to run the second query?

Option b) 选项b)

pool.getConnection(function(err, connection) {
    // Use the connection
    connection.query( 'SELECT something FROM sometable', function(err, rows) {
       //...based on the result, 
       connection.query(second query)
       connection.release();
    });
});

Thanks Hammer 谢谢锤

Both options are using a pool, so you're already using multiple connections. 这两个选项都使用一个池,因此您已经在使用多个连接。 So either option doesn't really matter, the only difference might be that the first option may result in the second query not being executed right away if the entire pool is in use after the first query is done. 因此,这两个选项都不是很重要,唯一的区别可能是,如果在第一个查询完成后整个池都在使用中,则第一个选项可能会导致第二个查询不会立即执行。

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

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