77
Indexing with MySQL

My MySQL SQL Presentation

Embed Size (px)

DESCRIPTION

Brief introduction to indexing within MySQL

Citation preview

Page 1: My MySQL SQL Presentation

Indexingwith MySQL

Page 2: My MySQL SQL Presentation

Most of our data lives in MySQL

We want to get it as efficiently as possible

How?

Page 3: My MySQL SQL Presentation

Small tables?

Page 4: My MySQL SQL Presentation

Small tables? Not always practical

Page 5: My MySQL SQL Presentation

Small tables? Not always practical

Indexes?

Page 6: My MySQL SQL Presentation

Small tables? Not always practical

Indexes? OK, but how?

Page 7: My MySQL SQL Presentation

First things first:

Page 8: My MySQL SQL Presentation

First things first:What are indexes used for?

Page 9: My MySQL SQL Presentation
Page 10: My MySQL SQL Presentation
Page 11: My MySQL SQL Presentation
Page 12: My MySQL SQL Presentation
Page 13: My MySQL SQL Presentation
Page 14: My MySQL SQL Presentation
Page 15: My MySQL SQL Presentation
Page 16: My MySQL SQL Presentation
Page 17: My MySQL SQL Presentation

How do indexes work?

Page 18: My MySQL SQL Presentation

How do indexes work?Storage Types

Page 19: My MySQL SQL Presentation

BTree

Page 20: My MySQL SQL Presentation

BTree

Ordered key-value map

Page 21: My MySQL SQL Presentation

BTree

Ordered key-value mapMost common storage type

Page 22: My MySQL SQL Presentation

BTree

Ordered key-value mapMost common storage typeQuickly find a given key and can be scanned in order

Page 23: My MySQL SQL Presentation

BTree

Ordered key-value mapMost common storage typeQuickly find a given key and can be scanned in orderWorks well with ranges

All records between 50 and 100All records starting with 'R'

Page 24: My MySQL SQL Presentation

Other storage types

Page 25: My MySQL SQL Presentation

Other storage types

RTree / Spatial Index - Identify 'close' values in 2+ dimensions - Useful for geographic databases

Page 26: My MySQL SQL Presentation

Other storage types

RTree / Spatial Index - Identify 'close' values in 2+ dimensions - Useful for geographic databases

Hash - Unordered key/value map - Faster than BTree, but terrible for ranges

Page 27: My MySQL SQL Presentation

How do indexes work?Pointers

Page 28: My MySQL SQL Presentation

Pointers

Page 29: My MySQL SQL Presentation

Pointers

1 Charles Mata Engineer 2

Aaron Macy Engineer 3 Kevin

Clement QA 4 Jeremy Tan

Manager

Page 30: My MySQL SQL Presentation

Pointers

1 Charles Mata Engineer 2

Aaron Macy Engineer 3 Kevin

Clement QA 4 Jeremy Tan

Manager

Aaron 2

Charles 1

Jeremy 4

Kevin 3

Page 31: My MySQL SQL Presentation

Pointers

1 Charles Mata Engineer 2

Aaron Macy Engineer 3 Kevin

Clement QA 4 Jeremy Tan

Manager

Aaron 2

Charles 1

Jeremy 4

Kevin 3

Page 32: My MySQL SQL Presentation

Index Types

Page 33: My MySQL SQL Presentation

Single column

Page 34: My MySQL SQL Presentation

Single column

Page 35: My MySQL SQL Presentation

Composite (multiple column)

Page 36: My MySQL SQL Presentation

Composite (multiple column)

Page 37: My MySQL SQL Presentation

Covering

Page 38: My MySQL SQL Presentation

Covering

Page 39: My MySQL SQL Presentation

Covering

Page 40: My MySQL SQL Presentation

Partial

Page 41: My MySQL SQL Presentation

Partial

Page 42: My MySQL SQL Presentation

Partial

Page 43: My MySQL SQL Presentation

Downsides?

Page 44: My MySQL SQL Presentation

Indexes take up

SPACE

Page 45: My MySQL SQL Presentation

Indexes slow down

INSERTS

Page 46: My MySQL SQL Presentation

Indexes confuse

OPTIMIZATION

Page 47: My MySQL SQL Presentation

Selectivity

Page 48: My MySQL SQL Presentation

Selectivity of a column isthe ratio between the number of

distinct values and thenumber of total values

Page 49: My MySQL SQL Presentation

Selectivity of a column isthe ratio between the number of

distinct values and thenumber of total values

Primary Keys and Unique ColumnsAlways Have Selectivity of 1

Page 50: My MySQL SQL Presentation

Selectivity Tips

Page 51: My MySQL SQL Presentation

Always aim for >15%

Selectivity Tips

Page 52: My MySQL SQL Presentation

Always aim for >15%

Joins on columns with low selectivity are expensive

Selectivity Tips

Page 53: My MySQL SQL Presentation

Always aim for >15%

Joins on columns with low selectivity are expensive

Watch out for columns like `status`, `gender`, and `active`

Selectivity Tips

Page 54: My MySQL SQL Presentation

What makes a good index?

Page 55: My MySQL SQL Presentation

Use smallest data type possible

Consider partial indexes on varchar/char to increase selectivity

Small

Page 56: My MySQL SQL Presentation

Try to identify columns that will be used to filter data

Filter Columns

Page 57: My MySQL SQL Presentation

Identify columns that will often be joined on/to from other tables

Join Columns

Page 58: My MySQL SQL Presentation

Covering Indexes

A query that can use a covering index will be substantially faster than one that has to read from the table

Page 59: My MySQL SQL Presentation

Group/Sort

Identify columns that will be used for grouping and sorting

Page 60: My MySQL SQL Presentation

Redundant Indexes

Page 61: My MySQL SQL Presentation

MySQL cannot use an indexif the columns do not form

a leftmost prefix of the index

Page 62: My MySQL SQL Presentation
Page 63: My MySQL SQL Presentation
Page 64: My MySQL SQL Presentation
Page 65: My MySQL SQL Presentation
Page 66: My MySQL SQL Presentation
Page 67: My MySQL SQL Presentation
Page 68: My MySQL SQL Presentation
Page 69: My MySQL SQL Presentation
Page 70: My MySQL SQL Presentation
Page 71: My MySQL SQL Presentation

Finding bad indexes

Page 72: My MySQL SQL Presentation

Slow query log

Page 73: My MySQL SQL Presentation

Slow query log

Page 74: My MySQL SQL Presentation

Slow query log

Page 75: My MySQL SQL Presentation

Explain

Page 76: My MySQL SQL Presentation

Explain

Page 77: My MySQL SQL Presentation

Explain