MySQL 优于 Redis

标签 mysql redis benchmarking

我正在尝试为 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 服务器。

如果我遗漏了任何使这个问题成为有效问题的信息,请告诉我。

最佳答案

根据以上评论,托管在 AWS 中的远程 Redis 实例的网络延迟大于本地 MySQL 实例的延迟。

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

关于MySQL 优于 Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49655888/

相关文章:

many-to-many - 如何在redis中建立多对多关系

Ruby attr_accessor 与 getter/setter 基准测试 : why is accessor faster?

c - C 语言 OpenMP 基准测试

performance - 我如何在 Go 中编写基准测试脚本来测量 ops/sec

mysql - 从表中的所有列中选择所有非空值

php - 在两个 mysql 表上填充数据时遇到问题

php - 为什么php不在mysql中存储数据?

ruby-on-rails - 如何从 Redis 缓存中缓存的 JSON API 访问对象值?

mysql - 如何在 SQL 中加入一对多关系中的单行?

memory - Redis Pub Sub channel 内存