What are the potential pitfalls of using DISTINCT in PHP queries?

Using DISTINCT in PHP queries can potentially slow down the query performance as it requires the database to scan and remove duplicates from the result set. To avoid this pitfall, it is recommended to first optimize the query to eliminate duplicates at the source before using DISTINCT in the query.

// Example of optimizing query to eliminate duplicates at the source
$query = "SELECT column1, column2 FROM table GROUP BY column1, column2";
$result = mysqli_query($connection, $query);

// Fetch and display results
while ($row = mysqli_fetch_assoc($result)) {
    echo $row['column1'] . " - " . $row['column2'] . "<br>";
}