MongoDB、Elasticsearch分组统计性能比较

环境参数

CentOS 7.6 虚拟机 4核 8GB

Elasticsearch 5.6.16

MongoDB 5.0.9

数据结构

{
    
    
  "userId": "rkyao",
  "searchId": "6e1c409ed7484a6a8a795e750bef9e2d",
  "content": "南南西海山",
  "stime": "2022-09-03T21:13:54+0800"
}

性能测试

content字段分组并统计每组数量。

Elasticsearch

数据量500万条,取前10组 第一次查询耗时500ms以内,后续查询3ms

GET /characteristic_company/search_record_one_month/_search
{
    
    
    "size": 0,
    "aggs": {
    
    
      "GROUP_BY_CONTENT": {
    
    
        "terms": {
    
    
          "field": "content.keyword",
          "size": 10,
          "order": {
    
    
            "_count": "desc"
          }
        }
      }
    }
}
MongoDB

数据量500万条,取前10组,第一次查询耗时84s,后续查询无明显提升。

数据量100万条,取前10组,第一次查询耗时7.49s,后续查询无明显提升。

db.characteristic_company_search_one_month.aggregate([
    {
    
    
        '$group': {
    
    '_id': {
    
    'content':'$content'}, 'score': {
    
    '$sum': 1}}
    },
    {
    
    $sort:{
    
    "score":-1}},
    {
    
    $limit:10}
], {
    
     allowDiskUse: true })

PS

  1. MongoDB查询时content字段加不加索引性能无明显差距。
  2. MongoDB分组为内存操作会导致内存溢出,需设置allowDiskUse为true。

猜你喜欢

转载自blog.csdn.net/yaorongke/article/details/126715819