php+mysql中預存程序效能簡單比較

來源:互聯網
上載者:User

簡介:這是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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.