My MySQL SQL Presentation

Post on 13-Jun-2015

365 views 4 download

Tags:

description

Brief introduction to indexing within MySQL

Transcript of My MySQL SQL Presentation

Indexingwith MySQL

Most of our data lives in MySQL

We want to get it as efficiently as possible

How?

Small tables?

Small tables? Not always practical

Small tables? Not always practical

Indexes?

Small tables? Not always practical

Indexes? OK, but how?

First things first:

First things first:What are indexes used for?

How do indexes work?

How do indexes work?Storage Types

BTree

BTree

Ordered key-value map

BTree

Ordered key-value mapMost common storage type

BTree

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

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'

Other storage types

Other storage types

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

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

How do indexes work?Pointers

Pointers

Pointers

1 Charles Mata Engineer 2

Aaron Macy Engineer 3 Kevin

Clement QA 4 Jeremy Tan

Manager

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

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

Index Types

Single column

Single column

Composite (multiple column)

Composite (multiple column)

Covering

Covering

Covering

Partial

Partial

Partial

Downsides?

Indexes take up

SPACE

Indexes slow down

INSERTS

Indexes confuse

OPTIMIZATION

Selectivity

Selectivity of a column isthe ratio between the number of

distinct values and thenumber of total values

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

Selectivity Tips

Always aim for >15%

Selectivity Tips

Always aim for >15%

Joins on columns with low selectivity are expensive

Selectivity Tips

Always aim for >15%

Joins on columns with low selectivity are expensive

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

Selectivity Tips

What makes a good index?

Use smallest data type possible

Consider partial indexes on varchar/char to increase selectivity

Small

Try to identify columns that will be used to filter data

Filter Columns

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

Join Columns

Covering Indexes

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

Group/Sort

Identify columns that will be used for grouping and sorting

Redundant Indexes

MySQL cannot use an indexif the columns do not form

a leftmost prefix of the index

Finding bad indexes

Slow query log

Slow query log

Slow query log

Explain

Explain

Explain