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

How to optimize database sql statements

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to optimize the database sql statement, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

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

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.dnamefrom salgrade,dept,empwhere (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, and 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's number, name, salary, department name

Select emp.empno,emp.ename,emp.sal,dept.dnamefrom emp,deptwhere (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

On how to optimize the database sql statement to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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