Я пытаюсь создать сценарий тестов для операции mysql и операции redis.
Вот что я пробовал:
1./ List of comment ids with a separate hash of comment JSON Data mapped to comment id
2./ List of comments json data
3./ Sorted set of comments with ranking and json data as mapped value
По какой-то причине mysql не выполняет redis, и я не понимаю, почему. Im запрашивает 100 записей.
Вот мои операции (разделенные попытками):
1./ $client->rpush($commentId); $client->hmset($commentId,$data);
2./ $client->rpush($jsonData);
3./ $client->zadd("comments",$i,$jsonData);
Вот мой тестовый сценарий:
$client = new Predis\Client($conf);
$st=microtime(true);
// sorted set solution
$dat=$client->zrange("comments",0,100);
// list solution
//$dat=$client->lrange("comments",0,100);
$ft=microtime(true);
$overall=$ft-$st;
echo "REDIS=>".$overall."\n";
$sta=microtime(true);
$st=mysqli_query($dbh,"select SQL_NO_CACHE * from comments where status>0 order by createdate desc limit 0,100");
while($r=mysqli_fetch_assoc($st)){
$dd=$r;
}
$fta=microtime(true);
$overall=$fta-$sta;
echo "MYSQL=>".$overall."\n";
Вот мой скрипт redis для отсортированных наборов:
$st=mysqli_query($dbh,"select SQL_NO_CACHE * from comments where status>0 order by createdate desc LIMIT 100");
$i=1;
while($r=mysqli_fetch_assoc($st)){
$client->zadd("comments",$i,json_encode($r));
$i++;
}
Вот мой скрипт redis store для списка:
$st=mysqli_query($dbh,"select SQL_NO_CACHE * from comments where status>0 order by createdate desc LIMIT 100");
$i=1;
while($r=mysqli_fetch_assoc($st)){
$key="comment:$id";
$client->rpush("comments",$key);
foreach($r as $k=>$v){
$client->hset($key,$k,$v);
}
$i++;
}
Вот мой скрипт redis store для списка без указания хэша:
$st=mysqli_query($dbh,"select SQL_NO_CACHE * from comments where status>0 order by createdate desc LIMIT 100");
$i=1;
while($r=mysqli_fetch_assoc($st)){
$key="comment:$id";
$client->rpush("comments",json_encode($r));
}
Вот схема БД:
CREATE TABLE 'comments' (
'commentid' int(11) NOT NULL AUTO_INCREMENT,
'parentid' int(11) DEFAULT '0',
'refno' int(11) DEFAULT '0',
'createdate' int(11) DEFAULT '0',
'remoteip' varchar(80) DEFAULT '',
'fingerprint' varchar(50) DEFAULT '',
'locid' int(11) DEFAULT '0',
'clubid' int(11) DEFAULT '0',
'profileid' int(11) DEFAULT '0',
'userid' int(11) DEFAULT '0',
'global' int(11) DEFAULT '0',
'official' int(11) DEFAULT '0',
'legacyuser' int(11) DEFAULT '0',
'mediaid' int(11) DEFAULT '0',
'status' int(11) DEFAULT '1',
'comment' varchar(4000) DEFAULT '',
'likes' int(11) DEFAULT '0',
'dislikes' int(11) DEFAULT '0',
'import' int(11) DEFAULT '0',
'author' varchar(50) DEFAULT '',
PRIMARY KEY ('commentid'),
KEY 'comments_locid' ('locid'),
KEY 'comments_userid' ('userid'),
KEY 'idx_legacyusers' ('legacyuser'),
KEY 'profile_index' ('profileid'),
KEY 'comments_createdate' ('createdate'),
KEY 'compound_for_comments' ('locid','global','status'),
KEY 'global' ('global'),
KEY 'status' ('status'),
KEY 'locid_status' ('locid','status'),
KEY 'global_status' ('global','status')
) ENGINE=InnoDB
Мы используем Redislabs как наш сервер redis.
Если мне не хватает чего-либо, чтобы сделать этот вопрос действительным, пожалуйста, дайте мне знать.
Основываясь на вышеприведенных комментариях, латентность сети удаленного экземпляра Redis, размещенная в AWS, была больше, чем латентность локального экземпляра MySQL.
Естественно, когда вы добавляете 40-100 мс латентности для каждого запроса Redis (в зависимости от того, насколько далеко от вашего приложения находится приложение Redis), это делает Redis более медленным с точки зрения общего времени запроса.