简体   繁体   English

NodeJS和MySql中的连接池问题

[英]Connection pooling issue in nodeJS and MySql

I am using NodeJs and Mysql. 我正在使用NodeJs和Mysql。 I have a query to update one field in mysql table as: 我有一个查询以将mysql表中的一个字段更新为:

Update messaging SET count = count + 1 WHERE msgId = 10;

I am using connnection pool. 我正在使用连接池。 configuration as: 配置为:

var pool = mysql.createPool({
connectionLimit : 100,
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.database,
port: 3306,
debug: false,
multipleStatements: true});

API that is using this query works when message is delivered to the 5 million users. 当消息传递给500万用户时,使用此查询的API起作用。 it makes the connections to remain opened . 它使连接保持打开状态 That results in server processing very slow. 这导致服务器处理非常缓慢。 Is there any alternative solution to improve the server performance ? 是否有其他解决方案可提高服务器性能

Paradoxically, reducing the number of connections will generally improve performance. 矛盾的是,减少连接数通常会提高性能。 Pooled connections are serially reusable resources, and there's a queue of connection requests. 池连接是可串行重用的资源,并且存在连接请求队列。 So, instead of trying to do a whole lot of queries at once, the DBMS will handle a smaller number of queries at once, and handle the load serially. 因此,DBMS不会一次尝试执行大量查询,而是一次处理少量查询,并按顺序处理负载。

So for queries like yours that should run for a short time, create a pool with a small connectionLimit. 因此,对于应该在短时间内运行的查询(如您的查询),请创建带有较小connectionLimit的池。 Try 10. If that works well, try 5. 尝试10。如果效果很好,请尝试5。

If you have other queries that take longer, you may want a different connection pool for those. 如果您有其他查询需要更长的时间,则可能需要其他连接池。

Not to mention that a clustered node app will open that number of connections for each member of the cluster. 更不用说集群节点应用程序将为集群的每个成员打开该数量的连接。 400 connections is too many for almost all MySQL deployments. 对于几乎所有MySQL部署而言,400个连接都是太多的。 (If you have a deployment that handles hundreds of connections, your purchasing manager knows about it, because such deployments are very expensive.) (如果您有一个可以处理数百个连接的部署,那么您的采购经理就会知道这一点,因为此类部署非常昂贵。)

You might have this problem, however: If millions of users are updating exactly the same row msgId = 10 you will have DBMS contention on that row. 但是,您可能会遇到此问题:如果数百万用户正在完全更新同一行msgId = 10 ,则该行将具有DBMS争用。 In that case, you may want to store up the number of hits to the row, and then do UPDATE... SET count = count + n once every few seconds rather than upon every hit. 在这种情况下,您可能希望存储行的命中数,然后执行UPDATE... SET count = count + n每隔几秒钟一次,而不是每次命中一次。

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

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