Database SQL statement optimization

Database SQL statement optimization

Why optimize:

With the launch of the actual project, after the database has been running 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 topic 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]

Here are some database SQL optimization solutions:

(01) Choose the most efficient table name order (often asked in the written test)

The database parser processes the table names in the FROM clause from right to left. The last table in the FROM clause will be processed first. If the FROM clause contains multiple tables, you must select the table with the least number of records and put it last. If there are more than three tables in the query, you need to select the table referenced by other tables and put it 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 least records and column names last, and so on.

2) If the three tables are related, put the table with the most references at the end, and so on.

(02) Join order in the WHERE clause (often tested in the written exam)

The database parses the WHERE clause from right to left. According to this principle, the connection 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 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 asterisk (*) in the SELECT clause

During the parsing process, the database will convert * into all column names in sequence. This work is done by querying the data dictionary, which means it will take more time.

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) Replace the HAVING clause with the WHERE clause

WHERE is executed first, HAVING is executed later

(07) Use internal functions more often to improve SQL efficiency

(08) Using table aliases

salgrade s

(09) Using column aliases

ename e

In short, database optimization is not a one-day topic. You have to conduct repeated tests and summaries in long-term work practice. I hope that students will understand it well in the future.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Build a stable and highly available cluster based on mysql+mycat, load balancing, master-slave replication, read-write separation operation
  • Installation and startup of mycat in windows environment
  • Example of using mycat to implement MySQL database read-write separation
  • MyBatis uses MyCat to realize multi-tenancy
  • Installation and use of mysql mycat middleware
  • Database query optimization: subquery optimization
  • Four isolation levels of the database
  • Data constraint examples based on MySQL database and introduction to five integrity constraints
  • How to deal with garbled characters in Mysql database
  • Introduction to MyCat, the database middleware

<<:  VSCode+CMake+Clang+GCC environment construction tutorial under win10

>>:  Analyzing the node event loop and message queue

Recommend

Detailed tutorial for installing MySQL on Linux

MySQL downloads for all platforms are available a...

Calculation of percentage value when the css position property is absolute

When position is absolute, the percentage of its ...

The difference between char and varchar in MYSQL

CHAR and VARCHAR types are similar, differing pri...

How many common loops do you know about array traversal in JS?

Preface As a basic data structure, arrays and obj...

Sample code for nginx to achieve dynamic and static separation

1. Simple configuration of nginx's dynamic an...

Use of Linux ls command

1. Introduction The ls command is used to display...

Some conclusions on developing mobile websites

The mobile version of the website should at least...

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Specific usage of fullpage.js full screen scrolling

1.fullpage.js Download address https://github.com...

Install Windows Server 2019 on VMware Workstation (Graphic Tutorial)

If prompted to enter a key, select [I don’t have ...

Detailed explanation of the benefits of PNG in various network image formats

BMP is an image file format that is independent o...

How does MySQL implement ACID transactions?

Preface Recently, during an interview, I was aske...

How to implement digital paging effect code and steps in CSS

A considerable number of websites use digital pagi...

Analyzing the node event loop and message queue

Table of contents What is async? Why do we need a...

Float and Clear Float in Overview Page

1. Float: The main purpose is to achieve the effe...