簡介:這是php+mysql中預存程序效能簡單比較的詳細頁面,介紹了和php,有關的知識、技巧、經驗,和一些php源碼等。
class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=334797' scrolling='no'>
PHP+MYSQL中,使用MYSQL的預存程序其實是很好的,而且效率還會快點的,
在這裡,小結複習下MYSQL的用法吧,使用的是PDO
1
//不使用預存程序
$time = microtime(TRUE);
$mem = memory_get_usage();
$dsn = 'pgsql:host=localhost;dbname=gonzalo;port=5432';
$user = 'user';
$password = 'password';
$conn = new PDO($dsn, $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$stmt = $conn->prepare('delete from web.tbltest');
$stmt->execute();
$stmt = $conn->prepare('INSERT INTO web.tbltest (field1) values (?)');
foreach (range(0,1000) as $i) {
$stmt->execute(array($i));
}
$conn->commit();
print_r(array('memory' => (memory_get_usage() - $mem) / (1024 * 1024), 'seconds' => microtime(TRUE) - $time));
2 使用預存程序
預存程序的寫法:
CREATE OR REPLACE FUNCTION web.method1()
RETURNS numeric AS
$BODY$
BEGIN
DELETE FROM web.tbltest;
FOR i IN 0..1000 LOOP
INSERT INTO web.tbltest (field1) values (i);
END LOOP;
RETURN 1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
$time = microtime(TRUE);
$mem = memory_get_usage();
$dsn = 'pgsql:host=localhost;dbname=gonzalo;port=5432';
$user = 'user';
$password = 'password';
$conn = new PDO($dsn, $user, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->beginTransaction();
$stmt = $conn->prepare('SELECT web.method1()');
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$out = $stmt->fetchAll();
$conn->commit();
print_r(array('memory' => (memory_get_usage() - $mem) / (1024 * 1024), 'seconds' => microtime(TRUE) - $time));
可以看到結果對比如下:
without stored procedures
memory: 0.0023880004882812
seconds: 0.31109309196472
with stored procedures
memory: 0.0020713806152344
Seconds: 0.065021991729736
可以看到,用了預存程序的效能還是不錯的
“php+mysql中預存程序效能簡單比較”的更多相關文章 》
愛J2EE關注Java邁克爾傑克遜視頻站JSON線上工具
http://biancheng.dnbcw.info/php/334797.html pageNo:10