Why should we optimize:
With the start of the actual project, the database will run for a period of time , the initial database settings will have some differences from the actual database running performance. At this time, we need to make an optimization adjustment.
Database optimization is a large topic and can be divided into four major categories:
》Host performance
》Memory usage performance
## 》Network transmission performance
## 》SQL statement execution performance [Software Engineer]
##The following are some database SQL optimization solutions:
The database parser follows the The table names in the FROM clause are processed in right-to-left order,
The table written last in the FROM clause will be processed first,
In the FROM clause When the sentence contains multiple tables, you must select the table with the smallest number of records and put it at the end.
If there are more than 3 tables connected to the query, you need to select the table that is used by other tables. The referenced table comes last.
For example: Query employee number, 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 so on
The database uses a right-to-left order to parse WHERE clauses. According to this principle, the connection between tables must be written to the left of other WHERE conditions,
Those conditions that can filter out the maximum number of records must be written to the right of the WHERE clause.
For example: query employee number, 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 * sign in SELECT clause
During the parsing process, the database will convert * into all column names in turn. This work is completed by querying the data dictionary, which means it will take more time (04) Delete all records in the table and replace DELETE with TRUNCATE because COMMIT will release the rollback point (06) Replace the HAVING clause with the WHERE clause WHERE is executed first, then HAVING is executed (07 ) Use internal functions to improve SQL efficiency ## salgrade s (09) Use column aliases
Select empno,ename from emp;
(05) Use COMMIT as much as possible
(08) Use table aliases
The above is the detailed content of Several MySQL database optimization solutions. For more information, please follow other related articles on the PHP Chinese website!