12

Click here to load reader

Optimizing MySQL

Embed Size (px)

DESCRIPTION

Drupal Meetup Group Toronto

Citation preview

Page 1: Optimizing MySQL

<Insert Picture Here>

Optimizing MySQL Morgan Tocker, MySQL Community Managerhttp://www.tocker.ca/

Page 2: Optimizing MySQL

Safe Harbor Statement

The  following  is  intended  to  outline  our  general  product  direction.  It  is  intended  for  information  purposes  only,  and  may  not  be  incorporated  into  any  contract.  It  is  not  a  commitment  to  deliver  any  material,  code,  or  functionality,  and  should  not  be  relied  upon  in  making  purchasing  decisions.  The  development,  release,  and  timing  of  any  features  or  functionality  described  for  Oracle’s  products  remains  at  the  sole  discretion  of  Oracle.

Page 3: Optimizing MySQL

Options

• Upgrade Hardware and/or MySQL Version • Optimize Configuration • Optimize Queries • Optimize Schema

Page 4: Optimizing MySQL

Commentary

• There are some specific cases where upgrades can make individual queries faster (i.e. subqueries). When I like this optimization the most, is to increase“capacity”. Lots of multi-core, multi-disk enhancements in 5.5 / 5.6.

• MySQL 5.6 comes with a much more optimized configuration. There are now only 3-4 settings that need to change.

• Optimizing queries is my favourite method :) There can be some big wins here.

• Optimizing schema relates to query optimization. Sometimes you have to change schema to support certain queries, but it always starts with the queries.

Page 5: Optimizing MySQL

Optimizing Queries

• Allows you to focus on queries that damage user experiences rather than just slow.

• Best method is to install a profiler. • MySQL also supports slow query logging +

Performance Schema (5.6+).

Page 6: Optimizing MySQL

# Continent Asia + population > 5M!mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' AND population > 5000000\G!*************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ALL!possible_keys: NULL! key: NULL! key_len: NULL! ref: NULL! rows: 267! Extra: Using where!1 row in set (0.00 sec)

Page 7: Optimizing MySQL

mysql> ALTER TABLE Country ADD INDEX p (population);!Query OK, 0 rows affected (0.02 sec)!Records: 0 Duplicates: 0 Warnings: 0!!mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' AND population > 5000000\G!*************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ALL!possible_keys: p! key: NULL! key_len: NULL! ref: NULL! rows: 264! Extra: Using where!1 row in set (0.01 sec)

Page 8: Optimizing MySQL

# 50 Million!mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' AND population > 50000000\G!*************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: range!possible_keys: p! key: p! key_len: 4! ref: NULL! rows: 24! Extra: Using index condition; Using where!1 row in set (0.00 sec)

Page 9: Optimizing MySQL

mysql> ALTER TABLE Country ADD INDEX c (continent);!Query OK, 0 rows affected (0.02 sec)!Records: 0 Duplicates: 0 Warnings: 0!!mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' !AND population > 50000000\G!*************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ref!possible_keys: p,c! key: c! key_len: 1! ref: const! rows: 51! Extra: Using index condition; Using where!1 row in set (0.00 sec)

Page 10: Optimizing MySQL

mysql> ALTER TABLE Country ADD INDEX pc (population,continent);!Query OK, 0 rows affected (0.02 sec)!Records: 0 Duplicates: 0 Warnings: 0!!mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' !AND population > 50000000\G!*************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: ref!possible_keys: p,c,pc! key: c! key_len: 1! ref: const! rows: 51! Extra: Using index condition; Using where!1 row in set (0.00 sec)

Page 11: Optimizing MySQL

mysql> ALTER TABLE Country ADD INDEX cp (continent,population);!Query OK, 0 rows affected (0.01 sec)!Records: 0 Duplicates: 0 Warnings: 0!!mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' !AND population > 50000000\G!*************************** 1. row ***************************! id: 1! select_type: SIMPLE! table: Country! type: range!possible_keys: p,c,pc,cp! key: cp! key_len: 5! ref: NULL! rows: 11! Extra: Using index condition!1 row in set (0.00 sec)

Page 12: Optimizing MySQL

Tips

• Think of where you can use indexes to eliminate rows - that’s the column you typically index.

• Read: http://dev.mysql.com/doc/refman/5.6/en/explain.html

• Composite indexes somewhat an advanced topic: • Useful when a single column does not eliminate

enough work. • “Ranges to the right”