Are there any potential performance issues when using count() in SQL queries to check for user existence?

Using count() in SQL queries to check for user existence can potentially cause performance issues, especially when dealing with large datasets. Instead of counting all rows that match the query criteria, it is more efficient to use EXISTS or LIMIT 1 to check for the existence of at least one matching row. This way, the query can stop processing as soon as a match is found, improving performance.

<?php
// Connect to the database
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');

// Check for user existence using EXISTS
$stmt = $pdo->prepare("SELECT EXISTS(SELECT 1 FROM users WHERE username = :username)");
$stmt->execute(['username' => 'example_user']);
$userExists = $stmt->fetchColumn();

if ($userExists) {
    echo 'User exists';
} else {
    echo 'User does not exist';
}
?>