Atikh's DBA blog
  • Home
  • Oracle
  • MySQL
  • MongoDB
  • PostgreSQL
  • Snowflake
  • About Me
  • Contact Us
Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

MongoDB : Basic Administration

 Atikh Shaikh     MongoDB     No comments   

After going through CRUD operations on MongoDB database, We will take look at some more basic MongoDB Administration

Getting MongoDB database-related information
If you want to know databases available in the system, execute "show dbs" command, it will list out all the databases

> show dbs;
admin      0.000GB
config     0.000GB
local      0.016GB
techno_db  0.001GB
> 

If you want to know in which database, you are currently execute db 
> db
techno_db
> 

if you are curious about what are the collections available under the database, just execute show collections or show tables

>show collections;
tech_large
techno_col
> 

or
> show tables;
tech_large
techno_col
> 

The command db.stats() will give you all the stats about the database 

> db.stats();
{
        "db" : "techno_db",
        "collections" : 2,
        "views" : 0,
        "objects" : 17000,
        "avgObjSize" : 37.64705882352941,
        "dataSize" : 640000,
        "storageSize" : 335872,
        "numExtents" : 0,
        "indexes" : 2,
        "indexSize" : 208896,
        "fsUsedSize" : 587198464,
        "fsTotalSize" : 10693378048,
        "ok" : 1,
        "operationTime" : Timestamp(1549877990, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1549877990, 1),
                "signature" : {
                        "hash" : BinData(0,"OFOjmV2iLyqywB6fMf4bdiUZ5CU="),
                        "keyId" : NumberLong("6636938165653340162")
                }
        }
}
> 

If you want to know stats about only particular collections then that is also possible, execute db..stats() command
  
> db.techno_col.stats();
{
        "ns" : "techno_db.techno_col",
        "size" : 70000,
        "count" : 2000,
        "avgObjSize" : 35,
        "storageSize" : 86016,
        "capped" : false,
        "wiredTiger" : {
                "metadata" : {
                        "formatVersion" : 1
                },
                "creationString" :
…
…
…
},
        "nindexes" : 1,
        "totalIndexSize" : 61440,
        "indexSizes" : {
                "_id_" : 61440
        },
        "ok" : 1,
        "operationTime" : Timestamp(1549878100, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1549878100, 1),
                "signature" : {
                        "hash" : BinData(0,"eBwJSS4K5vUHyZJKT54h2pduSEc="),
                        "keyId" : NumberLong("6636938165653340162")
                }
        }
}
> 

Getting Help
MongoDB provides by default build command called db.help(), it lists out commonly used methods for operating database
we can get the type command on any collections using db..help()

> db.help();
DB methods:
        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [just calls db.runCommand(...)]
        db.aggregate([pipeline], {options}) - performs a collectionless aggregation on this database; returns a cursor
        db.auth(username, password)
        db.cloneDatabase(fromhost) - deprecated
        db.commandHelp(name) returns the help for the command
        db.copyDatabase(fromdb, todb, fromhost) - deprecated
        db.createCollection(name, {size: ..., capped: ..., max: ...})
        db.createView(name, viewOn, [{$operator: {...}}, ...], {viewOptions})
        db.createUser(userDocument)
        db.currentOp() displays currently executing operations in the db
...
...
...
        db.shutdownServer()
        db.stats()
        db.version() current version of the server
> 
> db.techno_col.help();
DBCollection help
       db.techno_col.find().help() - show DBCursor help
       db.techno_col.bulkWrite( operations, )- ulk execute write operations, optional parameters are: w, wtimeout, j
        db.techno_col.count( query = {}, ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
...
...
...
        db.techno_col.unsetWriteConcern( ) - unsets the write concern for writes to the collection
        db.techno_col.latencyStats() - display operation latency histograms for this collection
>  

There is one method to increase the speed of your administration by simply using the tab key on the keyboard
Just type function or procedure name and press tab, it will all available functions under it

> db.techno_col.get
db.techno_col.getCollection(        db.techno_col.getIndexSpecs(    db.techno_col.getName(             db.techno_col.getShardVersion(
db.techno_col.getDB(                db.techno_col.getIndexes(        db.techno_col.getPlanCache(         db.techno_col.getSlaveOk(
db.techno_col.getFullName(          db.techno_col.getIndices(        db.techno_col.getQueryOptions(  db.techno_col.getSplitKeysForChunks(
db.techno_col.getIndexKeys(           db.techno_col.getMongo(        db.techno_col.getShardDistribution(   db.techno_col.getWriteConcern(

To get the definition of any method can be found using not providing brackets at the time of invocation of the method
for example

> db.techno_col.getIndexes
function (filter) {
    var res = this._getIndexesCommand(filter);
    if (res) {
        return res;
    }
    return this._getIndexesSystemIndexes(filter);
}
> 

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit

MongoDB : Indexing and explain Plan

 Atikh Shaikh     MongoDB     No comments   

In this article we are going to discuss about indexing and query optimization using output of explain plan in MongoDB.
  • explain() is important in debugging or optimizing queries. When database receives query, it must plan out how to execute it; this is called query plan,
  • EXPLAIN describes query paths and allows developers to diagnose slow operations by determining which indexes query has used
  • MongoDB has its own version of EXPLAIN that provides the same service. We will try to understand this in details
  • Apply this the to, the query we have executed in previous article (MongoDB: Creating large document)
MongoDB Index and explain(), MongoDB explain plan, mongoDB indexing, create large documents, createIndex

i.e. finding documents with num value greater than 1991 from techno_col collection, below is the output

>  db.techno_col.find({num: {"$gt" :1991}});
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd3c"), "num" : 1992 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd3d"), "num" : 1993 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd3e"), "num" : 1994 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd3f"), "num" : 1995 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd40"), "num" : 1996 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd41"), "num" : 1997 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd42"), "num" : 1998 }
{ "_id" : ObjectId("5c5d66a510f3108c3c83dd43"), "num" : 1999 }
>
  
Now get the explain plan for this query

> db.techno_col.find({num: {"$gt" :1991}}).explain("executionStats");
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "techno_db.techno_col",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "num" : {
                                "$gt" : 1991
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "num" : {
                                        "$gt" : 1991
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 8,
                "executionTimeMillis" : 2,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 2000,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "num" : {
                                        "$gt" : 1991
                                }
                        },
                        "nReturned" : 8,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 2002,
                        "advanced" : 8,
                        "needTime" : 1993,
                        "needYield" : 0,
                        "saveState" : 15,
                        "restoreState" : 15,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 2000
                }
        },
        "serverInfo" : {
                "host" : "test_mongo_srvr",
                "port" : 27017,
                "version" : "4.0.4",
                "gitVersion" : " nogitversion"
        },
        "ok" : 1,
        "operationTime" : Timestamp(1549631830, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1549631830, 1),
                "signature" : {
                 "hash" : BinData(0,"hKf4Ck7scgSFsRaM8qv8+hoJm4g="),
                 "keyId" : NumberLong("6636938165653340162")
                }
        }
}
>

We will try to understand the terms used in the output and understand the explain
we can see query engine has to scan the entire collection, all 2000 documents (docsExamined), to return only 8 rows as output (nReturned) of the query.
The value of totalKeysExamined field shows the number of indexes entries scanned, which is zero, these all stats show that this is not an efficient query
This collection techno_col needs an index to be created in order to make query faster and more efficient. you can use CreateIndex() method to create an index on the num key
First check if any indexes is already present, we can default index in _id column is already present, we will create new indexes, and then again we will check the count of indexes.

> db.techno_col.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "techno_db.techno_col"
        }
]
> 

> db.techno_col.createIndex({num : 1});
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> 

> db.techno_col.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "techno_db.techno_col"
        },
        {
                "v" : 2,
                "key" : {
                        "num" : 1
                },
                "name" : "num_1",
                "ns" : "techno_db.techno_col"
        }
]
> 

The collection techno_col has now two indexes one is the standard _id_ index that is automatically built for every collection. The second one is we created on num. If we do not provide a name, MongoDB automatically assigns one.
Now we will try to run explain() on the same query as we did earlier, we will be surprised to see the difference.

> db.techno_col.find({num: {"$gt" :1991}}).explain("executionStats");
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "techno_db.techno_col",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "num" : {
                                "$gt" : 1991
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "num" : 1
                                },
                                "indexName" : "num_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "num" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "num" : [
                                                "(1991.0, inf.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 8,
                "executionTimeMillis" : 1,
                "totalKeysExamined" : 8,
                "totalDocsExamined" : 8,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 8,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 9,
                        "advanced" : 8,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 8,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 8,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 9,
                                "advanced" : 8,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "num" : 1
                                },
                                "indexName" : "num_1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "num" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "num" : [
                                                "(1991.0, inf.0]"
                                        ]
                                },
                                "keysExamined" : 8,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : " test_mongo_srvr ",
                "port" : 27017,
                "version" : "4.0.4",
                "gitVersion" : "nogitversion"
        },
        "ok" : 1
}
> 

from the above renewed explain plan, we can observe below values for different parameters
docsExamined - 8
nReturned - 8
totalKeysExamined - 8

With these stats, we can say the query has been executed very quickly and efficiently. But we have to remember, that indexes are not free they consume some space from storage.
In this way we have learned, to generate query explain plan and create indexes and their use in real-time.
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Instagram
  •  Pin
  •  linkedin
  •  reddit
Newer Posts Older Posts Home

Author

Atikh Shaikh
View my complete profile

Categories

  • MongoDB (18)
  • Oracle 12c (30)
  • Oracle12cR2 New Feature (3)
  • PostgreSQL (21)
  • RMAN (10)
  • Snowflake (8)
  • mysql (23)
  • oracle (75)

Blog Archive

  • ►  2018 (38)
    • ►  November (25)
    • ►  December (13)
  • ►  2019 (33)
    • ►  January (15)
    • ►  February (6)
    • ►  March (2)
    • ►  April (5)
    • ►  May (5)
  • ►  2020 (5)
    • ►  April (1)
    • ►  May (2)
    • ►  July (2)
  • ►  2021 (8)
    • ►  June (3)
    • ►  July (3)
    • ►  August (1)
    • ►  December (1)
  • ►  2022 (33)
    • ►  May (3)
    • ►  June (10)
    • ►  July (3)
    • ►  August (4)
    • ►  September (8)
    • ►  October (3)
    • ►  November (2)
  • ►  2023 (14)
    • ►  February (1)
    • ►  April (5)
    • ►  May (2)
    • ►  June (1)
    • ►  September (1)
    • ►  October (1)
    • ►  December (3)
  • ►  2024 (5)
    • ►  January (2)
    • ►  March (3)
  • ►  2025 (7)
    • ►  March (1)
    • ►  April (3)
    • ►  May (2)
    • ►  August (1)
  • ▼  2026 (2)
    • ►  January (1)
    • ▼  May (1)
      • Oracle Patching Process and Commands

Popular Posts

  • ORA-29283: invalid file operation: unexpected "LFI" error (1509)[29437]
    I was trying to export the schema in my windows PC, it got stuck with below error    C:\Users\shaik\Videos\technodba exp>expdp userid...
  • Oracle : Starting with RMAN Commands
    In previous article we have discussed introduction to RMAN , Now will learn RMAN commands and taking backup of database and understanding i...
  • Table Dropped in Oracle Database ? Worry not, we can recover it
    There are a couple of options we can use to recover dropped tables in 19c Oracle database version  Restore table from Recycle Bin (assume ...
  • Oracle Dataguard Broker Configuration (DGMGRL)
    Data Guard Broker is a command-line interface that makes managing primary and standby databases easy. DBA can use a single command to switch...
  • MySQL Default database | MySQL System Database
    Once we complete installation of MySQL instance on Linux server, we could see below 4 databases/schema in instance mysql> show databas...

Labels

oracle Oracle 12c mysql PostgreSQL MongoDB oracle 19c Oracle23c oracle19c Orale PDB-CDB oracle12c python AWS Oracle ASM Virtualbox pluggable database storage engine

Pages

  • Disclaimer
  • Privacy Policy

Follow TechnoDBA

Copyright © Atikh's DBA blog | Powered by Blogger