"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
  •  | 
  •  

문제

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 ?

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top