简体   繁体   中英

Should you open/close a database connection inside of a loop or outside of a loop?

Using PDO with prepared statements (I learned from here: http://www.kitebird.com/articles/php-pdo.html ) I'm using a function that opens a database connection:

function testdb_connect ()
{
   $dbh = new PDO("mysql:host=localhost;dbname=test", "testuser", "testpass");
   return ($dbh);
}

I have a for loop that iterates and inserts rows in a table. Should I open the connection before the loop and close it after the looping construct

$dbh = testdb_connect();
for($i=0; $i<$number_of_values; $i++){
    //Insert rows
}
$dbh = NULL   ;

Or should I open and close it within the loop for each insert statement? What are the pros and cons of each method?

for($i=0; $i<$number_of_values; $i++){
    $dbh = testdb_connect();
    //Insert rows
    $dbh = NULL   ;
}

由于打开或关闭数据库或文件的任何调用都存在大量开销,因此出于效率考虑,我会在循环之前打开数据库,并在循环完成时关闭数据库。

Open the connection once for the whole script . There's a certain overhead in opening a connection, and there's absolutely no advantage in re-opening it. So to be as efficient as possible, open it once at the start of the script and close it at the end. Opening and closing it inside a loop is nonsense.

as a general rule, open as early as possible, and close as late as possible.

to directly answer your question, outside the loop.

If you look at any framework, you will notice that a connection is opened very early in the bootstrapping process, and closed in an exiting process (if at all). Apache and PHP are smart enough to automatically terminate all open connections when a script finishes.

You should open and close the database connection outside your loop. Opening and closing the database connection everytime in the loop will cause lot of overhead and can potentially slow down your site.

You should ideally open the database connection just once in your page and close it once. So you can open the connection at the very start of the page/script and close it at the end.

Opening and closing a connection is resource consuming and therefore it shouldn't be done in a loop, besides you only need one connection to carry out multiple queries.

There's also the insertion method: rather than running a query with every iteration it would make sense to build a single query and then run that at the end for example

create table test(
    testField VARCHAR(12)
)

function rowsToDb($rows)
{
    /*Define base of query*/
    $query =  "INSERT INTO test (testField) VALUES ";
    /*Iterate through rows concatenating new insert values*/
    foreach($rows as $row){
        $query .= "('". $row['testField']. "'),";
    }
    testdb_connect();
    /*Remove rogue "," and execute query*/
    mysql_query(substr($query, 0, strlen($query) - 1));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM