Strategic MySQL Planning for Complexity & Growth Or MySQL Scaling for Dummies Thomas Falgout Sr. Developer Yahoo!
The Basics Ways to squeeze extra performance: Query Optimization
Schema Definition
Hardware
Software
MySQL Config
Cache
Monitoring
Query Optimizations KNOW EVERY QUERY!
Developers usually don't write proper SQL
SQL Statements will directly affect your performance
Example: Repeating SQL Statements for no benefit
1000 very quick unnecessary queries vs 1 slow query
Query Optimizations Don't Develop in an island Talk it over.
Your coworkers might surprise you.
In a good way.
Query Optimizations - EXPLAIN Only works for SELECT
Quickly show you how much data your queries are processing
Could spend an entire session on just this.
But won't.
Read for yourself.
http://dev.mysql.com/doc/refman/5.1/en/explain.html
http://dev.mysql.com/doc/refman/5.1/en/using-explain.html
http://www.percona.com/files//presentations/EXPLAIN_demystified.pdf
LUUUUUCCCYYYYYYY!!!!!
Query Optimizations SELECTS SELECT * is bad Hurts performance (cache, maintainability, bandwidth) Joining two 100,000 row tables gets big
“ SELECT COUNT(*) FROM users” Performs full table scan on Innodb
Faster on MyISAM (stored in table info)
Query Optimizations INSERTS Can you batch INSERT?
INSERT INTO a VALUES (1,23),(2,34),(4,33);
To speed up INSERT operations that require multiple statements, LOCK your tables
Can you use LOAD DATA? (Import via CSV)
Query Optimizations UPDATE Updates are usually pretty good.
Not much to say.
Query Optimizations DELETE DELETES can be costly. Sometimes causes DB to rebuild index IDEAS Add a “to_delete” column and then delete batch
If having to delete A LOT of rows SELECT INTO rows to keep into temp table
DROP table
RENAME tmp table to table
Query Optimizations Find queries that do COUNT(*) or create tables with results Create tables with results
Aggregate data hourly/daily/per update/etc.
Query Optimization – Handling Logs Don't talk directly to DB
Send updates through daemon to load data
Don't update for each request Aggregate per 1000 req/2 min/app start
UPDATE counts SET count = count + 42 WHERE id=1
Schema Definition - MyISAM Original Engine for MySQL
Does not handle transactions
Faster to Write, Slightly faster to Read
Uses separate Data (.MYD), Index (.MYI) and Frame (.frm)
Schema Definition - INNODB 2 nd Engine for MySQL – From Oracle/Innobase
Handles transactions START TRANS; INSERT xxx; COMMIT Uses one file for both data and index for entire DB (Can change in my.cnf to separate to table)
Slower to write and read since it stores data per transaction. Can be tuned to be almost as fast as MyISAM
Schema Definition - BLACKHOLE Fastest Engine available

MySQL Scaling Presentation