简体   繁体   English

MySQL 存储过程与 Node.js 性能对比

[英]MySQL stored procedure vs Node.js performance

I have an API with Express/Node.js with MySQL DB.我有一个 API 和 Express/Node.js 和 MySQL DB。 I'm using the Mysql2 npm package (no ORM) for queries.我正在使用 Mysql2 npm package(无 ORM)进行查询。 For some particular routes, I have some queries running in parallel (which are not exactly related, so no joins can be performed).对于某些特定路线,我有一些并行运行的查询(它们并不完全相关,因此无法执行连接)。 I want to know whether using a stored procedure can improve my performance, especially considering the non-blocking behavior of Node.js.我想知道使用存储过程是否可以提高我的性能,特别是考虑到 Node.js 的非阻塞行为。 I'm mainly concerned with 2 parameters:我主要关心2个参数:

  1. Query time and查询时间和
  2. Server overhead.服务器开销。

To rephrase, if I changed from individual queries to stored procedure, what will be the effect on my response time and server utilization?换句话说,如果我从单个查询更改为存储过程,对我的响应时间和服务器利用率会有什么影响?

Edit:编辑:

What I mean by queries running in parallel is, I have a function set up, so that the queries can run independently (without waiting for the response of the previous query) and once all promises resolve, my function returns the results as a Promise.resolve.我所说的并行运行查询的意思是,我设置了 function,以便查询可以独立运行(无需等待上一个查询的响应),一旦所有承诺解决,我的 function 将结果返回为 ZA7593F0F287A7A44。解决。

A little snippet:一个小片段:

const makeThreeQueries = (q1, q2, q3) => { # queries being array with 2 elements: ['query string', ['Array', 'of', 'Args']]
    return new Promise((resolve) => {
        const results = []
        const queryHandler = (result) => {
            results.push(result)
            if (results.length === 3) resolve(results) # 3 in case there are 3 queries
        }
        db.query(...q1).then(queryHandler)
        db.query(...q2).then(queryHandler) # doesn't wait for q1 to finish
        db.query(...q3).then(queryHandler) # doesn't wait for q2 to finish
    })
    
}

Of course the main code is much more robust than this, with proper error handling and validation/positional consistency in place.当然,主代码比这更健壮,有适当的错误处理和验证/位置一致性。

There are some savings because control stays inside the Stored Proc rather than needing to go back and forth between the client and server.有一些节省,因为控制保留在存储过程中,而不是需要在客户端和服务器之间来回 go。

The savings is very dramatic if the client and server are on different servers that are geographically far apart.如果客户端和服务器位于地理上相距很远的不同服务器上,则节省的费用非常可观。 In this case, transmission time can dominate performance.在这种情况下,传输时间可以支配性能。

Whether the code is coming from the client or from a stored proc, parallelism may require careful attention to transactions -- to keep separate actions from stepping on each other.无论代码是来自客户端还是来自存储过程,并行性可能需要仔细注意事务——以防止单独的操作相互影响。

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

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