18
MySQL Database Indexing MySQL Database Indexing By Abdul Barek By Abdul Barek tekSymmetry, LLC tekSymmetry, LLC 12th January, 2010 12th January, 2010

Mysql Indexing

Embed Size (px)

DESCRIPTION

The Basic concept of MySql Indexing

Citation preview

Page 1: Mysql Indexing

MySQL Database IndexingMySQL Database Indexing

By Abdul BarekBy Abdul Barek

tekSymmetry, LLCtekSymmetry, LLC

12th January, 201012th January, 2010

Page 2: Mysql Indexing

What is Indexing?What is Indexing?

Like our text/reference books indexingLike our text/reference books indexing 1000 pages of contents (Data)1000 pages of contents (Data) 4-5 pages indexes (meta data) to 4-5 pages indexes (meta data) to

retrieve data quicklyretrieve data quickly Indexes with page number (pointer)Indexes with page number (pointer)

Page 3: Mysql Indexing

AdvantagesAdvantages

Quick (less time) in data retrieval Quick (less time) in data retrieval Less work for DB Engine in data searchingLess work for DB Engine in data searching

Page 4: Mysql Indexing

DisadvantagesDisadvantages

Requires extra space (4-5 pages for Requires extra space (4-5 pages for books)books)

Create/update/delete index when Create/update/delete index when DB insert/update/delete on dataDB insert/update/delete on data

Using too many indexes will slow Using too many indexes will slow down database server (I.e.; index down database server (I.e.; index for ‘the’, ‘on’ of books)for ‘the’, ‘on’ of books)

Page 5: Mysql Indexing

Fetch >> OperationFetch >> Operation

95% fetch/data retrieval95% fetch/data retrieval

5% insert/update/delete5% insert/update/delete

So why not indexing?So why not indexing?

Page 6: Mysql Indexing

How to create/drop indexHow to create/drop index

CREATE INDEX indexName ON CREATE INDEX indexName ON tableName(columnName)tableName(columnName)

DROP INDEX indexName ON DROP INDEX indexName ON tableNametableName

Page 7: Mysql Indexing

A students tableA students table(without index on name field)(without index on name field)

Page 8: Mysql Indexing

Select statement without indexSelect statement without index

EXPLAIN SELECT * FROM ‘students’ WHERE EXPLAIN SELECT * FROM ‘students’ WHERE name =‘shishir’name =‘shishir’

Page 9: Mysql Indexing

Select statement with indexSelect statement with index

EXPLAIN SELECT * FROM ‘students’ WHERE EXPLAIN SELECT * FROM ‘students’ WHERE name =‘shishir’name =‘shishir’

Page 10: Mysql Indexing

Where will we apply index?Where will we apply index?

All fields in where clause All fields in where clause All foreign keysAll foreign keys

Where will we not apply index?Where will we not apply index?

Fields which is almost constantFields which is almost constant - select * from news where - select * from news where active=1active=1

Page 11: Mysql Indexing

Where index will workWhere index will work Column = valueColumn = value LIKE ‘abc%‘, Full text searchLIKE ‘abc%‘, Full text search Comparison(<, <=, >, >=, BETWEEN)Comparison(<, <=, >, >=, BETWEEN) All joins with foreign keysAll joins with foreign keys

Where index will not workWhere index will not work LIKE ‘%abc%‘LIKE ‘%abc%‘ LIKE ‘%abc’LIKE ‘%abc’

Page 12: Mysql Indexing

Types of mysql indexingTypes of mysql indexing

Single ColumnSingle Column Select * from table where name=‘abc’Select * from table where name=‘abc’

Multi-columnMulti-column Select * from table where name=‘abc’ AND age=18Select * from table where name=‘abc’ AND age=18

create index indexName on table(col1,col2..)create index indexName on table(col1,col2..)

Multi column will be single column Multi column will be single column when ‘OR’when ‘OR’

Select * from table where name=‘abc’ OR age=18Select * from table where name=‘abc’ OR age=18

Page 13: Mysql Indexing

MySQL Indexing methodsMySQL Indexing methods

Page 14: Mysql Indexing

Indexing graphical example (B+)Indexing graphical example (B+)

Page 15: Mysql Indexing

Indexing graphical example (B+) - Indexing graphical example (B+) - contcont

Page 16: Mysql Indexing

Indexing graphical example (B+) - Indexing graphical example (B+) - Range Range

Page 17: Mysql Indexing

Indexing graphical example (Hash)Indexing graphical example (Hash)

Page 18: Mysql Indexing

Question?Question?