How can the "ORDER BY" function impact the performance of a PHP script when querying a large database?

Using the "ORDER BY" function in a query can impact the performance of a PHP script when querying a large database because it requires the database to sort the results before returning them. To improve performance, it is recommended to add an index on the column being sorted by to help the database engine retrieve the data more efficiently.

// Example of adding an index on a column in a MySQL database table
$sql = "ALTER TABLE table_name ADD INDEX index_name (column_name)";
$result = mysqli_query($connection, $sql);

if($result) {
    echo "Index added successfully";
} else {
    echo "Error adding index: " . mysqli_error($connection);
}