我是 redis 的新手,从以下链接开始
http://www.aspieschool.com/wiki/index.php?title=Redis_vs_MySQL_(Benchmarks)
这表明My sql write
花费的时间比redis write
redis 读取的时间短mysql read
。但就我而言,情况正好相反。
我正在使用以下代码写入 mysql :
<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('unable to connect ');
}
mysql_select_db("redistest",$con);
for($i=0;$i<100000;$i++){
$query = "insert into `test`(`name`) values ('Rohit')";
mysql_query($query);
}
?>
这大约需要 3 分钟,在 redis 中我使用的代码如下:
require 'predis-0.8/autoload.php';
$single_server = array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 15
);
$multiple_servers = array(
array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 15,
'alias' => 'first',
),
array(
'host' => '127.0.0.1',
'port' => 6380,
'database' => 15,
'alias' => 'second',
),
);
// simple set and get scenario
$time_start = microtime(true);
$client = new Predis\Client($single_server);
$client->flushall();
for($i=0;$i<5000;$i++){
echo $client->lPush('key8', 'C');
echo "<br>";
}
//$retval = $client->get('rohit');
//$ass = array();
//$ass = $client->lrange('key8',0,-1);
//foreach($ass as $ass)
//echo "<br/>$ass";
$time_end = microtime(true);
$diff = $time_end - $time_start;
echo "<br/>".$diff;
//var_dump($retval);
它大约需要 1.292163848877 秒:
从 mysql 读取:
<?php
$time_start = microtime(true);
$con=mysql_connect("localhost","root","");
if(!$con)
{
die('unable to connect ');
}
mysql_select_db("redistest",$con);
$sql="select * from `test`";
$result = mysql_query($sql);
while($results = mysql_fetch_array($result))
{
foreach($results as $results)
$results."<br/>";
}
$time_end = microtime(true);
$diff = $time_end - $time_start;
echo "<br/>".$diff;
?>
大约需要 0.040173053741455 秒
并使用 redis :
$time_start = microtime(true);
$client = new Predis\Client($single_server);
//$client->flushall();
//for($i=0;$i<5000;$i++){
//echo $client->lPush('key8', 'C');
//echo "<br>";
//}
//$retval = $client->get('rohit');
$ass = array();
$ass = $client->lrange('key8',0,-1);
foreach($ass as $ass)
//echo "<br/>$ass";
$time_end = microtime(true);
$diff = $time_end - $time_start;
echo "<br/>".$diff;
大约需要 0.13404297828674 秒。谁能建议我哪里出错了,?