M201: MongoDB Performance chapter 4 CRUD Operations学习记录

M201: MongoDB Performance chapter 4 CRUD Operations学习记录

运行环境

操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4

Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data

课后问题

lab 4.1 Equality,sort,range

In this lab you’re going to use the equality, sort, range rule to determine which index best supports a given query.

Given the following query:

db.accounts.find( { accountBalance : { $gte : NumberDecimal(100000.00) }, city: "New York" } ).sort( { lastName: 1, firstName: 1 } )

Which of the following indexes best supports this query with regards to the equality, sort, range rule.

Choose the best answer:

  • { city: 1, lastName: 1, firstName: 1, accountBalance: 1}
  • { lastName: 1, firstName: 1, city: 1, accountBalance: 1}
  • { accountBalance: 1, city: 1, lastName: 1, firstName: 1}
  • { lastName: 1, firstName: 1, , accountBalance: 1city: 1}

解析

按照索引构建原则:

Equality->sort->range

答案为:

  • { city: 1, lastName: 1, firstName: 1, accountBalance: 1}

lab 4.2 Aggregation Performance

Download Handouts:

restaurants.json.zip

For this lab, you’re going to create an index so that the following aggregation query can be executed successfully.

After importing the restaurants dataset, without any indexes:

$ mongoimport -d m201 -c restaurants --drop restaurants.json

If you attempt to run the following query you’ll receive an error.

db.restaurants.aggregate([
  { $match: { stars: { $gt: 2 } } },
  { $sort: { stars: 1 } },
  { $group: { _id: "$cuisine", count: { $sum: 1 } } }
])
{
  "ok": 0,
  "errmsg": "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.",
  "code": 16819,
  "codeName": "Location16819"
}

Identify why this error is occuring, and build an index to resolve the issue.

In the text box below, submit the index that resolves the issue.

For example, if you ran db.restaurants.createIndex({ foobar: 1 }) to fix the error, then you’d enter { foobar: 1 } into the text box.

Note: The index should be ascending in nature.

解答

下载json包restaurants.json.zip
并解压至路径E:\MongoDB\m201\chapter_4_crud_optimization\

开启守护进程mongod:

C:\Users\Shinelon>e:

E:\>MongoDB\Server\3.4\bin\mongod.exe --dbpath MongoDB\data

导入json:

C:\Users\Shinelon>e:

E:\>MongoDB\Server\3.4\bin\mongoimport.exe -d m201 -c restaurants --drop MongoDB\m201\chapter_4_crud_optimization\restaurants.json\restaurants.json
2018-04-23T11:05:37.032+0800    connected to: localhost
2018-04-23T11:05:37.062+0800    dropping: m201.restaurants
2018-04-23T11:05:40.020+0800    [####....................] m201.restaurants     26.2MB/144MB (18.2%)
2018-04-23T11:05:43.021+0800    [#########...............] m201.restaurants     56.6MB/144MB (39.4%)
2018-04-23T11:05:46.020+0800    [##############..........] m201.restaurants     87.1MB/144MB (60.6%)
2018-04-23T11:05:49.021+0800    [###################.....] m201.restaurants     117MB/144MB (81.7%)
2018-04-23T11:05:51.631+0800    [########################] m201.restaurants     144MB/144MB (100.0%)
2018-04-23T11:05:51.631+0800    imported 1000000 documents
E:\>MongoDB\Server\3.4\bin\mongo.exe
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.6
Server has startup warnings:
2018-04-22T19:47:03.593-0700 I CONTROL  [initandlisten]
2018-04-22T19:47:03.593-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-22T19:47:03.593-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-22T19:47:03.593-0700 I CONTROL  [initandlisten]
> use m201
switched to db m201
> db.restaurants.aggregate([
...   { $match: { stars: { $gt: 2 } } },
...   { $sort: { stars: 1 } },
...   { $group: { _id: "$cuisine", count: { $sum: 1 } } }
... ])
assert: command failed: {
        "ok" : 0,
        "errmsg" : "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.",
        "code" : 16819,
        "codeName" : "Location16819"
} : aggregate failed
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1

2018-04-23T11:10:20.427+0800 E QUERY    [thread1] Error: command failed: {
        "ok" : 0,
        "errmsg" : "Sort exceeded memory limit of 104857600 bytes, but did not opt in to external sorting. Aborting operation. Pass allowDiskUse:true to opt in.",
        "code" : 16819,
        "codeName" : "Location16819"
} : aggregate failed :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
doassert@src/mongo/shell/assert.js:16:14
assert.commandWorked@src/mongo/shell/assert.js:370:5
DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
@(shell):1:1

错误原因是Mongodb排序超过了内存限制

建索引{“stars”:1}:

> db.restaurants.createIndex({"stars":1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}

测试:

> db.restaurants.aggregate([   { $match: { stars: { $gt: 2 } } },   { $sort: { stars: 1 } },   { $group: { _id: "$cuisine", count: { $sum: 1 } } } ])
{ "_id" : "Seafood", "count" : 13744 }
{ "_id" : "Australian", "count" : 13719 }
{ "_id" : "Cajun", "count" : 13691 }
{ "_id" : "Belgian", "count" : 13835 }
{ "_id" : "Japanese", "count" : 13861 }
{ "_id" : "Malaysian", "count" : 13514 }
{ "_id" : "French", "count" : 13498 }
{ "_id" : "Indian", "count" : 13683 }
{ "_id" : "Asian", "count" : 13829 }
{ "_id" : "Vegetarian", "count" : 13778 }
{ "_id" : "Portuguese", "count" : 13658 }
{ "_id" : "Philippine", "count" : 13616 }
{ "_id" : "Irish", "count" : 13599 }
{ "_id" : "Moroccan", "count" : 13680 }
{ "_id" : "Sushi", "count" : 13647 }
{ "_id" : "Italian", "count" : 13733 }
{ "_id" : "Russian", "count" : 13752 }
{ "_id" : "Korean", "count" : 13437 }
{ "_id" : "Peruvian", "count" : 13656 }
{ "_id" : "Lebanese", "count" : 13731 }
Type "it" for more

所以答案为

{ stars: 1 }

猜你喜欢

转载自blog.csdn.net/sunbocong/article/details/80048448