In PHP, what are some common strategies for optimizing the process of deleting records from multiple tables in a database to improve performance and efficiency?

When deleting records from multiple tables in a database in PHP, one common strategy for optimizing performance is to use transactions. By wrapping the delete operations in a transaction, you can ensure that all deletions are either completed successfully or rolled back in case of an error, preventing partial deletions and maintaining data integrity.

<?php
// Establish a database connection
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Begin a transaction
$pdo->beginTransaction();

try {
    // Perform delete operations on multiple tables
    $pdo->exec("DELETE FROM table1 WHERE condition");
    $pdo->exec("DELETE FROM table2 WHERE condition");

    // Commit the transaction
    $pdo->commit();
    echo "Records deleted successfully.";
} catch (Exception $e) {
    // Roll back the transaction in case of an error
    $pdo->rollBack();
    echo "Error deleting records: " . $e->getMessage();
}
?>