How to use not in to optimize MySql

How to use not in to optimize MySql

Recently, when using select query in a project, I used not in to exclude the unused primary key ID. The SQL used at the beginning was as follows:

select 
  s.SORT_ID,
  s.SORT_NAME,
  s.SORT_STATUS,
  s.SORT_LOGO_URL,
  s.SORT_LOGO_URL_LIGHT
from SYS_SORT_PROMOTE s
  WHERE
    s.SORT_NAME = 'Must-Listen Classics'
    AND s.SORT_ID NOT IN ("SORTID001")
  limit 1;

When there is a lot of data in the table, the execution time of this SQL is long and the execution efficiency is low. I found information online that it can be optimized using left join. The optimized SQL is as follows:

select 
  s.SORT_ID,
  s.SORT_NAME,
  s.SORT_STATUS,
  s.SORT_LOGO_URL,
  s.SORT_LOGO_URL_LIGHT
from SYS_SORT_PROMOTE s
left join (select SORT_ID from SYS_SORT_PROMOTE where SORT_ID=#{sortId}) b
on s.SORT_ID = b.SORT_ID
  WHERE
    b.SORT_ID IS NULL
    AND s.SORT_NAME = 'Must-Listen Classics'
  limit 1;

In the above SORT_ID=#{sortId}, sortId passes in the ID value that needs to be excluded in the SORT_ID field. When performing a left outer join, the field to be filtered (SORT_ID) is used as the join condition. Finally, add b.SORT_ID IS NULL to the where condition to filter out the related data in the table.

Write down the essay here to record the optimization process.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • MYSQL IN and EXISTS optimization examples
  • Optimization of not in and minus in MySQL
  • Detailed Analysis of or, in, union and Index Optimization in MySQL
  • Implementation of MySQL select in subquery optimization
  • Optimized record of using IN data volume in Mysql

<<:  A practical record of troubleshooting a surge in Redis connections in Docker

>>:  Detailed tutorial on installing Python 3 virtual environment in Ubuntu 20.04

Recommend

7 interesting ways to achieve hidden elements in CSS

Preface The similarities and differences between ...

Sliding menu implemented with CSS3

Result:Implementation code: <!DOCTYPE html>...

Solution to MySQL Chinese garbled characters problem

1. The Chinese garbled characters appear in MySQL...

Detailed examples of replace and replace into in MySQL into_Mysql

MySQL replace and replace into are both frequentl...

Deploy Nginx+Flask+Mongo application using Docker

Nginx is used as the server, Mongo is used as the...

MySQL 5.7.18 Installer installation download graphic tutorial

This article records the detailed installation tu...

Detailed explanation of the two modes of Router routing in Vue: hash and history

hash mode (default) Working principle: Monitor th...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

Install CentOS 7 on VMware14 Graphic Tutorial

Introduction to CentOS CentOS is an enterprise-cl...

Five delay methods for MySQL time blind injection

Five delay methods for MySQL time blind injection...

Web development tutorial cross-domain solution detailed explanation

Preface This article mainly introduces the cross-...

MySQL 5.6 compressed package installation method

There are two installation methods for MySQL: msi...

64-bit CentOs7 source code installation mysql-5.6.35 process sharing

First install the dependent packages to avoid pro...

Detailed explanation of custom configuration of docker official mysql image

In order to save installation time, I used the of...

How to implement insert if none and update if yes in MySql

summary In some scenarios, there may be such a re...