I tried some of the solutions I found online (like changing localhost to 127.0.0.1) but nothing sped it up. Any ideas why it is slow? On average it's 2 seconds slower compared to my mysqli connections. try{ $pdo = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass); } catch(PDOException $e) { echo ''.$e->getMessage(); } Code (markup):
One possibility could be that PDO has some default settings that might not be optimized for your specific use case. You might want to check your network settings or consider tweaking some PDO attributes for performance enabling persistent connections or adjusting the fetch mode.
I'm just getting familiar with pdo, it seems to me that it's not the connection per se that made it slower, it's the fetch() as in sql code here.... while ($row = $stmt->fetch()) { something here.... } I read upon it and there are quite a few people complaining about this. It works great in some instances, but then when you need to fetch a large amount of data from a large DB it's not as "spunky" as mysqli would've been. But I have no real proof, except to do some back and forth testing, and I can't help but notice some sluggishness.
It makes sense that fetch performance can vary depending on the amount of data being handled. If you’re dealing with large datasets, consider using `fetchAll()` to grab all the rows at once or experimenting with different fetch modes that might better suit your needs. Testing a few approaches will help you find the sweet spot for speed!