which of these 2 is better?

Discussion in 'MySQL' started by JEET, Jan 27, 2010.

  1. #1
    Hi,
    Is it better to connect to db at starting of script and keep sending queries in the script then finally close at end, or is it better to connect at each query?

    I'm currently using the first method, but today while coding another script, found the second way working quite fast. Although the queries sent were 1-2, but the script worked quite fast. I always thought that connecting each time would require more time and slow things.

    Anyone else has some experience with this?
    Thanks :)
     
    JEET, Jan 27, 2010 IP
  2. ClassicPlus

    ClassicPlus Peon

    Messages:
    77
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    i always connect at the beginning and close at end, all works perfect. i dont know which method is faster.
     
    ClassicPlus, Jan 27, 2010 IP
  3. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #3
    Hi JEET,

    It seems that you post the question and the answer ;)
    Sometimes the answer reveals itself by the try-and-errors method.
    By the way, there is no need to close connection - ending script will do it for you (if you didn't use pconnect function).
    Regards,
    Nick
     
    koko5, Jan 27, 2010 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    Ok, I used the same (connect and disconnect per query) in another script and tested with 30 different queries. To my surprise, it's working faster now.
    0.039-0.045 seconds to execute 30 queries, build a webpage, and send it to browser. (not a cached page)
    Connecting at beginning and closing at end is taking 0.172 seconds to load the exact same page.
    Thanks :)
     
    JEET, Jan 27, 2010 IP