1)PHP 寫入mysql數據的代碼php
- <?php
- $mysqli=mysqli_connect("10.1.0.169","root","123456","test","3306");
- class runtime
- {
- var $StartTime = 0;
- var $StopTime = 0;
- function get_microtime()
- {
- list($usec, $sec) = explode(' ', microtime());
- return ((float)$usec + (float)$sec);
- }
- function start()
- {
- $this->StartTime = $this->get_microtime();
- }
- function stop()
- {
- $this->StopTime = $this->get_microtime();
- }
- function spent()
- {
- return round(($this->StopTime - $this->StartTime) * 1000, 1);
- }
- }
- $runtime= new runtime;
- $runtime->start();
- for($i=1;$i<100000;$i++){
- $ins = "insert into `test_order` (`id`)vlaue ('$i')";
- mysqli_query($mysqli,$ins);
- }
- $runtime->stop();
- echo "insert sucess! 頁面執行時間: ".$runtime->spent()." 毫秒\n";
- mysqli_close($mysqli);
- ?>
結果:mysql
使用PHP 給mysql 插入10W條數據時的狀態:執行時間是70秒git
使用PHP 給mysql 插入50W條數據時的狀態:執行時間是335秒github
2) 安裝phpredis模塊redis
1. 在https://github.com/owlient/phpredis 下載nicolasff-phpredis-2.1.3-167-ga5e53f1.zipsql
解壓 unzip –q nicolasff-phpredis-2.1.3-167-ga5e53f1.zip網絡
2. cd phprediside
/usr/local/php/bin/phpize 這個phpize是安裝php模塊的php-fpm
./configure --with-php-config=/usr/local/php/bin/php-config測試
make && make install
確認是否生成redis擴展庫:
/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
在php.ini中添加extension=redis.so
從新加載php /etc/init.d/php-fpm reload
3. php代碼測試
<?php
$redis = new Redis();
$redis->connect('10.1.0.169',6379);
$redis->set('test','hello world!');
echo $redis->get('test');
?>
輸出hello world!
3) PHP 寫入redis數據的代碼
rpush.php代碼:
$redis = new Redis();
$redis->connect('10.1.0.169',6379);
$redis->rpush("order","$ins");
lpop.php代碼:
$num=$redis->llen("order");
while ($num) {
$ins=$redis->lpop("order");
mysqli_query($mysqli,$ins);
$num=$redis->llen("order");
}
結論:
使用PHP 給redis插入10W條數據時的狀態:
執行時間是24秒
使用redis 給mysql插入10W條數據時的狀態:
執行時間是120秒
使用PHP 給redis 插入50W條數據時的狀態:
執行時間是124秒
使用redis 給mysql插入50W條數據時的狀態:
執行時間是735秒
4) 經過測試,驗證redis和mysql每秒的插入效率:
測試結論:在網絡rtt平均時間爲0.2ms,value值大小平均是1kb的狀況下,
redis 插入隊列數據的效率大約爲每秒 4200個value值
同等條件下,mysql插入數據的效率大約爲每秒 700個,一樣大小數據的insert操做