fixing common mistakes and myths - usenix · how to stop hating mysql: fixing common mistakes and...

54
How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group, www.pythian.com [email protected] LISA 2008

Upload: others

Post on 01-Apr-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

How to Stop Hating MySQL:Fixing Common Mistakes and Myths

Sheeri K. CabralDatabase Team Lead

The Pythian Group, www.pythian.com

[email protected] 2008

Page 2: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Who I Am

● MySQL DBA

● MySQL User Group

● MySQL Podcast (OurSQL, on hiatus), videos (technocation.org)

● Lots of community stuff

Page 3: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Myths about MySQL

● Uses too much memory

● Slow

● I need more features!

Page 4: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

More Myths about MySQL

● Don't use ENUM

● Schema changes take forever

● You have to restart to log

● No partitioning

Page 5: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

www.pythian.com/blogs/1168/why-you-want-to-switch-to-mysql-51

Page 6: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

MySQL uses too much memory!

● INFORMATION_SCHEMA.GLOBAL_VARIABLES (5.1)

● SHOW GLOBAL VARIABLES (5.0)

● LIKE– '%cache%'– '%buffer%'

Page 7: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Common Mistake: Wasting Memory

● bdb_cache_size

● bdb_log_buffer_size

● 32-bit operating sytem

Page 8: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Myth: Query Cache Is Faster!

● Use memcached for caching common queries

● query_cache_type– 1 or ON– 2 or DEMAND – SQL_CACHE– SQL_NO_CACHE

● Default query_cache_size=0

Page 9: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

MyISAM Index Cache

● key_buffer_size – globally allocated on startup

● How much is being used?– STATUS variables– Key_blocks_unused vs. Key_blocks_used– (Key_blocks_% * key_cache_block_size) / key_buffer_size)

Page 10: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

InnoDB Buffer Pool

● innodb_buffer_pool_size – globally allocated on startup

● How much is being used?– STATUS variables– Innodb_buffer_pool_pages_free– Innodb_buffer_pool_pages_total

Page 11: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Allocated Per-thread

● binlog_cache_size = 32 Kb

● max_binlog_cache_size = 4 Gb

● net_buffer_length = 16 Kb– 1 Mb max– max_allowed_packet

Page 12: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Per-Thread, Allocated As Needed

● join_buffer_size – memory buffer for joins not using indexes

● read_buffer_size – memory buffer for sequential table scans

● read_rnd_buffer_size – memory buffer for random table seeks

Page 13: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Per-Thread, Allocated As Needed● preload_buffer_size – when pre-loading indexes

● sort_buffer_size – for sorting

● myisam_sort_buffer_size– Index sorting only – REPAIR, OPTIMIZE, creating indexes

Page 14: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Temporary Tables

● tmp_table_size

● max_heap_table_size

● Large rows, BLOBs written to disk

Page 15: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Performance Caches

● Global Variables– table_cache

● STATUS variables Opened_tables

– thread_cache_size● STATUS variables Connections and Threads_created

Page 16: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Size Matters

● Larger/fragmented data/indexes use more memory

● Larger/fragmented data/indexes take more time to search

● Clustered indexes

● innodb_file_per_table

Page 17: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Size Matters – Data Diet

● OPTIMIZE – how often?

● Purge/archive regularly

● Follow large deletes with OPTIMIZE

Page 18: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Size Matters – Schema Diet

● IP addresses

● TIMESTAMP vs DATETIME

● Strings vs. Numbers vs. ENUM/SET

Page 19: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Myth: MySQL is Slow

● Memory/disk tradeoffs

● RAID

● Temporary tables– Created_tmp_tables– Created_tmp_disk_tables

Page 20: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Disk I/O

● Many data changes– Binary logs– Data– Index

● INSERT DELAYED● Batch update/deletes

Page 21: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Network

● Large queries vs. small– CPU compute time– Network traffic– Large data sets

● INSERT....ON DUPLICATE KEY UPDATE

Page 22: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Size Matters, So Does Performance

● Choosing Correct Table Types:– MyISAM– InnoDB– BLACKHOLE– MERGE– MEMORY– ARCHIVE

Page 23: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Query Optimization

● Use EXPLAIN

http://dev.mysql.com/doc/refman/5.1/en/using-explain.html

www.pythian.com/blogs/wp-content/uploads/explain-diagram.pdf

Page 24: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Know What You're Doing

● Subqueries

● VIEW

● TRIGGER

Page 25: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Know What You're Doing

● LIMIT

● Stored Procedures– compiled per thread

● Indexes– Selectivity– Functions– Overhead vs. utility

Page 26: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

More Features: Do Not Want!

● Look into Drizzle, a MySQL fork

● http://www.drizzleproject.org

Page 27: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

Feedback

● Questions?

● Comments?

● Why else do you hate MySQL?

[email protected]

Page 28: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

1

How to Stop Hating MySQL:Fixing Common Mistakes and Myths

Sheeri K. CabralDatabase Team Lead

The Pythian Group, www.pythian.com

[email protected] 2008

Page 29: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

2

Who I Am

● MySQL DBA

● MySQL User Group

● MySQL Podcast (OurSQL, on hiatus), videos (technocation.org)

● Lots of community stuff

Page 30: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

3

Myths about MySQL

● Uses too much memory

● Slow

● I need more features!

Page 31: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

4

More Myths about MySQL

● Don't use ENUM

● Schema changes take forever

● You have to restart to log

● No partitioning

Page 32: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

5

www.pythian.com/blogs/1168/why-you-want-to-switch-to-mysql-51

Page 33: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

6

MySQL uses too much memory!

● INFORMATION_SCHEMA.GLOBAL_VARIABLES (5.1)

● SHOW GLOBAL VARIABLES (5.0)

● LIKE– '%cache%'– '%buffer%'

Page 34: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

7

Common Mistake: Wasting Memory

● bdb_cache_size

● bdb_log_buffer_size

● 32-bit operating sytem

Page 35: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

8

Myth: Query Cache Is Faster!

● Use memcached for caching common queries

● query_cache_type– 1 or ON– 2 or DEMAND – SQL_CACHE– SQL_NO_CACHE

● Default query_cache_size=0

Page 36: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

9

MyISAM Index Cache

● key_buffer_size – globally allocated on startup

● How much is being used?– STATUS variables– Key_blocks_unused vs. Key_blocks_used– (Key_blocks_% * key_cache_block_size) / key_buffer_size)

Page 37: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

10

InnoDB Buffer Pool

● innodb_buffer_pool_size – globally allocated on startup

● How much is being used?– STATUS variables– Innodb_buffer_pool_pages_free– Innodb_buffer_pool_pages_total

Page 38: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

11

Allocated Per-thread

● binlog_cache_size = 32 Kb

● max_binlog_cache_size = 4 Gb

● net_buffer_length = 16 Kb– 1 Mb max– max_allowed_packet

Page 39: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

12

Per-Thread, Allocated As Needed

● join_buffer_size – memory buffer for joins not using indexes

● read_buffer_size – memory buffer for sequential table scans

● read_rnd_buffer_size – memory buffer for random table seeks

Page 40: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

13

Per-Thread, Allocated As Needed● preload_buffer_size – when pre-loading indexes

● sort_buffer_size – for sorting

● myisam_sort_buffer_size– Index sorting only – REPAIR, OPTIMIZE, creating indexes

Page 41: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

14

Temporary Tables

● tmp_table_size

● max_heap_table_size

● Large rows, BLOBs written to disk

Page 42: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

15

Performance Caches

● Global Variables– table_cache

● STATUS variables Opened_tables

– thread_cache_size● STATUS variables Connections and Threads_created

Page 43: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

16

Size Matters

● Larger/fragmented data/indexes use more memory

● Larger/fragmented data/indexes take more time to search

● Clustered indexes

● innodb_file_per_table

Page 44: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

17

Size Matters – Data Diet

● OPTIMIZE – how often?

● Purge/archive regularly

● Follow large deletes with OPTIMIZE

Page 45: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

18

Size Matters – Schema Diet

● IP addresses

● TIMESTAMP vs DATETIME

● Strings vs. Numbers vs. ENUM/SET

Page 46: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

19

Myth: MySQL is Slow

● Memory/disk tradeoffs

● RAID

● Temporary tables– Created_tmp_tables– Created_tmp_disk_tables

Page 47: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

20

Disk I/O

● Many data changes– Binary logs– Data– Index

● INSERT DELAYED● Batch update/deletes

Page 48: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

21

Network

● Large queries vs. small– CPU compute time– Network traffic– Large data sets

● INSERT....ON DUPLICATE KEY UPDATE

Page 49: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

22

Size Matters, So Does Performance

● Choosing Correct Table Types:– MyISAM– InnoDB– BLACKHOLE– MERGE– MEMORY– ARCHIVE

Page 50: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

23

Query Optimization

● Use EXPLAIN

http://dev.mysql.com/doc/refman/5.1/en/using-explain.html

www.pythian.com/blogs/wp-content/uploads/explain-diagram.pdf

Page 51: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

24

Know What You're Doing

● Subqueries

● VIEW

● TRIGGER

Page 52: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

25

Know What You're Doing

● LIMIT

● Stored Procedures– compiled per thread

● Indexes– Selectivity– Functions– Overhead vs. utility

Page 53: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

26

More Features: Do Not Want!

● Look into Drizzle, a MySQL fork

● http://www.drizzleproject.org

Page 54: Fixing Common Mistakes and Myths - USENIX · How to Stop Hating MySQL: Fixing Common Mistakes and Myths Sheeri K. Cabral Database Team Lead The Pythian Group,  cabral@pythian.com

27

Feedback

● Questions?

● Comments?

● Why else do you hate MySQL?

[email protected]