Sunteți pe pagina 1din 20

An Introduction To MySQL Performance Optimization

Luis Rei luis.rei@gmail.com http://luisrei.com Barcamp Portugal 2008 September 6, 2008

Performance Problems

Dont Make Guesses
IT IS A capital mistake, to theorize before one has data. One begins to twist facts to suit theories, instead of theories to suit facts. (Sherlock Holmes)

PROFILE THE APPLICATION!!! Database? Identify The Problem Fix It, Optimize It, Buy It, ... Silver Bullets

Tools

SHOW Commands mytop Benchmarks MySQL Benchmark Suite Super Smack

SHOW VARIABLES;

SHOW PROCESSLIST; SHOW STATUS;

TOP & MYTOP

Benchmarks

Why? Strategies

One change at a time Repeat tests Use real data when possible (query optimizer) Run the benchmark on a different machine

The MySQL Benchmark Suite

Super Smack

More Control Simulates multiple clients Stress tests Allows more realistic scenarios Requires a lot of conguration

Table Design

Use the Appropriate data type Smallest possible (TINYINT FTW) Normalize Denormalize (massive JOINs FTL) Very large tables arent great either...

MySQL Architecture

Storage Engines

Index Types Transactions And Concurrency Data types Workload-specic Characteristics MyISAM, InnoDB, Memory, Archive, and many others

No Transactions, Not Crash Safe, Table Locks Compact data storage (HD & RAM) Does not support foreign keys TEXT and BLOB indexes Read Only or Mostly Read (SELECT) not always faster than InnoDB (INSERT/UPDATE) Fast Writes with Reads in the same table dont mix Slow crash recovery Good for many webapps and data crunching

MyISAM

InnoDB
De-facto standard Transactions, ACID, Crash Safe, Row Locks MVCC Tables and indexes are much larger Support for foreign keys High read/write concurrency Faster crash recovery Use the plugin from innodb.com
(Multi-version Concurrency Control) (2-5x)

Memory


No transactions, Table Locks Stores data in memory
contents are lost on restart (table remains) can be populated at startup

Does not support foreign keys Very fast for storing temporary results No TEXT or BLOB :( Can have non-unique keys (problem for hash indexes)

Make rows lookup faster


TABLE ... ADD INDEX(lastname)

Indexes

SELECT ... WHERE lastname = Rei If this is a common query, make an index on lastname - ALTER Partial Indexes

Space vs Speed
- ALTER TABLE ... ADD INDEX(lastname(3))

Multicolumn
ALTER TABLE ... ADD INDEX(lastname, rstname) Clustered (records are stored in primary-key order -InnoDB) Full-text (quickly retrieve every word in a eld - MyISAM)
SELECT ... WHERE body = "%luisrei.com%" SELECT ... WHERE lastname = Rei and rstname = Luis

Queries

Query Cache Less data is better - dont SELECT * Single large query vs Multiple small queries Index based ordering SELECT ... WHERE lastname = Rei ORDER BY birthdate DESC; ALTER TABLE ... DROP INDEX lastname, ADD INDEX (lastname, birthdate); Slow query log Hints SELECT ... USE INDEX myfavindex ... (IGNORE, FORCE) SELECT * FROM Table1 STRAIGHT_JOIN Table2 ...

EXPLAIN

Other Stuff - Google It



Partitions MySQL Cluster Sharding Replication Cache (Memcache)

References

MySQL Reference Manual Books

Derek J. Balling and Jeremy Zawodny (2004) High Performance MySQL, OReilly Paul DuBois (2005) MySQL (3rd Edition), Sams

Blogs MySQL Performance Blog - http://www.mysqlperformanceblog.com

Presentations Various presentations from Percona available at http://www.percona.com

S-ar putea să vă placă și