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

Detailed explanation of how to use $props, $attrs and $listeners in Vue

Table of contents background 1. Document Descript...

Example of usage of keep-alive component in Vue

Problem description (what is keep-alive) keep-ali...

Summary of various common join table query examples in MySQL

This article uses examples to describe various co...

Discussion on the Issues of Image Button Submission and Form Repeated Submission

In many cases, in order to beautify the form, the ...

A brief discussion on the application of Html web page table structured markup

Before talking about the structural markup of web...

How to encapsulate axios in Vue project (unified management of http requests)

1. Requirements When using the Vue.js framework t...

IE6 space bug fix method

Look at the code: Copy code The code is as follows...

Vue sample code for easily implementing virtual scrolling

Table of contents Preface Rolling principle accom...

How to build ssh service based on golang image in docker

The following is the code for building an ssh ser...

How to use CURRENT_TIMESTAMP in MySQL

Table of contents Use of CURRENT_TIMESTAMP timest...

How to install Maven automatically in Linux continuous integration

Unzip the Maven package tar xf apache-maven-3.5.4...