Why to optimize:
With the start of the actual project, after the database runs for a period of time, the initial database settings will
have some differences with the actual database performance. At this time, we need to make an optimization adjustment.
The subject of database optimization is relatively large and can be divided into four categories:
》Host performance
》Memory usage performance
》Network transmission performance
》SQL statement execution performance [Software Engineer]
Some database SQL optimization schemes are listed below:
(01) Choose the most efficient The table name order of the table (written test often) The parser of the database processes the table names in the FROM clause in
the order from right to left. In the case of multiple tables, you must select the table with the least number of records and put it at the end. If there are more than 3 tables for join query, you need to select the table referenced by other tables and put it at the end. For example: query employee's ID, name, salary, salary grade, department name select emp.empno,emp.ename,emp.sal,salgrade.grade,dept.dname from salgrade,dept,emp where (emp.deptno = dept. deptno) and (emp.sal between salgrade.losal and salgrade.hisal)
1) If the three tables are completely unrelated, write the table with the fewest records and column names at the end, and so on
2) If the three tables are related, put the table with the most references at the end, and then And so on
(02) The connection order in the WHERE clause (the written test is often tested)
The database uses the right-to-left order to parse the WHERE clause. According to this principle, the connection between the tables must be written on the
left of other WHERE conditions.
Those that can be The condition to filter out the maximum number of records must be written to the right of the WHERE clause.
For example: query employee's ID, name, salary, department name
select emp.empno,emp.ename,emp.sal,dept.dname
from emp,dept
where (emp.deptno = dept.deptno) and (emp.sal > 1500 ) (03) Avoid using * in the SELECT clause. In the process of parsing, the database will convert * into all column names in turn. This work is done by querying the data dictionary, which means that it will take more time to select empno,ename from emp; (04) Use TRUNCATE instead of DELETE (05) Use COMMIT as much as possible because COMMIT will release the rollback point (06) Use the WHERE clause to replace the HAVING clause WHERE executes first, HAVING after (07) Use more Internal functions improve SQL efficiency
(08)使用表的别名
salgrade s
(09)使用列的别名
ename e
With the start of the actual project, after the database runs for a period of time, the initial database settings will
have some differences with the actual database performance. At this time, we need to make an optimization adjustment.
The subject of database optimization is relatively large and can be divided into four categories:
》Host performance
》Memory usage performance
》Network transmission performance
》SQL statement execution performance [Software Engineer]
Some database SQL optimization schemes are listed below:
(01) Choose the most efficient The table name order of the table (written test often) The parser of the database processes the table names in the FROM clause in
the order from right to left. In the case of multiple tables, you must select the table with the least number of records and put it at the end. If there are more than 3 tables for join query, you need to select the table referenced by other tables and put it at the end. For example: query employee's ID, name, salary, salary grade, department name select emp.empno,emp.ename,emp.sal,salgrade.grade,dept.dname from salgrade,dept,emp where (emp.deptno = dept. deptno) and (emp.sal between salgrade.losal and salgrade.hisal)
1) If the three tables are completely unrelated, write the table with the fewest records and column names at the end, and so on
2) If the three tables are related, put the table with the most references at the end, and then And so on
(02) The connection order in the WHERE clause (the written test is often tested)
The database uses the right-to-left order to parse the WHERE clause. According to this principle, the connection between the tables must be written on the
left of other WHERE conditions.
Those that can be The condition to filter out the maximum number of records must be written to the right of the WHERE clause.
For example: query employee's ID, name, salary, department name
select emp.empno,emp.ename,emp.sal,dept.dname
from emp,dept
where (emp.deptno = dept.deptno) and (emp.sal > 1500 ) (03) Avoid using * in the SELECT clause. In the process of parsing, the database will convert * into all column names in turn. This work is done by querying the data dictionary, which means that it will take more time to select empno,ename from emp; (04) Use TRUNCATE instead of DELETE (05) Use COMMIT as much as possible because COMMIT will release the rollback point (06) Use the WHERE clause to replace the HAVING clause WHERE executes first, HAVING after (07) Use more Internal functions improve SQL efficiency
(08)使用表的别名
salgrade s
(09)使用列的别名
ename e