How can SQL queries be optimized for better performance when working with bot detection scripts in PHP?

When working with bot detection scripts in PHP, optimizing SQL queries for better performance can be achieved by using indexes on columns frequently used in the queries, avoiding unnecessary joins and selecting only the necessary columns, and utilizing query caching to reduce database load.

// Example of optimizing SQL query for bot detection script in PHP
// Adding an index on the 'user_agent' column
$query = "CREATE INDEX idx_user_agent ON users(user_agent)";
$result = mysqli_query($connection, $query);

// Avoiding unnecessary joins and selecting only necessary columns
$query = "SELECT user_id, user_agent FROM users WHERE user_agent = 'bot'";
$result = mysqli_query($connection, $query);

// Utilizing query caching to reduce database load
$query = "SELECT SQL_CACHE user_id, user_agent FROM users WHERE user_agent = 'bot'";
$result = mysqli_query($connection, $query);