mysql表:2M,5W行:
Mysql连接:
- <?php
-
-
$link = mysql_connect('localhost', 'lcms2', 'abcd');
-
mysql_select_db("lcms2");
-
mysql_query("SET NAMES 'utf8'");
-
-
$time = microtime(1);
-
-
for ($i=1; $i<10000;$i ) {
-
$sql = "SELECT * from `cl_translation` WHERE `varName`='my group'";
-
-
$result = mysql_query($sql,$link);
-
if (!$result) {
-
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
-
exit;
-
}
-
-
$rows = array();
-
-
while ($row = mysql_fetch_assoc($result)) {
-
$rows[] = $row;
-
-
}
}
-
-
$intval_time = microtime(1) - $time;
-
echo $intval_time . "
";
-
-
var_dump($rows);
-
-
-
?>
PDO连接代码:
- <?php
-
-
$dsn = "mysql:dbname=lcms2;host=localhost";
-
$user = "lcms2";
-
$password = "abcd";
-
-
try{
-
#$dbh = new pdo($dsn, $user, $password,array(PDO::ATTR_PERSISTENT => true));
-
$dbh = new pdo($dsn, $user, $password);
-
} catch (PDOException $e){
-
echo "Connetion failed: " . $e->getMessage();
-
}
-
$dbh->exec("SET NAMEs 'utf8'");
-
$ph = array();
-
-
$time = microtime(1);
-
-
$sql = "SELECT * from `cl_translation` WHERE `varName`=:string";
-
-
$ph[0] = $dbh->prepare($sql);
-
for ( $i=1; $i<10000; $i ) {
-
-
$ph[0]->execute(array('string'=>'my group'));
-
-
}
-
$result = $ph[0]->fetchAll();
-
-
$intval_time = microtime(1) - $time;
-
echo $intval_time . "
";
-
-
var_dump($result);
-
-
-
?>
pdo直接查询,不用prepare语句:
- <?php
-
$time = microtime(1);
-
-
-
$dsn = "mysql:dbname=lcms2;host=localhost";
-
$user = "lcms2";
-
$password = "lsrzlcms";
-
-
try{
-
#$dbh = new pdo($dsn, $user, $password,array(PDO::ATTR_PERSISTENT => true));
-
$dbh = new pdo($dsn, $user, $password);
-
} catch (PDOException $e){
-
echo "Connetion failed: " . $e->getMessage();
-
}
-
$dbh->exec("SET NAMEs 'utf8'");
-
$ph = array();
-
-
for ( $i=1; $i<10000; $i ) {
-
-
$sql = "SELECT * from `cl_translation` WHERE `varName`='my group'";
-
-
$ph = $dbh->query($sql);
-
if ( !$ph ) {
-
var_dump($dbh->errorInfo());
-
die("Error when execute query" );
-
}
-
}
-
-
$result = $ph->fetchAll();
-
-
$dbh=NULL;
-
-
$intval_time = microtime(1) - $time;
-
echo $intval_time . "
";
-
-
var_dump($result);
-
-
-
?>
连接结果:
第一次: 0.014165878295898s
0.00062799453735352
0.00014495849609375
改变id值
0.00049090385437012
0.00012493133544922
改变prepare的值:
0.051913022994995
0.00035595893859863
0.00033116340637207
mysql驱动:
0.00032401084899902
0.00030779838562012
mysql 1000次
0.30312514305115
0.30235195159912
mysql 10000次:
3.0247800350189
3.043329000473
3.0260350704193
pdo 10000次,包括prepare内的循环:
0.89934897422791
0.89384698867798
pdo 10000次,不包括prepare内的循环,只循环excute:
0.79944610595703
0.79354596138
pdo 不用statements语句,直接查询:
0.877690076828
0.81276917457581
0.82689189910889
---------------------------------------------------
害死人了,for后忘了加大括号,导致第一次进行mysql_select时,没有执行数据库查询!!!
pdo的效率还是很高的,终于松了一口气!
另外:
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY好像没有作用。保持连接对pdo查询影响不大,只影响连接。
阅读(5690) | 评论(0) | 转发(0) |