MySQL Database Indexing By Abdul Barek tekSymmetry, LLC 12th January, 2010
What is Indexing? Like our text/reference books indexing 1000 pages of contents (Data) 4-5 pages indexes (meta data) to retrieve data quickly Indexes with page number (pointer)
Advantages Quick (less time) in data retrieval  Less work for DB Engine in data searching
Disadvantages Requires extra space (4-5 pages for books) Create/update/delete index when DB insert/update/delete on data Using too many indexes will slow down database server (I.e.; index for ‘the’, ‘on’ of books)
Fetch >> Operation 95% fetch/data retrieval 5% insert/update/delete So why not indexing?
How to create/drop  index CREATE INDEX indexName ON tableName(columnName) DROP INDEX indexName ON tableName
A students table (without index on name field)
Select statement without index EXPLAIN SELECT * FROM ‘students’  WHERE name =‘shishir’
Select statement with index EXPLAIN SELECT * FROM ‘students’  WHERE name =‘shishir’
Where will we apply index? All fields in where clause  All foreign keys Where will we not apply index? Fields which is almost constant   - select * from news where  active=1
Where index will work Column = value LIKE ‘abc%‘, Full text search Comparison(<, <=, >, >=, BETWEEN) All joins with foreign keys     Where index will not work LIKE ‘%abc%‘ LIKE ‘%abc’
Types of mysql indexing Single Column Select * from table where name=‘abc’ Multi-column Select * from table where name=‘abc’ AND age=18 create index indexName on table(col1,col2..) Multi column will be single column when ‘OR’ Select * from table where name=‘abc’ OR age=18
MySQL Indexing methods
Indexing graphical example (B+)
Indexing graphical example (B+) - cont
Indexing graphical example (B+) - Range
Indexing graphical example (Hash)
Question?

Mysql Indexing

  • 1.
    MySQL Database IndexingBy Abdul Barek tekSymmetry, LLC 12th January, 2010
  • 2.
    What is Indexing?Like our text/reference books indexing 1000 pages of contents (Data) 4-5 pages indexes (meta data) to retrieve data quickly Indexes with page number (pointer)
  • 3.
    Advantages Quick (lesstime) in data retrieval Less work for DB Engine in data searching
  • 4.
    Disadvantages Requires extraspace (4-5 pages for books) Create/update/delete index when DB insert/update/delete on data Using too many indexes will slow down database server (I.e.; index for ‘the’, ‘on’ of books)
  • 5.
    Fetch >> Operation95% fetch/data retrieval 5% insert/update/delete So why not indexing?
  • 6.
    How to create/drop index CREATE INDEX indexName ON tableName(columnName) DROP INDEX indexName ON tableName
  • 7.
    A students table(without index on name field)
  • 8.
    Select statement withoutindex EXPLAIN SELECT * FROM ‘students’ WHERE name =‘shishir’
  • 9.
    Select statement withindex EXPLAIN SELECT * FROM ‘students’ WHERE name =‘shishir’
  • 10.
    Where will weapply index? All fields in where clause All foreign keys Where will we not apply index? Fields which is almost constant - select * from news where active=1
  • 11.
    Where index willwork Column = value LIKE ‘abc%‘, Full text search Comparison(<, <=, >, >=, BETWEEN) All joins with foreign keys Where index will not work LIKE ‘%abc%‘ LIKE ‘%abc’
  • 12.
    Types of mysqlindexing Single Column Select * from table where name=‘abc’ Multi-column Select * from table where name=‘abc’ AND age=18 create index indexName on table(col1,col2..) Multi column will be single column when ‘OR’ Select * from table where name=‘abc’ OR age=18
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.