What are the best practices for managing database table capacity in PHP applications?

When managing database table capacity in PHP applications, it is important to regularly monitor the size of the tables and optimize queries to improve performance. Implementing proper indexing, partitioning large tables, and archiving old data are common strategies to manage table capacity effectively.

// Example of archiving old data from a table to manage capacity
$query = "CREATE TABLE archived_data AS SELECT * FROM original_table WHERE created_at < DATE_SUB(NOW(), INTERVAL 1 YEAR)";
$result = mysqli_query($connection, $query);

if($result){
    // Data archived successfully, now you can delete it from the original table
    $deleteQuery = "DELETE FROM original_table WHERE created_at < DATE_SUB(NOW(), INTERVAL 1 YEAR)";
    $deleteResult = mysqli_query($connection, $deleteQuery);

    if($deleteResult){
        echo "Old data successfully archived and deleted.";
    } else {
        echo "Error deleting old data.";
    }
} else {
    echo "Error archiving old data.";
}