How can the SELECT query be optimized for better performance when dealing with large datasets?
When dealing with large datasets, the SELECT query can be optimized for better performance by using indexes on columns frequently used in the WHERE clause, avoiding the use of SELECT * and instead specifying only the necessary columns, and limiting the number of rows returned using the LIMIT clause.
// Example of optimizing a SELECT query for better performance
$sql = "SELECT column1, column2 FROM table_name WHERE column3 = :value LIMIT 100";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':value', $value);
$stmt->execute();
$results = $stmt->fetchAll();