What are the potential pitfalls of using COUNT() and DISTINCT in PHP MySQL queries?

Using COUNT() and DISTINCT together in MySQL queries can lead to performance issues, especially with large datasets. This combination can result in slower query execution times and increased resource consumption. To optimize queries using COUNT() and DISTINCT, consider using a subquery or restructuring the query to avoid unnecessary duplication of data.

$query = "SELECT COUNT(DISTINCT column_name) AS count FROM table_name";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$count = $row['count'];
echo "Count: " . $count;