Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Database sql Optimization 1

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Why optimize:

With the start of the actual project, the database runs for a period of time, the initial database settings will be different from the actual database performance, then we need to make an optimization adjustment.

Database optimization is a big topic, which can be divided into four categories:

"Host performanc

"memory usage performance

"Network transmission performance

"SQL statement execution performance [Software engineer]

Here are some database SQL optimization scenarios:

(01) choose the most efficient order of table names

The parser of the database processes the table names in the FROM clause from right to left, the last table written in the FROM clause will be processed first, in the case of multiple tables in the FROM clause, you must choose the table with the least number of records to be placed at the end, if there are more than 3 table join queries, then you need to select the table referenced by other tables to be placed at the end.

For example: query employee's 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 least 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)

The database parses the WHERE clause from right to left. According to this principle, the join between tables must be written to the left of other WHERE conditions.

Conditions that can filter out the maximum number of records must be written to the right of the WHERE clause.

For example: query employee's 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 the * sign in the SELECT clause

In the process of parsing, the database will convert * to all column names in turn, which is done by querying the data dictionary, which means it will take more time.

Select empno,ename from emp

(04) replace DELETE with TRUNCATE

(05) use COMMIT as much as possible

Because COMMIT releases the rollback point.

(06) replace HAVING clause with WHERE clause

WHERE executes first, HAVING then executes

(07) use more internal functions to improve SQL efficiency

(08) use aliases for tables

Salgrade s

(09) use the alias of the column

Ename e

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report