What one can and should optimize


Optimizing hardware for MySQL


Optimizing disks


Optimizing OS


Choosing API


Optimizing the application


Portable applications should use


If you need more speed, you should:

Don't be afraid to make the first version of your application not perfectly portable; when you have solved your problem, you can always optimize it later.

Optimizing MySQL


Compiling and installing MySQL


Maintenance


Optimizing SQL

Use SQL for the things it's good at, and do other things in your application. Use the SQL server to: Don't use an SQL server: Tips

Speed difference between different SQL servers (times in seconds)



In the above test, MySQL was run with a 8M cache; the other databases were run with installations defaults.


Important MySQL startup options


Optimizing tables


How MySQL stores data


MySQL table types


MySQL row types (only relevant for ISAM/MyISAM tables)


MySQL caches (shared between all threads, allocated once)

Note that MySQL doesn't have a row cache, but lets the OS handle this!

MySQL buffer variables (not shared, allocated on demand)


How the MySQL table cache works


MySQL extensions / optimization that gives you speed


When MySQL uses indexes


When MySQL doesn't use an index


Learn to use EXPLAIN

Use EXPLAIN on every query that you think is too slow!
mysql> explain select t3.DateOfAction, t1.TransactionID
    -> from t1 join t2 join t3
    -> where t2.ID = t1.TransactionID and t3.ID = t2.GroupID
    -> order by t3.DateOfAction, t1.TransactionID;
+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+
| table | type   | possible_keys | key     | key_len | ref              | rows | Extra                           |
+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+
| t1    | ALL    | NULL          | NULL    |    NULL | NULL             |   11 | Using temporary; Using filesort |
| t2    | ref    | ID            | ID      |       4 | t1.TransactionID |   13 |                                 |
| t3    | eq_ref | PRIMARY       | PRIMARY |       4 | t2.GroupID       |    1 |                                 |
+-------+--------+---------------+---------+---------+------------------+------+---------------------------------+
Types ALL and range signal a potential problem.

Learn to use SHOW PROCESSLIST

Use SHOW processlist to find out what is going on:
+----+-------+-----------+----+---------+------+--------------+-------------------------------------+
| Id | User  | Host      | db | Command | Time | State        | Info                                |
+----+-------+-----------+----+---------+------+--------------+-------------------------------------+
| 6  | monty | localhost | bp | Query   | 15   | Sending data | select * from station,station as s1 |
| 8  | monty | localhost |    | Query   | 0    |              | show processlist                    |
+----+-------+-----------+----+---------+------+--------------+-------------------------------------+
Use KILL in mysql or mysqladmin to kill off runaway threads.

How to find out how MySQL solves a query

Run the following commands and try to understand the output:

MySQL is extremely good


Things to avoid with MySQL


Different locks in MySQL


Tricks to give MySQL more information to solve things better

Note that you can always comment out a MySQL feature to make the query portable:
SELECT /*! SQL_BUFFER_RESULTS */ ...

Example of doing transactions


Example of using REPLACE

REPLACE works exactly like INSERT, except that if an old record in the table has the same value as a new record on a unique index, the old record is deleted before the new record is inserted. Instead of using
  SELECT 1 FROM t1 WHERE key=#
  IF found-row
    LOCK TABLES t1
    DELETE FROM t1 WHERE key1=#
    INSERT INTO t1 VALUES (...)
    UNLOCK TABLES t1;
  ENDIF
Do
  REPLACE INTO t1 VALUES (...)    

General tips


Benefits of using MySQL 3.23


Important features that we are actively working on