Home  >  Article  >  Database  >  Several MySQL database optimization solutions

Several MySQL database optimization solutions

巴扎黑
巴扎黑Original
2017-07-19 17:21:061268browse

I am looking for a job recently. Many companies will ask questions about database optimization during interviews. Today I will summarize the database optimization issues and use the MySQL database as an example to explain.

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:

(01) Choose the most efficient order of table names (frequent written test)

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

(02) The connection order in the WHERE clause (written test common Test)

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
Select empno,ename from emp;

(04) Delete all records in the table and replace DELETE with TRUNCATE

(05) Use COMMIT as much as possible

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

(08) Use table aliases

## salgrade s

(09) Use column aliases

## ename e

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn