最近在轉移網站到阿裡雲RDS數據庫服務器的時候遇到一些負載瓶頸方面的問題,需要在調整MySQL參數、修改Drupal設置、添加索引等工作的時候,查看某些MySQL語句執行時間的變化,所以搞了一個很小的PHP程序來循環檢測:
<?php ini_set('display_errors', 'On'); error_reporting(E_ALL); $serverName = 'xxx.mysql.rds.aliyuncs.com'; $userName = 'un'; $password = 'pw'; $databaseName = 'db'; $link = mysql_connect("$serverName","$userName","$password") or die("unable to connect to msql server: " . msql_error()); mysql_select_db("$databaseName", $link) or die("unable to select database 'db': " . msql_error()); $sql = " SELECT s.source, s.context, t.translation, t.language FROM locales_source s LEFT JOIN locales_target t ON s.lid = t.lid AND t.language = 'zh-hans' WHERE s.textgroup = 'default' AND s.version = '7.38' AND LENGTH(s.source) < '75'; "; //$sql = "show tables;"; FOR ($i = 0; $i <= 10; $i++) { $time_start = microtime(); $result = mysql_query("$sql"); $time_end = microtime(); $time_long = ($time_end - $time_start)*1000; if (!$result) { die('query failed: '); } print_r($result); //$row = mysql_fetch_array($result); //print_r ($row); //while ($row = mysql_fetch_array($result)){ //print_r($row); //} print "time_long = $time_long ms\n"; sleep(1); } ?>
在這個程序中設置了1秒為循環,每隔一秒進行一次MySQL查詢操作,并輸出所花費的時間。如果我們在進行了某項優化後,看到這個數值有了明顯的變化,那就說明這次修改找到了問題所在。
评论