mysql myisam storage engine

21
MySQL 5.6 MyISAM Kristian Köhntopp

Upload: kristian-koehntopp

Post on 22-Jan-2017

563 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: MySQL MyISAM Storage Engine

MySQL 5.6 MyISAMKristian Köhntopp

Page 2: MySQL MyISAM Storage Engine

MyISAM Facts

• Old ‘default engine’

• Very small files

• Can do FULLTEXT (badly), GIS (badly)

• Can do compound PK w/ auto_increment (bad idea)

• Table level locking (performance problem)

2

Page 3: MySQL MyISAM Storage Engine

MyISAM disadvantages

• No transactions.

• No table recovery (uses “REPAIR TABLE”)

• Small footprint = inefficient updates, fragmentation.

3

Page 4: MySQL MyISAM Storage Engine

MyISAM file structure4

.MYD file

RecordRecordRecordRecordRecordRecordRecorddeletedRecordRecorddeletedRecord

.MYI File

Index 1 Index 2

Page 5: MySQL MyISAM Storage Engine

MyISAM file structure

• Records packed next to each other in data file.

• Delete → Gap.

• Gap → Next Insert, or Append.

• FIXED vs. DYNAMIC (vs. COMPRESSED)

• Fragmentation (String extension!)

5

Page 6: MySQL MyISAM Storage Engine

MyISAM file structure

• Index: record number (FIXED), byte offset (DYNAMIC)

• Data pointer size variable

• myisam_data_pointer_size = 6 (256TB)

• ALTER TABLE t MIN_ROWS = n MAX_ROWS = m AVG_ROW_SIZE = p;

6

Page 7: MySQL MyISAM Storage Engine

MyISAM update storm

• UPDATE t SET somestring = CONCAT(somestring, somestring)

• Split record = Seek.

• Move to end of table = all indices updated.

• Either way: fragmentation, seeks.

7

Page 8: MySQL MyISAM Storage Engine

MyISAM file structure

• UPDATE t SET count = count + 1, and server crash?

• Some records updated. You do not know which.

• Recover from uncrashed slave, or have invalid data.

• Also, the table is marked as crashed.

8

Page 9: MySQL MyISAM Storage Engine

Compressed MyISAM

• Close all filehandles: FLUSH TABLES

• Run myisampack to compress data file: invalid index.

• Run myisamchk to rebuild index.

• Data footprint approx. 50% smaller.

• Read-only.

9

Page 10: MySQL MyISAM Storage Engine

MyISAM locking

• Table level locks: SHARED (read), EXCLUSIVE (write)

• Many concurrent SELECT statements, serialized write statements.

• Writes have precedence.

• Running statements have precedence.

10

Page 11: MySQL MyISAM Storage Engine

MyISAM locking

• SELECT high_priority …

• Writes (INSERT, UPDATE, DELETE, …)

• SELECT …

• Low priority (UPDATE LOW_PRIORITY …)

11

Page 12: MySQL MyISAM Storage Engine

MyISAM locking

• many SELECTs, some slow (4s runtime).

• foreach $i (@stmt) { $dbh->do($i) }

• INSERT blocks subsequent reads.

• INSERT blocked by running (slow) read.

• One insert every 4s.

12

Page 13: MySQL MyISAM Storage Engine

Explicit locking

• LOCK TABLES t1 READ, t2 READ, t3 WRITE;

• All locks taken at once: Avoids deadlocks.

• Unable to touch unlocked tables during lock: Ensures correctness and completeness.

• UNLOCK TABLES (or disconnect)

13

Page 14: MySQL MyISAM Storage Engine

Explicit locking

• How does explicit locking affect the MyISAM interlock scenario?

• Locking can actually make things faster.

• But will increase latency.

14

Page 15: MySQL MyISAM Storage Engine

Tuning MyISAM

• MyISAM caches MYI data, but no MYD data.

• System must have sufficient buffer cache: mysqld size must be limited.

• key_buffer_size = …

• Ideally: Sum of all MYI files.

15

Page 16: MySQL MyISAM Storage Engine

Tuning MyISAM

• key_cache_division_limit = 100

• The division point between the hot and warm sublists of the key cache buffer list (percent)

16

Page 17: MySQL MyISAM Storage Engine

Tuning MyISAM

• After a server restart, the key cache is cold.

• LOAD INDEX INTO CACHE <indexname>, <indexname2>, …

• This preloads the cache w/ linear disk accesses.

• Much faster than normal warmup.

17

Page 18: MySQL MyISAM Storage Engine

Tuning MyISAM

• Handling MyISAM index update storms:

• delayed_key_write = OFF|ON|ALL

• CREATE TABLE t (…) DELAY_KEY_WRITE = 1;

• Status variable Key_blocks_not_flushed

• Large number of unflushed blocks + crash = monster repair table

18

Page 19: MySQL MyISAM Storage Engine

Tuning MyISAM

• myisam_recover_options = FORCE,BACKUP

• may not be enough in the face of delayed key writes

• myisam_repair_threads = 1

• used to be broken with > 1

19

Page 20: MySQL MyISAM Storage Engine

Monitoring MyISAM

• Table_locks_immediate

• Table locks granted without waiting

• Table_locks_waited

• Table locks granted after wait

• 1% = dangerous, 3% = deadly

20

Page 21: MySQL MyISAM Storage Engine

MyISAM maintenance

• SHOW TABLE STATUS - data_free.

• OPTIMIZE TABLE to reclaim data_free.

• This is very slow and X-locks.

• ANALYZE TABLE - optimizer statistics.

• Also very slow (in MyISAM).

21