英文:
Golang SQL: More Efficient to Query Two Tables at Once or Separate Queries/Connection Pools?
问题
我有一个连接池,用于数据库A和数据库B。我正在将一些Node.JS代码转移到Go(如果有关系的话,我正在使用SQL Server),其中一些查询正在执行以下操作:
db.A.Query(`
select ... from some_table;
select ... from B..other_table;
`)
这种方式好,还是像这样好:
db.A.Query(...)
db.B.Query(...)
我从这里阅读到这一行:
> 为您需要访问的每个不同数据存储创建一个sql.DB对象
现在我才意识到我将“数据存储”读成了“数据库”,所以现在我甚至不确定是否高效地拥有这两个数据库连接池!
谢谢您的帮助!
英文:
I have a connection pool for database A and database B. I am moving some Node.JS code over to Go (I'm using SQL Server if that matters), and some of the queries are doing this:
db.A.Query(`
select ... from some_table;
select ... from B..other_table;
`)
Is it better to do it that way, or like:
db.A.Query(...)
db.B.Query(...)
I read this line:
> create one sql.DB object for each distinct datastore you need to access
from here. And only now do I realize I read 'datastore' as 'database', so now I'm not even sure if it's efficient to have these two database connection pools!
Thank you for any help!
答案1
得分: 1
对于大多数情况和发送多个SELECT查询的SQL Server客户端程序来说,在一个批处理中发送查询与分批发送查询在效率上没有太大差异。也许如果查询返回的结果集非常小,并且您以非常高的频率运行它们,可能会看到一些实质性的差异。但在典型情况下,无论您是将查询发送到一个批处理还是两个批处理中,都不会有太大影响。
对于SQL Server本身来说,这并不重要,所以唯一的区别在于客户端/服务器的网络流量。
SSMS可以让您比较在一个批处理脚本和多个批处理脚本中运行查询时的客户端统计信息。例如,在SSMS中运行以下查询:
select top 10 * from sys.objects
select top 5 * from sys.columns
然后是:
select top 10 * from sys.objects
GO
select top 5 * from sys.columns
在SSMS中输出以下客户端统计信息:
英文:
For most scenarios and SQL Server client programs sending multiple SELECT queries in a batch is not materially more efficient. Perhaps if the queries returned very small result sets, and you ran them at very high frequency, you could see a material difference. But in the paradigm case, whether you send the queries in one or two batches won't matter much.
It won't matter to SQL Server at all, so the only difference will be in the client/server network traffic.
SSMS will let you compare the client statistics between running queries in a one-batch script and a multi-batch script. EG running
select top 10 * from sys.objects
select top 5 * from sys.columns
and then
select top 10 * from sys.objects
GO
select top 5 * from sys.columns
in SSMS outputs the following client statistics
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论