首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MongoDB集料性能

MongoDB集料性能
EN

Stack Overflow用户
提问于 2021-12-29 03:56:36
回答 1查看 37关注 0票数 0

我有两个收藏品,一个是出价,另一个是拍卖行。我能够获得投标在客户明智的计数出价收集内近一百万条记录。而拍卖行收藏有500000条记录。我需要与mongodb中的快速获取相同的结果。

这是几乎29秒的响应时间。但我需要时间来得到回应

代码语言:javascript
运行
复制
{ $match: { customer: '00000000823026' } },
        { $group: {
                _id: '$auctioncode',
                data: {
                    $last: '$$ROOT'
                }
            }
        },
        {
            $lookup: {
                from: 'auctions',
                let: { auctioncode: '$_id' },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $and: [{ $eq: ['$_id', '$$auctioncode'] }],
                            },
                        },
                    },
                ],
                as: 'auction',
            },
        },
        { $match: { auction: { $exists: true, $not: { $size: 0 } } } },
        {
            $addFields: {
                _id: '$data._id',
                auctioncode: '$data.auctioncode',
                amount: '$data.amount',
                customer: '$data.customer',
                customerName: '$data.customerName',
                maxBid: '$data.maxBid',
                stockcode: '$data.stockcode',
                watchlistHidden: '$data.watchlistHidden',
                winner: '$data.winner'
            }
        },
        {
            $match: {
                $and: [
                    {
                        // to get only RUNNING auctions in bid history
                        'auction.status': { $ne: 'RUNNING'},
                        // to filter auctions based on status
                        // tslint:disable-next-line:max-line-length
                    },
                ],
            },
        },
        { $sort: { 'auction.enddate': -1 } },
        { $count: 'totalCount'}

目前的结果是totalCount 2640

如何优化和需要在mongodb中找到一种性能变化的方法

EN

回答 1

Stack Overflow用户

发布于 2021-12-29 05:00:03

如果您所需要的只是结果的计数,下面的代码将进行更多的优化。

索引customer键以获得更好的执行时间。

注意:,如果您使用的是MongoDB版本的>= 5.0,您可以使用$lookuppipeline方法,因为它使用的是与较低版本不同的if索引。

代码语言:javascript
运行
复制
db.collection.aggregate([
  {
    $match: {
      customer: '00000000823026'
    }
  },
  {
    $group: {
      _id: '$auctioncode',
      data: {
        $last: '$$ROOT'
      }
    }
  },
  {
    $lookup: {
      from: 'auctions',
      localField: '_id',
      foreignField: '_id',
      as: 'auction',
    },
  },
  {
    $match: {
      // auction: {$ne: []},
      // to get only RUNNING auctions in bid history
      'auction.status': { $ne: 'RUNNING'},
      // to filter auctions based on status
      // tslint:disable-next-line:max-line-length
  // {
  //   $addFields: {  <-- Not Required
  //     _id: '$data._id',
  //     auctioncode: '$data.auctioncode',
  //     amount: '$data.amount',
  //     customer: '$data.customer',
  //     customerName: '$data.customerName',
  //     maxBid: '$data.maxBid',
  //     stockcode: '$data.stockcode',
  //     watchlistHidden: '$data.watchlistHidden',
  //     winner: '$data.winner'
  //   }
  // },
  // { $sort: { 'auction.enddate': -1 } },  <-- Not Required
  { $count: 'totalCount'}
], {
  allowDiskUse: true
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70514974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档