"Select count (*)" vs count of results from "Select 1" : which is more efficient way to get the rows count in DB2? [closed]

StackOverflow https://stackoverflow.com/questions/22893289

  •  28-06-2023
  •  | 
  •  

Question

I have two approaches to get count of rows in a table in DB2. One way is

SELECT COUNT(*) FROM Foo WHERE col1 = val1;

Another way is to count the results (number 1's list) which are retrived from following query with a method in my java code

SELECT 1 FROM Foo WHERE col1 = val1;

Here I will get the "list of number 1's" from second query and then get the size of that list in my java code to get count.

Can somebody explain which is most efficient way to get the rows count ?

Was it helpful?

Solution

select count is faster - because the database only needs to return a single number rather than a potentially long list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top