Are there any best practices for optimizing DESC LIMIT queries in PHP?

When using DESC LIMIT queries in PHP, it is important to optimize the query to improve performance. One way to do this is by adding an index on the column being sorted in descending order. This can help speed up the query execution by allowing the database to quickly locate the relevant rows.

// Example of optimizing a DESC LIMIT query in PHP
// Assuming we have a table named 'users' with a column 'created_at' that we want to sort in descending order

// Add an index on the 'created_at' column
$query = "CREATE INDEX idx_created_at ON users (created_at DESC)";
$result = mysqli_query($connection, $query);

// Execute the DESC LIMIT query
$query = "SELECT * FROM users ORDER BY created_at DESC LIMIT 10";
$result = mysqli_query($connection, $query);

// Process the query results
while ($row = mysqli_fetch_assoc($result)) {
    // Process each row
}

// Don't forget to clean up after executing the query
mysqli_free_result($result);