アプリ版:「スタンプのみでお礼する」機能のリリースについて

現在、以下のようなperlのスクリプトを作っているのですが、mysqlへの接続,切断は以下の場合、どちらにした方がいいのでしょうか?

(1)の場合
use DBI;
$dsn="DBI:mysql:database=dbname:host=localhost";
$dbh=DBI->connect($dsn,'user','pass');
$sth = $dbh->prepare("SELECT no,title,name,date,host From `table1` where no='1'");
$sth->execute;
$sth->fetchrow_array;
$sth->finish;
$sth2 = $dbh->prepare("SELECT no,title,name,date,host From `table2` where no='1'");
$sth2->execute;
$sth2->fetchrow_array;
$sth2->finish;
$dbh->disconnect;

(2)の場合
use DBI;
$dsn="DBI:mysql:database=dbname:host=localhost";
$dbh=DBI->connect($dsn,'user','pass');
$sth = $dbh->prepare("SELECT no,title,name,date,host From `table1` where no='1'");
$sth->execute;
$sth->fetchrow_array;
$sth->finish;
$dbh->disconnect;

$dsn="DBI:mysql:database=dbname:host=localhost";
$dbh=DBI->connect($dsn,'user','pass');
$sth2 = $dbh->prepare("SELECT no,title,name,date,host From `table2` where no='1'");
$sth2->execute;
$sth2->fetchrow_array;
$sth2->finish;
$dbh->disconnect;

(1)の場合と(2)の場合の違いは、(1)の場合、最初にデータベースに接続して、一番最後に切断する事で、(2)の場合、毎回データベースへの接続と切断を行う事が違います。

(2)の場合、毎回接続と切断を行うので、2回ほどではあまり変わらないかもしれないのですが、もし10回とか接続と切断を行うとかなり負荷が高くなるような気がするのですが、他のHPなどを拝見すると接続をしたら切断を行うように癖をつけるようにした方がいいと書かれていたりします。

どちらの方が正しいやり方というか、負荷がすくなく使えるのでしょうか?

A 回答 (1件)

参考URLをご覧になってみて下さい。



再接続しなおすことのコストを考えると、最初に接続・最後に切断が良いようです。

参考URL:http://homepage3.nifty.com/hippo2000/perltips/db …
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!