SlideShare a Scribd company logo
1 of 23
MongoDB Performance 
Tuning Mastery 
MongoDB Meetup London 
Kenny Gorman 
Chief Technologist; Data @ Rackspace 
Co-Founder @ ObjectRocket 
@kennygorman
Order Matters 
1. Schema design 
1. Workload tuning 
1. Instance tuning 
1. Reactive tuning
Schema Design 
● Why MongoDB? 
● Resist your urge to model relationally 
● Develop sound workload patterns 
● Test those patterns
Workload Tuning 
Identify Tune 
● Profile 
● Explain 
● Tune
Profiler 
while performance_problems(): 
bad_statements = find_candidates() 
for statement in bad_statements: 
statement.explain() 
● What is the function for 
performance_problems()? 
● If true, then tools()
Profiler 
● Per DB, capped 
● db.system.profile 
● Your most 
important tool 
● Leave it on. Yes, I 
said that. Leave it 
the F*&K on.
Profiler 
$> db.setProfilingLevel(2); 
{ "was" : 0, "slowms" : 100, "ok" : 1 } 
$> db.testme.save({"name":"Kenny"}); 
$> db.system.profile.find().pretty() 
{ "ts" : ISODate("2013-02-11T18:45:06.857Z"), 
"op" : "insert", 
"ns" : "test.testme", 
"keyUpdates" : 0, 
"numYield" : 0, 
"lockStats" : {..}, 
"millis" : 0, 
"client" : "127.0.0.1", 
"user" : "" }
Profiler 
{ 
"ts" : ISODate("2012-09-14T16:34:00.010Z"), // date it occurred 
"op" : "query", // the 
operation type 
"ns" : "game.players", // the 
db and collection 
"query" : { "total_games" : 1000 }, // query document 
"ntoreturn" : 0, // # 
docs returned with limit() 
"ntoskip" : 0, 
// # of docs to skip() 
"nscanned" : 959967, // number of docs 
scanned 
"keyUpdates" : 0, // 
updates of secondary indexes 
"numYield" : 1, // # of 
times yields took place 
"lockStats" : { ... }, // 
subdoc of lock stats 
"nreturned" : 0, // # 
docs actually returned 
"responseLength" : 20, // size 
of doc 
"millis" : 859, // how 
long it took 
"client" : "127.0.0.1", // 
http://docs.mongodb.org/manual/reference/database-profiler/ 
client asked for it 
"user" : "" 
// the user asking for it 
}
Profiler: Finding Candidates 
// response time by operation type 
db.system.profile.aggregate( 
{ $group : { 
_id :"$op", 
count:{$sum:1}, 
"max response time":{$max:"$millis"}, 
"avg response time":{$avg:"$millis"} 
}}); 
// slowest by namespace 
db.system.profile.aggregate( 
{ $group : { 
_id :"$ns", 
count:{$sum:1}, 
"max response time":{$max:"$millis"}, 
"avg response time":{$avg:"$millis"} 
}}, 
{$sort: { 
"max response time":-1} 
}); 
// slowest by client 
db.system.profile.aggregate( 
{$group : { 
_id :"$client", 
count:{$sum:1}, 
"max response time":{$max:"$millis"}, 
"avg response time":{$avg:"$millis"} 
}}, 
{$sort: { 
"max response time":-1} 
}); 
// summary moved vs non-moved 
db.system.profile.aggregate( 
{ $group : { 
_id :"$moved", 
count:{$sum:1}, 
"max response time":{$max:"$millis"}, 
"avg response time":{$avg:"$millis"} 
}});
Explain: What am I looking for? 
● fastmod 
● nscanned != nreturned 
● key updates 
● moves 
● lock waits 
● just returning too much data (count or size or both) 
● bad cardinality
Explain 
● Take the document from profiler output, 
explain it 
● Look at results, make corrections 
● Rinse and repeat
Explain 
$>db.system.profile.find({"op":"query","ns":"test.testme"}).pretty(); 
{ "ts" : ISODate("2013-02-11T19:53:16.302Z"), 
"op" : "query", 
"ns" : "test.testme", 
"query" : { "name" : 1 }, 
"ntoreturn" : 0, 
"ntoskip" : 0, 
"nscanned" : 32001, 
// why scanning so many? 
"keyUpdates" : 0, 
"numYield" : 0, 
"lockStats" : {...}, 
"nreturned" : 1, 
// just to return 1 
"responseLength" : 56, 
"millis" : 29, 
// slow! 
"client" : "127.0.0.1", 
"user" : "" 
}
Explain 
$> db.testme.find({ "name": 1 }).explain() 
{ 
"cursor" : "BasicCursor", 
// Basic 
"isMultiKey" : false, 
"n" : 1, 
"nscannedObjects" : 32001, 
"nscanned" : 32001, 
"nscannedObjectsAllPlans" : 32001, 
"nscannedAllPlans" : 32001, 
"scanAndOrder" : false, 
"indexOnly" : false, 
"nYields" : 0, 
"nChunkSkips" : 0, 
"millis" : 14, 
"indexBounds" : { 
// WTF! 
}, 
... 
}
Explain 
$> db.testme.ensureIndex({"name":-1}); 
$> db.testme.find({"name":1}).explain() 
{ 
"cursor" : "BtreeCursor name_-1", 
// Btree 
"isMultiKey" : false, 
"n" : 1, 
"nscannedObjects" : 1, 
"nscanned" : 1, 
"nscannedObjectsAllPlans" : 1, 
"nscannedAllPlans" : 1, 
"scanAndOrder" : false, 
"indexOnly" : false, 
"nYields" : 0, 
"nChunkSkips" : 0, 
"millis" : 0, 
"indexBounds" : { 
"name" : [ 
[ 
1, 
// w00t! 
1 
] 
] 
}, 
... 
}
Now I need tools
Response time analysis 
● Response time analysis techniques come from Oracle community circa 
2000-2004. 
● Response time = service time + queue time ( time_to_complete + 
time_waiting_in_queue ) 
● Each document in profile collection a couple response time attributes. 
o millis 
o timeAcquiring 
o timeLocked 
● The only true measure of response time in MongoDB 
● Aids in prioritization of tuning opportunities. Finding the bang for the buck, 
or the immediate performance problem.
Definitions: 
system.profile.lockStats.timeLockedMicros 
The time in microseconds the operation held a specific lock. For operations that require more than one lock, like those 
that lock the localdatabase to update the oplog, then this value may be longer than the total length of the operation 
(i.e. millis.) 
system.profile.lockStats.timeAcquiringMicros 
The time in microseconds the operation spent waiting to acquire a specific lock. 
system.profile.millis 
The time in milliseconds for the server to perform the operation. This time does not include network time nor time to 
acquire the lock.
Response time analysis 
$>db.system.profile.aggregate( 
[ 
{ $project : { 
"op" : "$op", 
"millis" : "$millis", 
"timeAcquiringMicrosrMS" : { $divide : [ "$lockStats.timeAcquiringMicros.r", 1000 ] 
}, 
"timeAcquiringMicroswMS" : { $divide : [ "$lockStats.timeAcquiringMicros.w", 1000 ] 
}, 
"timeLockedMicrosrMS" : { $divide : [ "$lockStats.timeLockedMicros.r", 1000 ] }, 
"timeLockedMicroswMS" : { $divide : [ "$lockStats.timeLockedMicros.w", 1000 ] } } 
}, 
{ $project : { 
"op" : "$op", 
"millis" : "$millis", 
"total_time" : { $add : [ "$millis", "$timeAcquiringMicrosrMS", 
"$timeAcquiringMicroswMS" ] }, 
"timeAcquiringMicrosrMS" : "$timeAcquiringMicrosrMS", 
"timeAcquiringMicroswMS" : "$timeAcquiringMicroswMS", 
"timeLockedMicrosrMS" : "$timeLockedMicrosrMS", 
"timeLockedMicroswMS" : "$timeLockedMicroswMS" } 
}, 
{ $group : { 
_id : "$op", 
"average response time" : { $avg : "$millis" }, 
"average response time + acquire time": { $avg: "$total_time"}, 
"average acquire time reads" : { $avg : "$timeAcquiringMicrosrMS" }, 
"average acquire time writes" : { $avg : "$timeAcquiringMicroswMS" }, 
"average lock time reads" : { $avg : "$timeLockedMicrosrMS" }, 
"average lock time writes" : { $avg : "$timeLockedMicroswMS" } } 
} 
] 
);
Response time analysis 
{ 
"_id" : "insert", 
"average response time" : 0.07363770250368189, 
// time executing 
"average acquire time reads" : 0, 
"average acquire time writes" : 5.623796023564078, // time 
waiting 
"average lock time reads" : 0, 
"average lock time writes" : 0.25491826215022123 // time in 
lock.. woah. 
} 
{ 
"_id" : "update", 
"average response time" : 0.23551171393341552, 
// time executing.. moves? 
"average acquire time reads" : 0, 
"average acquire time writes" : 10.261996300863133, // lots of 
waiting 
"average lock time reads" : 0, 
"average lock time writes" : 0.3795672009864362 
// time in lock.. again! 
}
Response time analysis 
● Python code to get you started with profiler and graphite: 
https://github.com/kgorman/slum
Instance Tuning 
● Mongostat 
● Cache management, Hot Set, and Disk I/O. 
● Locks and Sharding 
● Some thoughts on Hardware
Links 
http://www.kennygorman.com/mongodb-profiler-helpful-queries/ 
https://gist.github.com/kgorman/134896c7414fde8e090b 
https://github.com/kgorman/slum 
https://github.com/kgorman/ocean 
http://docs.mongodb.org/manual/reference/database-profiler/ 
http://docs.mongodb.org/manual/reference/method/cursor.explain/#explain-output- 
fields-core
Contact 
@kennygorman 
kenny.gorman@rackspace.com

More Related Content

Recently uploaded

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Recently uploaded (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Featured

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 

Featured (20)

Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 

MongoDB performance tuning mastery

  • 1. MongoDB Performance Tuning Mastery MongoDB Meetup London Kenny Gorman Chief Technologist; Data @ Rackspace Co-Founder @ ObjectRocket @kennygorman
  • 2. Order Matters 1. Schema design 1. Workload tuning 1. Instance tuning 1. Reactive tuning
  • 3. Schema Design ● Why MongoDB? ● Resist your urge to model relationally ● Develop sound workload patterns ● Test those patterns
  • 4. Workload Tuning Identify Tune ● Profile ● Explain ● Tune
  • 5. Profiler while performance_problems(): bad_statements = find_candidates() for statement in bad_statements: statement.explain() ● What is the function for performance_problems()? ● If true, then tools()
  • 6. Profiler ● Per DB, capped ● db.system.profile ● Your most important tool ● Leave it on. Yes, I said that. Leave it the F*&K on.
  • 7. Profiler $> db.setProfilingLevel(2); { "was" : 0, "slowms" : 100, "ok" : 1 } $> db.testme.save({"name":"Kenny"}); $> db.system.profile.find().pretty() { "ts" : ISODate("2013-02-11T18:45:06.857Z"), "op" : "insert", "ns" : "test.testme", "keyUpdates" : 0, "numYield" : 0, "lockStats" : {..}, "millis" : 0, "client" : "127.0.0.1", "user" : "" }
  • 8. Profiler { "ts" : ISODate("2012-09-14T16:34:00.010Z"), // date it occurred "op" : "query", // the operation type "ns" : "game.players", // the db and collection "query" : { "total_games" : 1000 }, // query document "ntoreturn" : 0, // # docs returned with limit() "ntoskip" : 0, // # of docs to skip() "nscanned" : 959967, // number of docs scanned "keyUpdates" : 0, // updates of secondary indexes "numYield" : 1, // # of times yields took place "lockStats" : { ... }, // subdoc of lock stats "nreturned" : 0, // # docs actually returned "responseLength" : 20, // size of doc "millis" : 859, // how long it took "client" : "127.0.0.1", // http://docs.mongodb.org/manual/reference/database-profiler/ client asked for it "user" : "" // the user asking for it }
  • 9. Profiler: Finding Candidates // response time by operation type db.system.profile.aggregate( { $group : { _id :"$op", count:{$sum:1}, "max response time":{$max:"$millis"}, "avg response time":{$avg:"$millis"} }}); // slowest by namespace db.system.profile.aggregate( { $group : { _id :"$ns", count:{$sum:1}, "max response time":{$max:"$millis"}, "avg response time":{$avg:"$millis"} }}, {$sort: { "max response time":-1} }); // slowest by client db.system.profile.aggregate( {$group : { _id :"$client", count:{$sum:1}, "max response time":{$max:"$millis"}, "avg response time":{$avg:"$millis"} }}, {$sort: { "max response time":-1} }); // summary moved vs non-moved db.system.profile.aggregate( { $group : { _id :"$moved", count:{$sum:1}, "max response time":{$max:"$millis"}, "avg response time":{$avg:"$millis"} }});
  • 10. Explain: What am I looking for? ● fastmod ● nscanned != nreturned ● key updates ● moves ● lock waits ● just returning too much data (count or size or both) ● bad cardinality
  • 11. Explain ● Take the document from profiler output, explain it ● Look at results, make corrections ● Rinse and repeat
  • 12. Explain $>db.system.profile.find({"op":"query","ns":"test.testme"}).pretty(); { "ts" : ISODate("2013-02-11T19:53:16.302Z"), "op" : "query", "ns" : "test.testme", "query" : { "name" : 1 }, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 32001, // why scanning so many? "keyUpdates" : 0, "numYield" : 0, "lockStats" : {...}, "nreturned" : 1, // just to return 1 "responseLength" : 56, "millis" : 29, // slow! "client" : "127.0.0.1", "user" : "" }
  • 13. Explain $> db.testme.find({ "name": 1 }).explain() { "cursor" : "BasicCursor", // Basic "isMultiKey" : false, "n" : 1, "nscannedObjects" : 32001, "nscanned" : 32001, "nscannedObjectsAllPlans" : 32001, "nscannedAllPlans" : 32001, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 14, "indexBounds" : { // WTF! }, ... }
  • 14. Explain $> db.testme.ensureIndex({"name":-1}); $> db.testme.find({"name":1}).explain() { "cursor" : "BtreeCursor name_-1", // Btree "isMultiKey" : false, "n" : 1, "nscannedObjects" : 1, "nscanned" : 1, "nscannedObjectsAllPlans" : 1, "nscannedAllPlans" : 1, "scanAndOrder" : false, "indexOnly" : false, "nYields" : 0, "nChunkSkips" : 0, "millis" : 0, "indexBounds" : { "name" : [ [ 1, // w00t! 1 ] ] }, ... }
  • 15. Now I need tools
  • 16. Response time analysis ● Response time analysis techniques come from Oracle community circa 2000-2004. ● Response time = service time + queue time ( time_to_complete + time_waiting_in_queue ) ● Each document in profile collection a couple response time attributes. o millis o timeAcquiring o timeLocked ● The only true measure of response time in MongoDB ● Aids in prioritization of tuning opportunities. Finding the bang for the buck, or the immediate performance problem.
  • 17. Definitions: system.profile.lockStats.timeLockedMicros The time in microseconds the operation held a specific lock. For operations that require more than one lock, like those that lock the localdatabase to update the oplog, then this value may be longer than the total length of the operation (i.e. millis.) system.profile.lockStats.timeAcquiringMicros The time in microseconds the operation spent waiting to acquire a specific lock. system.profile.millis The time in milliseconds for the server to perform the operation. This time does not include network time nor time to acquire the lock.
  • 18. Response time analysis $>db.system.profile.aggregate( [ { $project : { "op" : "$op", "millis" : "$millis", "timeAcquiringMicrosrMS" : { $divide : [ "$lockStats.timeAcquiringMicros.r", 1000 ] }, "timeAcquiringMicroswMS" : { $divide : [ "$lockStats.timeAcquiringMicros.w", 1000 ] }, "timeLockedMicrosrMS" : { $divide : [ "$lockStats.timeLockedMicros.r", 1000 ] }, "timeLockedMicroswMS" : { $divide : [ "$lockStats.timeLockedMicros.w", 1000 ] } } }, { $project : { "op" : "$op", "millis" : "$millis", "total_time" : { $add : [ "$millis", "$timeAcquiringMicrosrMS", "$timeAcquiringMicroswMS" ] }, "timeAcquiringMicrosrMS" : "$timeAcquiringMicrosrMS", "timeAcquiringMicroswMS" : "$timeAcquiringMicroswMS", "timeLockedMicrosrMS" : "$timeLockedMicrosrMS", "timeLockedMicroswMS" : "$timeLockedMicroswMS" } }, { $group : { _id : "$op", "average response time" : { $avg : "$millis" }, "average response time + acquire time": { $avg: "$total_time"}, "average acquire time reads" : { $avg : "$timeAcquiringMicrosrMS" }, "average acquire time writes" : { $avg : "$timeAcquiringMicroswMS" }, "average lock time reads" : { $avg : "$timeLockedMicrosrMS" }, "average lock time writes" : { $avg : "$timeLockedMicroswMS" } } } ] );
  • 19. Response time analysis { "_id" : "insert", "average response time" : 0.07363770250368189, // time executing "average acquire time reads" : 0, "average acquire time writes" : 5.623796023564078, // time waiting "average lock time reads" : 0, "average lock time writes" : 0.25491826215022123 // time in lock.. woah. } { "_id" : "update", "average response time" : 0.23551171393341552, // time executing.. moves? "average acquire time reads" : 0, "average acquire time writes" : 10.261996300863133, // lots of waiting "average lock time reads" : 0, "average lock time writes" : 0.3795672009864362 // time in lock.. again! }
  • 20. Response time analysis ● Python code to get you started with profiler and graphite: https://github.com/kgorman/slum
  • 21. Instance Tuning ● Mongostat ● Cache management, Hot Set, and Disk I/O. ● Locks and Sharding ● Some thoughts on Hardware
  • 22. Links http://www.kennygorman.com/mongodb-profiler-helpful-queries/ https://gist.github.com/kgorman/134896c7414fde8e090b https://github.com/kgorman/slum https://github.com/kgorman/ocean http://docs.mongodb.org/manual/reference/database-profiler/ http://docs.mongodb.org/manual/reference/method/cursor.explain/#explain-output- fields-core

Editor's Notes

  1. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  2. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  3. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  4. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  5. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  6. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  7. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  8. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  9. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  10. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  11. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.
  12. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared. Use new Aggregation Framework Allows for aggregated queries from loads of data Examples: https://gist.github.com/995a3aa5b35e92e5ab57
  13. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared. Use new Aggregation Framework Allows for aggregated queries from loads of data Examples: https://gist.github.com/995a3aa5b35e92e5ab57
  14. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared. Use new Aggregation Framework Allows for aggregated queries from loads of data Examples: https://gist.github.com/995a3aa5b35e92e5ab57
  15. Data is saved in capped collections, 1 per shard db.system.profile Turn it on, gather data, later analyze for tuning opportunities db.setProfilingLevel(1,20) db.getProfilingStatus() 1 document per statement show profile db.system.profile.find() leave it on, don't be scared.