Manual:Database layout/MySQL Optimization/Tutorial

WikiMedia MySQL Optimization Tutorial
To many MediaWiki developers, SQL query performance and optimization is shrouded in mystery. Most know that there are efficient and inefficient queries, and that if they write an inefficient query, it will either be noticed during code review, or it will be noticed because it takes down a wiki, which will prompt an ops person to fix the breakage and yell at the developer that caused it. But few people seem to really understand how query performance works. How can you tell if a query is inefficient? How do you write efficient queries, and avoid inefficient ones? If so few people know this, it must be this difficult, mysterious thing, right? Fortunately, you don't have to be Domas or Tim to understand this. If you understand how a phone book works, you can learn this too.

This tutorial will cover the basics of how database engines in general, and MySQL specifically, execute different kinds of queries, and explain why certain queries are executed more efficiently than others and what role indexes play in this process. We will demonstrate better practices by writing efficient queries, and showing you how to use tables and indexes so they facilitate efficient queries, and discuss common pitfalls that result in inefficient queries and how to address them. We will also demonstrate how to obtain a query analysis from MySQL and how to make sense of it.

This will be a technical tutorial aimed at MediaWiki core and extension developers who want to avoid writing bad queries and reviewers that want to catch them. Only basic knowledge of SQL is needed to participate in the tutorial; and people who know nothing about things like PHP, MediaWiki internals or server-side database tweaking should be able to follow just fine.

Suggest a Query to be Used in the Demo
Below, add a link to a query you want to see optimized in the tutorial demo.