common mysql scalability mistakes and how to avoid them

83
@RonaldBradford #surgecon The most common MySQL scalability mistakes, and how to avoid them. Ronald Bradford 2010.08 www.RonaldBradford.com

Upload: ronald-bradford

Post on 15-Jan-2015

8.258 views

Category:

Technology


2 download

DESCRIPTION

The most common mistakes are easy to avoid however many startups continue to fall prey, with the impact including large re-design costs, delays in new feature releases, lower staff productivity and less then ideal ROI. All growing and successful sites need to achieve higher Availability, seamless Scalability and proven Resilience. Know the right MySQL environment to provide a suitable architecture and application design to support these essential needs.Presented at Surge Scalability -- http://omniti.com/surge/2010

TRANSCRIPT

Page 1: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

The most common

MySQL scalability

mistakes,

and how to avoid them.

Ronald Bradford

2010.08

www.RonaldBradford.com

Page 2: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon99

Page 3: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

My website seems to freeze or responds randomly?

PROBLEM

9

Page 4: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

The default MyISAM storage engine uses exclusive table locks for DML.

CAUSE

9

Page 5: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Optimize blocking query performance

Use a transactional engine with MVCC and row based locking to address the LOCKING issue

SOLUTION

9

Page 6: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Crazy end user report that selects all customer, order, order lines and order history data and performs poor joins. This takes shared read locks blocking future write locks then future reads.

EXAMPLE

9

Page 7: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

MySQL is unique in that it offers different mechanisms for storing and retrieving data, each with strengths and weaknesses.

The DEFAULT is not always the best.

WHY

9

Page 8: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

MySQL PROCESSLISTBlocked have State = LockedBlocker - Same table, older Time

HOW

9mysql> SHOW PROCESSLIST;+----+------+-----------+-------+---------+------+------------+---------------| Id | User | Host | db | Command | Time | State | Info +----+------+-----------+-------+---------+------+------------+---------------| 13 | root | localhost | odtug | Query | 144 | User sleep | UPDATE test1 ...| 14 | root | localhost | odtug | Query | 116 | Locked | select * from test1 | 15 | root | localhost | odtug | Query | 89 | Locked | select * from test1

Page 9: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Optimize Blocker

Indexes

Limit query

Summary table

Change storage engine

HOW

9

Page 10: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon88

Page 11: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Why is my database so large?

PROBLEM

8

Page 12: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Don’t store large static objects in the database

SOLUTION

8

Page 13: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

80% of data is email content/attachments

60% of data is PDF documents

30% of data is uncompressed large XML objects

EXAMPLE

8

Page 14: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Maximize memory usage for important data

Reduce database recovery time

WHY

8

Page 15: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Compress large text data

90% saving on XML data

Store static data in files

Avoids DB handling overhead

HOW

8

Page 16: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

HOW

8Table Size per schema

# Schema Table UsageSELECT table_schema,table_name,engine,row_format, table_rows, avg_row_length, (data_length+index_length)/1024/1024 as total_mb, (data_length)/1024/1024 as data_mb, (index_length)/1024/1024 as index_mb, CURDATE() AS todayFROM information_schema.tables WHERE table_schema = DATABASE()ORDER BY 7 DESC;

Page 17: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon77

Page 18: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

I can't access my website?

PROBLEM

7

Page 19: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

TRUE STORY

Page 20: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Question:

TRUE STORY

Page 21: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Question:

How do you know when your server is down or not accessible?

TRUE STORY

Page 22: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Question:

How do you know when your server is down or not accessible?

Answer:

TRUE STORY

Page 23: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Question:

How do you know when your server is down or not accessible?

Answer:

The users will let us know.

TRUE STORY

Page 24: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Monitoring

Alerting

Dashboard

Status Site

SOLUTION

7

Page 25: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Monitoring/Alerting

GraphicalHistoricalNecessaryGenerally missing/incompleteUseless for real-time analysis

HOW

7

Page 26: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

DashboardThe state of NOW

Sampling at 1s/3s/5s

e.g. 0.1% of throughput

HOW

7

Page 27: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

InstrumentationImportant to business viability

e.g. orders per minute

page load time

Seamless implementationi.e. no code changes to view real-time

extensible

HOW

7

Page 28: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Have a status websiteallow for comments (e.g. blog)

Have a public dashboard website

Host them somewhere else!

HOW

7

Page 29: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon66

Page 30: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

My replication slave can't keep up?

PROBLEM

6

Page 31: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Know the weakest link(s) of MySQL replication and don't exceed that,or cheat.

SOLUTION

6

Page 32: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

If replication can't catchup, slaves are useless.

Backup & recovery may also suffer.

WHY

Page 33: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Master

DML Statement

Write Data/Redo Log

Write Binary Log

Return OK to client

HOW

Page 34: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

SlaveDetect master log changeRetrieve binary log entryWrite relay log (IO_THREAD)Read relay logApply DML (SQL_THREAD)Write Data/redo log

HOW

Page 35: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Clever workaroundsRestrict queries executed

--ignoreDifferent storage enginesDifferent index structures

HOW

Page 36: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Cheating

RAID 0 (large number of slaves)

Pre fetch thread

EXPERT TIP

Page 37: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon55

Page 38: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

My server has crashed with a hard drive failure

PROBLEM

5

Page 39: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Question:Have you ever performed a database recovery?

Answer:No, why?

TRUE STORY

5

Page 40: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Consultant:Do you know that your daily backups only recover the data up to that time, e.g. 1am. You know you have lost all your sales and changes since then.

Customer:No, I didn’t know that.

TRUE STORY

5

Page 41: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Have a DR Plan

Documented

Tested

Timed

Verified - End to End

SOLUTION

5

Page 42: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Do you pass the MySQL backup/recovery quiz?

http://rb42.com/mysql-backup-quiz

HOW

5

Page 43: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

1. Do you have MySQL backups in place? 2. Do you backup ALL your MySQL data? 3. Do you have consistent MySQL backups? 4. Do you have backups that include both static snapshot and point in time transactions? 5. Do you review your backup logs EVERY SINGLE day or have tested backup monitoring in place? 6. Do you perform a test recovery of your static backup? 7. Do you perform a test recovery to point in time? 8. Do you time your backup and recovery process and review over time? 9. Do you have off-site copies of your backups? 10. Do you backup your primary binary logs?

QUIZ

5http://rb42.com/mysql-backup-quiz

Page 44: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon44

Page 45: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Why is my database executing 1,200 qps for 50 users?

PROBLEM

4

Page 46: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Determine what queries are running and why they are running?

SOLUTION

4

Page 47: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Excessive SQL statementsDuplicateRedundant CachableRow at a time (RAT)

CAUSE

4

Page 48: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Reducing SQL load both improves performance now and provides greater capacity as you scale

WHY

4

Page 49: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

EXAMPLE

4http://ronaldbradford.com/blog/we-need-more-cats-2009-08-22/

SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes'SELECT option_value FROM wp_options WHERE option_name = 'aiosp_title_format' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_show_only_even' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_num_months' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_day_length' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_hide_event_box' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_advanced' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_navigation' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'ec3_disable_popups' LIMIT 1SELECT option_value FROM wp_options WHERE option_name = 'sidebars_widgets' LIMIT 1

Page 50: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

EXAMPLE

4http://ronaldbradford.com/blog/optimizing-sql-performance-the-art-of-elimination-2010-07-08/

SELECT * FROM activities_theme WHERE theme_parent_id=0SELECT * FROM activities_theme WHERE theme_parent_id=1SELECT * FROM activities_theme WHERE theme_parent_id=2SELECT * FROM activities_theme WHERE theme_parent_id=11SELECT * FROM activities_theme WHERE theme_parent_id=16

SELECT *FROM activities_themeWHERE theme_parent_id in (0,1,2,11,16)

Page 51: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

EXAMPLE

4http://ronaldbradford.com/blog/optimizing-sql-performance-the-art-of-elimination-2010-07-08/

5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist` WHERE (ArtistID = 196 )5 Query SELECT * FROM `artist` WHERE (ArtistID = 2188 )5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist`5 Query SELECT * FROM `artist`

Page 52: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

EXAMPLE

4http://ronaldbradford.com/blog/optimizing-sql-performance-the-art-of-elimination-2010-07-08/

SELECT pages_id, pages_livestats_code, pages_title, pages_parent, pages_exhibid, pages_theme, pages_accession_numFROM pages WHERE pages_id = 0

5 minutes

6000 executions

0 is out of bounds

Page 53: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Capture & Analyze

DML is easy

SELECT is harder

HOW

4http://www.slideshare.net/ronaldbradford/capturing-analyzing-and-optimizing-mysql

Page 54: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

MySQL Binary (Redo) Log

mysqlbinlog

mk-query-digest

One Liner

HOW

4http://ronaldbradford.com/blog/mysql-dml-stats-per-table-2009-09-09/

Page 55: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Process List

General Log

tcpdump

Application

HOW

4

Page 56: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Capturing, Analyzing and Optimizing your SQL

http://www.slideshare.net/ronaldbradford/capturing-analyzing-and-optimizing-mysql

HOW

4

Page 57: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

/* Comment your queries */

The more products you have, the more developers you have, the more time you spend in code identification before you can even determine a resolution

EXPERT TIP

4

Page 58: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon33

Page 59: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

The database is slow.

My webpage takes five seconds to load.

PROBLEM

3

Page 60: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Evaluate the time taken in the database and all stages in between

SOLUTION

3

Page 61: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Client example showed a webpage taking 5 seconds to load. The html component was taking only 400 ms. Any MySQL performance improvement will only tune 8% of the total time.

EXAMPLE

3

Page 62: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Performance is important to end user

Performance is perception

WHY

3

Page 63: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Firebug - http://getfirebug.com/

Httpwatch - http://httpwatch.com/

Page speed - http://code.google.com/speed/page-speed/

YSlow - http://developer.yahoo.com/yslow/

wget/curlApplication code instrumentation

HOW

3

Page 64: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

http://www.stevesouders.com/

http://developer.yahoo.com/performance/rules.html

EXPERT TIP

3

Page 65: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon22

Page 66: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

I want to add new H/W. How do I change my application to support this?

PROBLEM

2

Page 67: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Develop a seamless integration that requires no code changes, no downtime and very little additional physical resources.

SOLUTION

2

Page 68: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Integrated monitoring and instrumentation

Deployed from Day 1

HOW

2

Page 69: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Seamless automated server deployment

Version Control

Build & Release

Runtime config management

Automated discovery

HOW

2

Page 70: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

API

One code path for business functionality

Implied business documentation

Enforced data exchange standard

Testability

HOW

2

Page 71: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Different levels of data availability

Read & Write

Read Only

No Access

Cached

HOW

2

Page 72: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Different principles for scalability

Read Scalability

Write Scalability

Caching

HOW

2

Page 73: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon11

Page 74: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

My website is slow?

PROBLEM

1

Page 75: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Seek professional advice.

Hire for example Ronald Bradford.

SOLUTION

1

Page 76: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

20+ years of system architecture, database design and performance tuning.

Employment as Senior Consultant with both Oracle Corporation (96-99) and MySQL Inc (06-08)

WHY

1

Page 77: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

http://ronaldbradford.com

[email protected]

Take my business card

HOW

1

Page 78: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

RR

Page 79: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Monitoring. Before, during and after NOW.

You may not be able to predict the future but you can preempt the future.

Choose the best product and features for you needs.

The best SQL statement is the one you never have to execute.

RECAP

R

Page 80: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

3 levels of real time data access.Read/Write, Read and no access

3 aspects of scalability.Read, Write and Caching

Operate below 90% capacity. That 10% is your insurance.

RECAP

R

Page 81: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

My web pages are timing out

My website takes a long time to aggregate results

Not covered

R

Page 82: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

Successful MySQL Scalability1. Integrated monitoring & instrumentation2. Seamless automated server deployment3. Disaster is inevitable4. Application Programming Interface5. Support different levels of data availability6. Support different scalability principles

REFERENCE

R

Page 83: Common MySQL Scalability Mistakes and how to avoid them

The most common MySQL scalability mistakes, and how to avoid them.@RonaldBradford #surgecon

http://ronaldbradford.com

[email protected]

CONTACT

C