Upload
tasawr-interactive
View
2.050
Download
0
Embed Size (px)
DESCRIPTION
The Basic concept of MySql Indexing
Citation preview
MySQL Database IndexingMySQL Database Indexing
By Abdul BarekBy Abdul Barek
tekSymmetry, LLCtekSymmetry, LLC
12th January, 201012th January, 2010
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)
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
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)
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?
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
A students tableA students table(without index on name field)(without index on name field)
Select statement without indexSelect statement without index
EXPLAIN SELECT * FROM ‘students’ WHERE EXPLAIN SELECT * FROM ‘students’ WHERE name =‘shishir’name =‘shishir’
Select statement with indexSelect statement with index
EXPLAIN SELECT * FROM ‘students’ WHERE EXPLAIN SELECT * FROM ‘students’ WHERE name =‘shishir’name =‘shishir’
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
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’
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
MySQL Indexing methodsMySQL Indexing methods
Indexing graphical example (B+)Indexing graphical example (B+)
Indexing graphical example (B+) - Indexing graphical example (B+) - contcont
Indexing graphical example (B+) - Indexing graphical example (B+) - Range Range
Indexing graphical example (Hash)Indexing graphical example (Hash)
Question?Question?