2012-10-14 52 views
1

我有一個表格,如id,type,data等等......我需要編寫一個web服務腳本PHP應該將數據插入到表,然後選擇最後的10條同類型和HTTP主體返回。如何編寫一個PHP + MySQL Web服務來執行每秒1000個請求(INSERT和SELECT)每秒

1000個實例需要每秒訪問此腳本。

我的問題是最好的辦法是什麼?

這是我的代碼。我不知道這是很好的做法,可能與如何使它更好的例子做...

// Connect to mysql server 
$link = mysql_connect(HOST, USER, PASSWORD); 
if(!$link) { 
    die('Could not connect to server: ' . mysql_error()); 
} 

// Select database 
$db = mysql_select_db(DATABASE); 
if(!$db) { 
    die('Cannot use the database'); 
} 
mysql_set_charset('charset=utf8', $link); 

// Insert data 
$query = "INSERT INTO `mytable` (`id`, `type`, `data`) VALUES ('$id', '$type', '$data')"; 
mysql_query($query); 

// Select data 
$query = "SELECT * FROM `mytable` WHERE `type`='$type' ORDER BY DESC LIMIT 10"; 
$result = mysql_query($query); 

while($row = mysql_fetch_assoc($result)) { 

    // Create xml to return 

} 
+1

開始刪除你的mysql的使用並移動到mysqli –

回答

2

你的PHP腳本本身(忽略任何安全問題)會不會是這個比例最多的一個問題你期望的負載。您的基礎設施將此事使這劇本,你想到還是沒有負載工作之間的差別。

您的MySQL實例能否每秒支持1000個新連接?您的Web服務器可以每秒運行該腳本1000次嗎?

解決這個問題的唯一方法是基準它找出你在哪裏,你可以支持哪些負載?如果你不能支持它,那麼你需要找到的瓶頸,但我懷疑它會永遠是這個腳本。


響應您的評論更新,最好的辦法是模擬你期望的負載,沒有一點擔心,如果你的安裝可以處理這個問題。如果它無法處理它,那麼你需要縮小這個問題的範圍。

首先,下載像Apache JMeter的工具。有一些教程可以幫助您設置模擬來嘗試您的設置。

從這裏您可以計算出問題的範圍,如果您可以支持超過您期望的流量,您可能不必擔心。如果你能只是支持它,或者你是一個很長的路要走,你需要找到它們阻止你達到這個目標的瓶頸。

通過分別測試系統的各個部分來縮小問題的範圍,這就是您回答諸如Web服務器或數據庫支持的連接數量等問題的地方。一旦確定了瓶頸,怎樣從處理更多的流量停止你的原因,你就可以有一個你需要做什麼更好的主意。

使用Apache JMeter的:
http://www.davegardner.me.uk/blog/2010/09/23/effective-load-testing-with-apache-jmeter/
使用mysqlslap加載測試MySQL:
http://dev.mysql.com/doc/refman/5.1/en/mysqlslap.html

Bootnote:有可能會是一個很多東西需要學習......希望這將讓你開始你可以相對輕鬆地支持你正在尋找的負載。如果不是,它需要大量的關於Web服務的可擴展體系結構的閱讀,您正在使用的系統的高級配置以及耐心等等。


使用PDO

//Connect to mysql server 
$host = ""; // Your database host name 
$driver = "mysql"; 
$database = ""; // Your database; 
$user = ""; // The user for the database; 
$password = ""; // The password for the database; 

// Create a DSN string from the above parameters 
$dsn = "$driver:host=$host;dbname=$database"; 

// Create a connection you must have the pdo_mysql 
// extension added to your php.ini 
try { 
    $db = new PDO($dsn, $user, $password); 

// The connection could not be made 
} catch(PDOException $ex) 
    die("Could not connect to server: {$ex->getMessage()}"); 
} 

// Prepare an insert statement 
$stmt = $db->prepare(" 
    INSERT INTO `mytable` (`id`, `type`, `data`) 
    VALUES  (:id, :type, :data) 
");  

// I'm guessing at the types here, use the reference 
// http://php.net/manual/en/pdo.constants.php to 
// select the right datatypes. Using parameter binding 
// you ensure that the value is converted and escaped 
// correctly for the database 
$stmt->bindParam(":id", $id, PDO::PARAM_INT); 
$stmt->bindParam(":type", $type, PDO::PARAM_STR); 
$stmt->bindParam(":data", $data, PDO::PARAM_LOB); 

// Execute the insert statement 
$stmt->execute(); 

// Prepare a select data 
$stmt = $db->prepare(" 
    SELECT * 
    FROM  `mytable` 
    WHERE `type` = :type 
    ORDER BY `id` DESC 
    LIMIT 10 
"); 

// Again bind the parameters 
$stmt->bindParam(":type", $type, PDO::PARAM_STR); 

// Execute the select statement 
$stmt->execute(); 

// There are different ways that fetch can return 
// a row, the web page above lists all of the 
// different types of fetch as well. In this case 
// we are fetching the rows as associative arrays 
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 

    // Write XML for rows 
} 

// Finalise and output XML response 

使用PDO:
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

+0

並使用PDO而不是mysql –

+0

感謝您的回覆。我是新來的PHP。你能幫我嗎?我如何檢查「MySQL實例每秒鐘支持1000個新連接,並且Web服務器每秒運行這個腳本1000次」。另外如何開始PDO – user1725661

+0

再次感謝您的回覆 – user1725661

0

作爲一個小的改進,你可以做到這一切在一個單一的查詢:

$query= " 
    INSERT INTO `mytable` (`id`, `type`, `data`) 
    VALUES ('$id', '$type', '$data') 
    ;  
    SELECT * 
    FROM `mytable` 
    WHERE `type` = '$type' 
    ORDER BY DESC 
    LIMIT 10 
    ;"; 
+0

感謝您的回覆。我不能這樣做,因爲通過以不同的實例每秒運行此腳本1000次插入數據 – user1725661