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

Implementation of sharing data between Docker Volume containers

What is volume? Volume means capacity in English,...

Vue implements tab label (label exceeds automatic scrolling)

When the created tab label exceeds the visible ar...

mysql 8.0.19 win10 quick installation tutorial

This tutorial shares the installation tutorial of...

Detailed explanation of mysql scheduled tasks (event events)

1. Brief introduction of the event An event is a ...

Linux 6 steps to change the default remote port number of ssh

The default ssh remote port in Linux is 22. Somet...

Solution to the problem of not finding Tomcat configuration in Intelli Idea

I joined a new company these two days. The compan...

mysql code to implement sequence function

MySQL implements sequence function 1. Create a se...

How to configure domestic sources in CentOS8 yum/dnf

CentOS 8 changed the software package installatio...

Example code for implementing stacked carousel effect with HTML+CSS+JS

Effect: When the slideshow moves in one direction...

Use Vue3 for data binding and display list data

Table of contents 1. Comparison with Vue2 1. New ...

TypeScript problem with iterating over object properties

Table of contents 1. Problem 2. Solution 1. Decla...

5 Simple XHTML Web Forms for Web Design

Simple XHTML web form in web design 5. Technique ...

How to implement property hijacking with JavaScript defineProperty

Table of contents Preface Descriptors Detailed ex...

mysql 5.7.11 winx64.zip installation and configuration method graphic tutorial

Install and configure the MySql database system. ...