为什么我的猫鼬查询这么慢? (约30秒)

时间:2019-05-15 03:56:23

标签: javascript node.js mongodb mongoose

我有一条路线,希望用户ID返回该用户在应用程序上编写的报价数组。如果用户只写了几个提议,则查询返回的速度相当快,但是一旦达到10个提议,查询就会变得异常缓慢。

我已经研究过索引编制,但是不确定如何去做。

这是路线:

 router.get('/offers', passport_admin.authenticate('admin-rule', { 
 session: false }), (req, res) => {
 const userId = req.query.id
 Offer.find({user: userId})
     .then(offers => {
     if(offers){
       if(offers.length > 0){
        res.json(offers)
        } else {
         res.json({msg: 'This user has not made any offers yet.'})
        }

        } else {
      res.status(400).json({msg: 'User not found'})
        }

      })
   });

这是我希望为该特定用户返回的12个商品对象之一。我知道它很大,但仍然...:

  [ { _id: 5cad4493fe8ff00017f0d9ab,
    user: 5cad438cfe8ff00017f0d9aa,
    propertyAddress: '56585 South SW 368 ST& SW 214 AV Groves #6A2',
     propertyDetails:
     { privateRemarks:
    'This property is a trial property to test the SimplyRETS. Private agent remarks will be included in this field for use in the SimplyRETS REST API. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
   showingContactName: null,
   property: [Object],
   mlsId: 1005160,
   showingContactPhone: null,
   terms:
    'Submit,Cash,Cash To Existing Loan,Cash To New Loan,Assumable,Exchange,Owner May Carry,Owner Will Carry,Trade',
   showingInstructions:
    'The showing instructions for this trial property are brought to you by the SimplyRETS team. This field will include any showing remarks for the given listing in your RETS feed. Enjoy!',
   office: [Object],
   leaseTerm: null,
   disclaimer:
    'This information is believed to be accurate, but without warranty.',
   address: [Object],
   agreement: 'Exclusive Agency',
   listDate: '1995-01-05T21:20:35.957061Z',
   agent: [Object],
   modified: '2003-05-30T17:37:32.270926Z',
   school: [Object],
   photos: [Array],
   listPrice: 21445988,
   listingId: '51337707',
   mls: [Object],
   geo: [Object],
   tax: [Object],
   coAgent: [Object],
   sales: [Object],
   leaseType: 'Modified Gross',
   virtualTourUrl: null,
   remarks:
    'This property is a trial property to test the SimplyRETS. This field will include remarks or descriptions from your RETS feed intended for public view. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
   association: [Object],
   favorite: false },
offerDetails:
 { fullNameErr: '',
   emailErr: '',
   phoneErr: '',
   downpaymentErr: '',
   preApprovalErr: '',
   homeInspectionErr: '',
   appraisalErr: '',
   loanErr: '',
   escrowErr: '',
   msg: '',
   fullName: 'Ryan Brennan',
   cobuyerName: 'Ryan Brennan',
   email: 'Ryansbrennan444@gmail.com',
   phone: '215.760.4950',
   cobuyerEmail: 'eatshit@gmail.com',
   priceErr: '',
   price: '$21,000,000',
   mortgage: true,
   downpayment: '$1,000,000',
   preApproval: [Object],
   homeInspectionDays: '45',
   appraisalDays: '10',
   loanDays: '45',
   escrowDays: '10',
   proofOfFunds: [Object],
   proofOfFundsErr: '' },
date: 2019-04-10T01:19:15.739Z,
__v: 0 } ]

这是我的报价模型:

 const OfferSchema = new Schema({

  user: {
    type: Schema.Types.ObjectId,
    ref: 'users'
 },
    propertyAddress: {
    type: String,
    required: true
 },
    propertyDetails: {
    type: Object,
    required: true,
 },
    offerDetails: {
    type: Object,
    required: true
 },
    date: {
    type: Date,
    default: Date.now
 }
});

我希望此查询在30秒内返回。

1 个答案:

答案 0 :(得分:0)

  

我已经研究过索引编制,但是不确定如何处理。

如果没有索引,这将不可避免地变慢。一个好的开始是为user创建一个索引,因为这就是您要查询的(Offer.find({user: userId}))。如果您已连接到mongo,则db.Offer.createIndex({user: 1})之类的东西应该是一个好的开始。

This page in the docs是学习索引的一个很好的起点。