Delete Records That Are Not Referenced

This section provides a tutorial example on bulk deletion for records that are not referenced by another table.

There are two options to delete records that are not referenced by another table. For example, deleting all orders that has no order lines can be done by:

1. Using "NOT IN (...)":

DELETE from Orders where ID NOT IT (select OrderID from OrderLines);

2. Using "LEFT JOIN":

DELETE Orders from Orders o LEFT JOIN OrderLines l ON o.ID = l.OrderID 
  where l.CaseID is null;

Let's compare try both options and see their performance difference.

1. Using "NOT IN (...)" to delete records that are not referenced. I see that deleting 6% of 687533 rows takes 1.80 seconds.

mysql> select count(*) from Orders
|   687533 |

mysql> drop Table Orders_Test;
mysql> create Table Orders_Test like Orders;
mysql> insert into Orders_Test select * from Orders; 

mysql> select count(*) from Orders_Test 
  where ID NOT IN (select OrderID from OrderLines);
|    46829 |

mysql> select 46829/687533;
|   0.0681 |

mysql> DELETE from Orders_Test 
  where ID NOT IN (select OrderID from OrderLines);
Query OK, 46829 rows affected (1.80 sec)

mysql> drop Table Orders_Test;

2. Using "LEFT JOIN" to delete records that are not referenced. I see that deleting 6% of 687533 rows takes 2.04 seconds.

mysql> create Table Orders_Test like Orders;
mysql> insert into Orders_Test select * from Orders; 

mysql> select count(*) from Orders_Test o 
    -> LEFT JOIN OrderLines l ON o.ID = l.OrderID where l.OrderID is null
|    46829 |

mysql> DELETE o from Orders_Test o 
    -> LEFT JOIN OrderLines l ON o.ID = l.OrderID where l.OrderID is null
Query OK, 46829 rows affected (2.04 sec)

mysql> select count(*) from Orders_Test 
  where ID NOT IN (select CaseID from CaseMolecules);
|        0 |

mysql> drop Table Orders_Test;

Conclusion: the "LEFT JOIN" option takes slightly more execution time than the "NOT IN" option.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 PHP Programs and MySQL Server

 Perl Programs and MySQL Servers

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Window Functions for Statistical Analysis

 Use Index for Better Performance

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 System, User-Defined and Stored Procedure Variables

 MySQL Server Administration

 Storage Engines in MySQL Server

 InnoDB Storage Engine - Primary and Secondary Indexes

 Performance Tuning and Optimization

Bulk Changes on Large Tables

 General Guidelines on Bulk Changes

 Bulk Delete with a Simple Condition

Delete Records That Are Not Referenced

 Reset AUTO_INCREMENT Value on Large Tables

 MySQL Server on macOS

 Installing MySQL Server on Linux

 Connection, Performance and Second Instance on Linux

 Archived Tutorials

 References

 Full Version in PDF/EPUB