PDO slow connection to MySQL database

Discussion in 'MySQL' started by qwikad.com, Aug 15, 2024.

  1. #1
    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):
     
    qwikad.com, Aug 15, 2024 IP
  2. GreenHost.Cloud

    GreenHost.Cloud Active Member

    Messages:
    471
    Likes Received:
    34
    Best Answers:
    3
    Trophy Points:
    73
    #2
    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.
     
    GreenHost.Cloud, Aug 21, 2024 IP
  3. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    7,274
    Likes Received:
    1,696
    Best Answers:
    31
    Trophy Points:
    475
    #3
    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.
     
    qwikad.com, Aug 21, 2024 IP
  4. GreenHost.Cloud

    GreenHost.Cloud Active Member

    Messages:
    471
    Likes Received:
    34
    Best Answers:
    3
    Trophy Points:
    73
    #4
    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!
     
    GreenHost.Cloud, Aug 22, 2024 IP