Node MySQL execute multiple queries the fastest possible

允我心安 提交于 2019-12-04 13:12:52

Method 1 and 2 are similar except that Pool creates a connection if all connections in the pool are used.

To determine which is faster, you need to know the computing power vs network bandwidth between your application server and your database server.

Here's why:

In method 1 and 2, you are using a single connection to execute multiple queries. If the processing power of the database machine is faster than then network bandwidth to send/receive multiple queries, then method 1 and 2 is more efficient. If the processing power of the database machine is slower than the bandwidth(e.g. both application / mysql server resides on the same machine), then method 3 and 4 will theoretically be faster.

Another factor is whether the statements depend on one another. Because step 1 and 2 essentially runs the statements synchronously, the entire set of operation is an atomic / consistent operation. For method 3 and 4, because they run asynchronously although they are triggered in parallel, there can be instances where a later statement complete earlier than an earlier statement. If that happens and there's dependency, method 3 and 4 will corrupt your data.

TL;DR

  • Fast + Synchronous (insert/update, then select results) = Method 1, 2 (Use pooling to reduce the need to open new connections)
  • Fast + Asynchronous(mass inserts) = Method 3, 4
  • Slow connection between application/database server = Method 1,2

Of the four above options using 3 real queries in my app, that each take about 1-3 seconds each to execute, method 3 ended up being the fastest for anyone interested.

Method 1 and 2 were about a full 1/2 second slower, and method 4 was only super slightly slower.

Edit: I did these benchmarks by using the console.time('query') suggestion in the comments above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!