【问题标题】:MySQL Outperforming RedisMySQL 优于 Redis
【发布时间】:2018-04-04 16:20:17
【问题描述】:

我正在尝试为 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,我不明白为什么。 我正在查询 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 存储脚本:

$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 存储脚本,用于不指向哈希的列表:

$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 服务器。

如果我遗漏了什么使这个问题成为有效问题,请告诉我。

【问题讨论】:

  • 如果您不提供Minimal, Complete, and Verifiable example,我们将无法评估您的测试。
  • 现在怎么样@Dinei
  • 您的问题是什么?如果您谈论的是直接从 MySQL 中选择与从 Redis 中获取...我并不感到惊讶。当存储需要一次又一次发生的计算密集型计算时,redis 的价值就发挥了作用。
  • 我不明白为什么我们将时间浪费在 redis 作为缓存后端。我的印象是从 mysql 后端切换到内存中的 redis 解决方案是我们被告知要采取的方法。
  • 你说你正在使用 redislabs。我猜这是一个云托管的 redis 实例?您是否将其与本地 MySQL 实例进行比较?也许是网络往返延迟让云 redis 显得很慢。

标签: mysql redis benchmarking


【解决方案1】:

基于上述 cmets,AWS 中托管的远程 Redis 实例的网络延迟大于本地 MySQL 实例的延迟。

自然地,当您为每个 Redis 请求添加 40-100 毫秒的延迟时(取决于您的应用与托管 Redis 在互联网上的距离),它会使 Redis 在总请求时间方面显得更慢。

【讨论】:

  • 正确。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 2012-01-13
  • 1970-01-01
  • 2013-01-14
  • 2017-01-18
  • 2013-05-26
  • 1970-01-01
  • 2013-12-13
相关资源
最近更新 更多