calvinsomething
calvinsomething

Reputation: 123

Golang SQL: More Efficient to Query Two Tables at Once or Separate Queries/Connection Pools?

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!

Upvotes: 0

Views: 783

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89361

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

enter image description here

Upvotes: 1

Related Questions