# MongoDB Diagnostics and Debugging (Version 3.4) <br />Ch 3: Slow Queries ###### tags: `MongoDB University M312` ## What Slow Queries Look Like in an Application ### Response Time * End to end application request should be < 200ms * \> 100ms is considered slow for MongoDB by default ## Response Time Degredation ### The Culprits * Working set exceeding RAM * Queries takes longer as data set grows * Growing pool of clients * Unbounded array growth * Excessive number of indexes :::info Mongostat, mlogvis and the porfiler are here to help to resolve the slowness problem. ***# Make sure to use profiler only in development*** ::: ## Throughput Drops ### Possible Causes for a Drop in Throughput * Long-running queries * Collection scan * Poorly anchored regex * Inefficient index usage * Index Builds * Write Contention ### If You Suspect an Increase in Long-Running Queries * Server log * db.currentOp() * Turn on the profiler(But further slowdown) ## Impact of Application Changes ### Detect Application Changes * Keep historical monitering data * Watching out for spike in connections * Watching out for spike in ops/s * Spotting long lasting, non-indexed queries ### Connections Spikes * Exhausting connection pools * Incorrect connection to production environment * Analytical workload and reporting tools ## Using Mtools to Find Slow Queries with 3 tools * mloginfo * mplotqueries * mlogvis 使用 mtools 快速啟動 Replica set ```bash= mlaunch init --replicaset --name m312RS --wiredTigerCacheSizeGB 0.3 --port 30000 --host localhost ``` ## Fixing Missing Indexes ### Building an Index Options * Build the index on the primary from the shell * Build the index in the Background on the primary on the shell * Build the index from the Compass * Build the index on each member of the replica set * Use Ops manager and Cloud manager to build the index ```bash= # check the log file to find if the index is building background mlogfilter $logfile --component INDEX mlogfilter $logfile --component COMMAND ```