Contrary to general opinion, COUNT (*) is slightly faster than count (1), although the count of indexed columns is still the fastest if indexed retrieval is possible. such as COUNT (EMPNO)
(Translator: In the CSDN forum, there has been quite a heated discussion, the author's point of view is not very accurate, through the actual test, the above three methods do not have significant performance differences)
14. Replace the HAVING clause with the WHERE clause
Avoid the HAVING clause, which will filter the result set only after all records have been retrieved. This processing requires sorting, totals, and so on. If you can limit the number of records through the WHERE clause, you can reduce the overhead.
For example:
Low efficiency:
SELECT Region,avg (log_size)
From LOCATION
GROUP by REGION
Having REGION REGION!= ' SYDNEY '
and REGION!= ' PERTH '
Efficient
SELECT Region,avg (log_size)
From LOCATION
WHERE REGION REGION!= ' SYDNEY '
and REGION!= ' PERTH '
GROUP by REGION
(Translator: The condition in having is generally used for comparisons of some aggregate functions, such as count (), and so on. In addition, general conditions should be written in the WHERE clause)
15. Reduce the query to the table
In SQL statements that contain subqueries, you should pay special attention to reducing the query to the table.
For example:
Low efficiency
SELECT Tab_name
From TABLES
WHERE tab_name = (SELECT tab_name
From Tab_columns
WHERE VERSION = 604)
and db_ver= (SELECT db_ver
From Tab_columns
WHERE VERSION = 604)
Efficient
SELECT Tab_name
From TABLES
WHERE (Tab_name,db_ver)
= (SELECT tab_name,db_ver)
From Tab_columns
WHERE VERSION = 604)
Update multiple column Examples:
Low efficiency:
UPDATE EMP
SET Emp_cat = (SELECT MAX (CATEGORY) from Emp_categories),
Sal_range = (SELECT MAX (Sal_range) from emp_categories)
WHERE emp_dept = 0020;
Efficient:
UPDATE EMP
SET (Emp_cat, Sal_range)
= (SELECT max (CATEGORY), Max (Sal_range)
From Emp_categories)
WHERE emp_dept = 0020;
16. Improve SQL efficiency through internal functions.
(translator: Often seen in forums like ' can write with a SQL ... ' Posts, but the complexity of SQL often sacrificed the efficiency of execution. It is very meaningful to be able to master the application of the above function to solve the problem in practical work.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.