Database query optimization: subquery optimization

Database query optimization: subquery optimization

1. Case

Take all employees who are not the head of the company and group them by age!

select age as 'age', count(*) as 'number of people' from t_emp where id not in 
(select ceo from t_dept where ceo is not null) group by age; 

How to optimize?

①Solve the full table scan of the dept table and create an index for the ceo field:

At this time, query again:

② Further optimization, replace not in.

The above SQL can be replaced with:

select age as 'age',count(*) as 'number of people' from emp e left join dept d on e.id=d.ceo where d.id is null group by age; 

Conclusion: When judging the range, try not to use not in and not exists, use left join on xxx is null instead.

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:
  • MySQL database query optimization MySQL efficiency
  • Query optimization and paging algorithm for massive databases
  • Database query optimization (master-slave table design)
  • Detailed explanation of optimizing query statements in MySQL database
  • A collection of query optimization and paging algorithms for massive databases 1/2
  • Collection of query optimization and paging algorithms for massive databases 2/2
  • A practical record of a database query timeout optimization problem

<<:  How to use firewall iptables strategy to forward ports on Linux servers

>>:  Detailed example of using useState in react

Recommend

Use a few interview questions to look at the JavaScript execution mechanism

Table of contents Previous words Synchronous and ...

Native js implementation of magnifying glass component

This article example shares the specific code for...

Win10 + Ubuntu20.04 LTS dual system boot interface beautification

Effect display The built-in boot interface is too...

Analysis of Docker's method for creating local images

The so-called container actually creates a readab...

Detailed explanation of how Zabbix monitors the master-slave status of MySQL

After setting up the MySQL master-slave, you ofte...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

How to add conditional expressions to aggregate functions in MySql

MySQL filtering timing of where conditions and ha...

Summary of Vue watch monitoring methods

Table of contents 1. The role of watch in vue is ...

Mysql | Detailed explanation of fuzzy query using wildcards (like,%,_)

Wildcard categories: %Percent wildcard: indicates...

How to use bar charts in Vue and modify the configuration yourself

1. Import echart in HTML file <!-- Import echa...

Tomcat first deployment web project process diagram

Put your own web project in the webapps directory...

A Deep Dive into JavaScript Promises

Table of contents 1. What is Promise? 2. Why is t...