Optimizing MySQL
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Optimizing MySQL

  • 967 views
Uploaded on

Drupal Meetup Group Toronto

Drupal Meetup Group Toronto

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
967
On Slideshare
842
From Embeds
125
Number of Embeds
12

Actions

Shares
Downloads
18
Comments
0
Likes
0

Embeds 125

http://www.tocker.ca 71
http://www.feedspot.com 12
http://localhost 11
http://feedreader.com 11
http://cloud.feedly.com 9
http://feeds.feedburner.com 4
http://linkup25.rssing.com 2
http://digg.com 1
http://10.0.1.4 1
http://d1qhe8io1bro9v.cloudfront.net 1
http://www.inoreader.com 1
http://feedly.com 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. <Insert Picture Here> Optimizing MySQL
 Morgan Tocker, MySQL Community Manager
 http://www.tocker.ca/

  • 2. 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.
  • 3. Options • • • • Upgrade Hardware and/or MySQL Version Optimize Configuration Optimize Queries Optimize Schema
  • 4. 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.
  • 5. 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+).
  • 6. # Continent Asia + population > 5M! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 
 AND population > 5000000G! *************************** 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)
  • 7. 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 > 5000000G! *************************** 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)
  • 8. # 50 Million! mysql> EXPLAIN SELECT * FROM Country WHERE continent='Asia' 
 AND population > 50000000G! *************************** 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)
  • 9. 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 > 50000000G! *************************** 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)
  • 10. 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 > 50000000G! *************************** 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)
  • 11. 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 > 50000000G! *************************** 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)
  • 12. 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”