SQL에서 특정 데이타의 개수를 뽑아낼 때 사용하는 COUNT()함수가 있다.
뭐 데이터의 양이 적은 경우에는 속도차이를 느끼지 못하겠지만 데이터의 양이 많은 DB를 사용하는 경우에는 어떨까...????하는 의문이 생긴다.
아직 확실한 테스트를 해보지는 않았다. 그저 인터넷에서 찾은 정보에 의하면....
영어에 약한분들은 Example부분은 그냥 건너뛰고 Tip: Performance......부분을 보면된다.
=================================================================
In some cases, you will be required to use the SQL GROUP BY clause with the SQL COUNT function.
For example, you could use the SQL COUNT function to return the name of the department and the number of employees (in the associated department) that make over $25,000 / year.
SELECT department, COUNT(*) AS "Number of employees" FROM employees WHERE salary > 25000 GROUP BY department;
Because you have listed one column in your SQL SELECT statement that is not encapsulated in the SQL COUNT function, you must use the SQL GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.
Since the SQL COUNT function will return the same results regardless of what NOT NULL field(s) you include as the SQL COUNT function parameters (ie: within the brackets), you can change the syntax of the SQL 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 SQL 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.
========================================================================
출처 :Http://www.techonthenet.com/sql/count.php
[SQL] 기존 테이블의 컬럼에 not null 제약 추가하기 (0) | 2015.04.20 |
---|---|
[SQL] 한번에 여러 record(데이타) 삽입하기 (0) | 2015.04.20 |
java.lang.SecurityException: Sealing violation exception (0) | 2015.04.20 |
[SQL] CONNECT BY , START WITH (0) | 2015.04.20 |
[SQL] 결과가 두줄 이상인 경우 한줄로 합치기 (0) | 2015.04.20 |