How can PHP be optimized for complex database calculations to improve performance?

When dealing with complex database calculations in PHP, one way to improve performance is by optimizing the SQL queries used to fetch and manipulate data. This can involve using indexes, limiting the number of rows returned, and ensuring that the queries are efficient. Additionally, consider caching results where possible to reduce the number of database calls.

// Example of optimizing SQL query for complex database calculations
$query = "SELECT SUM(column_name) FROM table_name WHERE condition = 'value'";
$result = mysqli_query($connection, $query);

if($result){
    $row = mysqli_fetch_assoc($result);
    $sum = $row['SUM(column_name)'];
    mysqli_free_result($result);
}