Skip to main content
added [query-performance] to 2412 questions - Shog9 (Id=1924)
Link
formatting and flow improvement; shout case replacement; minor rewording for greater clarity
Source Link
Andriy M
  • 23.2k
  • 6
  • 59
  • 103

Why is a COUNT query faster than a RESULTSETresult set query?

Suppose I have two similar queries, with complex joins; query1query 1 returns rows and columns, and query2 returns just the Count of rows as per query 1;:

var mySearchSql = @"SELECTSELECT col1, col2, col3, ...etc 
    FROM MyBigTable
       LEFT JOIN AnotherTable1 On ... etc 
       LEFT JOIN AnotherTable2 On ... etc
    WHERE someCol1= val1
    AND someCol2= val2
    AND etc...
";

var myCountSql = @"SELECT

and query 2 returns just the count of rows as per query 1:

SELECT count(MyBigTable.PKcol)
    FROM MyBigTable
       LEFT JOIN AnotherTable1 On ... etc 
       LEFT JOIN AnotherTable2 On ... etc
    WHERE someCol1= val1
    AND someCol2= val2
    AND etc...
";

Self-evident logic tells me the SqlSQL engine (of most databases, say SqlServerSQL Server, MySqlMySQL, IBM DB2, whatevs...) will RETURNreturn from the count query FASTERfaster than the resultset query; AT LEAST WHEN WE ARE TALKING BIG RESULT-SETSresult set query – at least when we are talking big result sets.

Obviously Obviously transferring lots of rows and columns across a network... will take longer. than just a single scalar value of count  !

My questions are (a) does the DB-Engine have to do similar amount of work / effort for both queries, and (b) therefore, the bottleneck (or delay in receiving the results) is simply due to transferring bigger data across a network ?:

  1. Does the DB Engine have to do similar amount of work / effort for both queries?
  2. If so, is the bottleneck (or delay in receiving the results) simply due to transferring bigger data across a network?

Why is a COUNT query faster than a RESULTSET query?

Suppose I have two similar queries, with complex joins; query1 returns rows and columns, and query2 returns just the Count of rows as per query 1;

var mySearchSql = @"SELECT col1, col2, col3, ...etc 
    FROM MyBigTable
       LEFT JOIN AnotherTable1 On ... etc 
       LEFT JOIN AnotherTable2 On ... etc
    WHERE someCol1= val1
    AND someCol2= val2
    AND etc...
";

var myCountSql = @"SELECT count(MyBigTable.PKcol)
    FROM MyBigTable
       LEFT JOIN AnotherTable1 On ... etc 
       LEFT JOIN AnotherTable2 On ... etc
    WHERE someCol1= val1
    AND someCol2= val2
    AND etc...
";

Self-evident logic tells me the Sql engine (of most databases, say SqlServer, MySql, IBM DB2, whatevs...) will RETURN from the count query FASTER than the resultset query; AT LEAST WHEN WE ARE TALKING BIG RESULT-SETS.

Obviously transferring lots of rows and columns across a network... will take longer. than just a single scalar value of count  !

My questions are (a) does the DB-Engine have to do similar amount of work / effort for both queries, and (b) therefore, the bottleneck (or delay in receiving the results) is simply due to transferring bigger data across a network ?

Why is a COUNT query faster than a result set query?

Suppose I have two similar queries, with complex joins; query 1 returns rows and columns:

SELECT col1, col2, col3, ...etc 
FROM MyBigTable
   LEFT JOIN AnotherTable1 On ... etc 
   LEFT JOIN AnotherTable2 On ... etc
WHERE someCol1= val1
AND someCol2= val2
AND etc...

and query 2 returns just the count of rows as per query 1:

SELECT count(MyBigTable.PKcol)
FROM MyBigTable
   LEFT JOIN AnotherTable1 On ... etc 
   LEFT JOIN AnotherTable2 On ... etc
WHERE someCol1= val1
AND someCol2= val2
AND etc...

Self-evident logic tells me the SQL engine (of most databases, say SQL Server, MySQL, IBM DB2, whatevs...) will return from the count query faster than the result set query – at least when we are talking big result sets. Obviously transferring lots of rows and columns across a network will take longer than just a single scalar value of count!

My questions are:

  1. Does the DB Engine have to do similar amount of work / effort for both queries?
  2. If so, is the bottleneck (or delay in receiving the results) simply due to transferring bigger data across a network?
Source Link
joedotnot
  • 123
  • 1
  • 4

Why is a COUNT query faster than a RESULTSET query?

Suppose I have two similar queries, with complex joins; query1 returns rows and columns, and query2 returns just the Count of rows as per query 1;

var mySearchSql = @"SELECT col1, col2, col3, ...etc 
    FROM MyBigTable
       LEFT JOIN AnotherTable1 On ... etc 
       LEFT JOIN AnotherTable2 On ... etc
    WHERE someCol1= val1
    AND someCol2= val2
    AND etc...
";

var myCountSql = @"SELECT count(MyBigTable.PKcol)
    FROM MyBigTable
       LEFT JOIN AnotherTable1 On ... etc 
       LEFT JOIN AnotherTable2 On ... etc
    WHERE someCol1= val1
    AND someCol2= val2
    AND etc...
";

Self-evident logic tells me the Sql engine (of most databases, say SqlServer, MySql, IBM DB2, whatevs...) will RETURN from the count query FASTER than the resultset query; AT LEAST WHEN WE ARE TALKING BIG RESULT-SETS.

Obviously transferring lots of rows and columns across a network... will take longer. than just a single scalar value of count !

My questions are (a) does the DB-Engine have to do similar amount of work / effort for both queries, and (b) therefore, the bottleneck (or delay in receiving the results) is simply due to transferring bigger data across a network ?