왠지 더 멋지게 빠르게 잘 해줄것만 같은 기분이 들었습니다.
그래서 실험을 해봤죠.
count안에 들어가는 칼럼은 당연히 index가 걸려있는 칼럼입니다.
1. select count(*) from table;
2. select count(column) from table;
3. select /*+INDEX_FES(table column) */ count(*) from table;
4. select /*+INDEX_FES(table column) count(column) from table;
참고로 INDEX_FES 힌트는 INDEX FAST SCAN하게 하는겁니다.
빠르기는
3 > 1 > 4 > 2 순입니다.
어떤 컬럼의 인덱스를 FAST SCAN하는지를 정해주고 count(*)를 하는게 제일 빠르더군요.
생각보다 오라클은 머리가 좋습니다.
count(*)는 옵티마이저가 제일 좋은 방법을 찾아서 숫자를 세어주는가보더라구요.
아~ 츄닝의 길은 멀고도 험하다!
[출처] 구원님의 성큼성큼 BLOG
! 위 쿼리에서 주의할점 count함수내 컬럼을 명시할 경우 해당컬럼에서 NULL인 로우는 제외하지만 컬럼명시가아닌 *로 지정한다면 모든 행을 리턴하므로 결과값이 상이할 수 있는여지가 있음!!
-> Better performance
TIP: Performance Tuning
Since the COUNT function will return the same results regardless of what NOT NULL field(s) you include as the COUNT function parameters (ie: within the brackets), you can change the syntax of the COUNT function to COUNT(1) to get better performance as the database engine will not have to fetch back the data fields.
For example, based on the example above, the following syntax would result in better performance:
SELECT department, COUNT(1) as "Number of employees"
FROM employees
WHERE salary > 25000
GROUP BY department;
Now, the COUNT function does not need to retrieve all fields from the employees table as it had to when you used the COUNT(*) syntax. It will merely retrieve the numeric value of 1 for each record that meets your criteria.