Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb system performance (1)

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Create a fixed collection

Db.createCollection ("my_collection", {capped:true,size:10000,max:5})

Turn an ordinary set into a fixed set

Db.runCommand ({convertTocapped: "test", size:10000})

GridFS is a mechanism for storing large binaries in MongoDB, and GridFS can be used to store the following files:

Store huge files, such as videos, high-definition pictures, etc.

Benefits:

GridFS can be used to simplify requirements.

GridFS will directly take advantage of the established replication or sharding mechanism, and fault recovery and extension are easy.

GridFS can avoid problems with the file system where users upload content.

GridFS does not produce disk fragmentation

GridFS uses two tables to store data:

Files contains metadata objects

Chunks binary blocks that contain some other relevant information

[root@a2 bin] #. / mongofiles put mongosniff.tar.gz

[root@a2 bin] #. / mongo

MongoDB shell version: 2.4.3

Connecting to: test

> show tables

Fs.chunks

Fs.files

System.indexes

> db.fs.files.find ()

{"_ id": ObjectId ("5196601b171c296452c1f9f1"), "filename": "mongosniff.tar.gz", "chunkSize": 262144, "uploadDate": ISODate ("2013-05-17T16:51:40.843Z"), "md5": "5067d9e1c4a0e41b93f204a72ec52794", "length": 7171743}

>

Fs.files stores information about files, while fs.chunks stores files in binary format.

View storage file information

[root@a2 bin] #. / mongofiles list

Connected to: 127.0.0.1

Mongosniff.tar.gz 7171743

Get the storage file:

[root@a2 bin] #. / mongofiles get mongosniff.tar.gz

System performance optimization:

Build an index

When you create a collection, the system automatically builds an index of _ id and cannot delete it

> db.system.indexes.find ()

{"v": 1, "key": {"_ id": 1}, "ns": "test.c1", "name": "_ id_"}

{"v": 1, "key": {"_ id": 1}, "ns": "test.c2", "name": "_ id_"}

Set up a general index

> db.c2.ensureIndex ({age:1}, {true}); (if the amount of data is large, you can use background,age:1 to indicate that the indexes are sorted in ascending order of age, and-1 is in descending order)

< /p>

> db.system.indexes.find ()

{"v": 1, "key": {"_ id": 1}, "ns": "test.c1", "name": "_ id_"}

{"v": 1, "key": {"_ id": 1}, "ns": "test.c2", "name": "_ id_"}

{"v": 1, "key": {"age": 1}, "ns": "test.c2", "name": "age_1", "background": true}

Create a unique index

> db.c2.ensureIndex ({name:1}, {unique:true})

If the name field of the re-entered data is duplicated, an error will be reported, as follows:

> db.c2.insert ({name: "user0"})

E11000 duplicate key error index: test.c2.$name_1 dup key: {: "user0"}

Delete all indexes

> db.c2.dropIndex ()

Delete the index of the name field

> db.c2.dropIndex ({name:1})

MongoDB provides an explain command to let us know how the system handles query requests

> db.c2.find ({age: {$gt:26}}) .explain ()

{

"cursor": "BtreeCursor age_1"

"isMultiKey": false

"n": 3, the number of data returned

"nscannedObjects": 3

"nscanned": 3, the number of documents scanned because the age field is indexed

"nscannedObjectsAllPlans": 3

"nscannedAllPlans": 3

"scanAndOrder": false

"indexOnly": false

"nYields": 0

"nChunkSkips": 0

"millis": 1, time consuming (milliseconds)

"indexBounds": {Index used

"age": [

[

twenty-six,

1.7976931348623157e+308

]

]

}

"server": "a1pur27017"

}

MongoDB Database Profiler is a slow query log function, which can be used as a basis for us to optimize the database.

Enable the profile function:

1) > db.setProfilingLevel (2)

{"was": 0, "slowms": 100, "ok": 1}

(2) [root@a1 bin] # / mongod-- dbpath=/usr/local/mongodb/data/-- logpath=/usr/local/mongodb/dblogs-profile=2-- fork

Add-profile parameter

Optimization scheme:

1: create index db.posts.ensureIndex ({ts:1})

2: limit the number of returned results db.posts.find () .sort ({ts:-1}) .limit (10)

3: query the fields used. Do not query all fields.

4: adopt cappedcollection

Capped Collections is more efficient than ordinary Collections in reading and writing.

5: adopt Profiling

Mongosniff this tool can monitor from the bottom which commands are sent to MongoDB for execution.

Executing this command may result in the following error

[root@a1 bin] #. / mongosniff-help

. / mongosniff: error while loading shared libraries: libpcap.so.0.9: cannot open shared object file: No such file or directory

Solution:

Yum-y install libpcap-devel

Since the system automatically installs version 1.0, we also have to establish a soft connection

Ln-s / usr/lib64/libpcap.so.1.0.0 / usr/lib64/libpcap.so.0.9

How to use it:

[root@a1 bin] #. / mongosniff-source NET lo

Sniffing... 27017

You can output these data to a log file, so you can keep the history of all database operations, which will be a great contribution to the later performance analysis and security audit.

Mongostat

This tool can quickly view the system of a group of running MongoDB instances

Counting information, you also need to open a client for command operation:

The usage is as follows:

[root@localhost bin] #. / mongostat

View the status of the database:

> db.stats ()

{

"db": "test"

"collections": 6

"objects": 234

"avgObjSize": 263.77777777777777

"dataSize": 61724

"storageSize": 1077248

"numExtents": 6

"indexes": 4

"indexSize": 32704

"fileSize": 67108864

"nsSizeMB": 16

"dataFileVersion": {

"major": 4

"minor": 5

}

"ok": 1

}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report