Database index
Upcoming SlideShare
Loading in...5
×
 

Database index

on

  • 105 views

How to create indexes. Where to start and best practices

How to create indexes. Where to start and best practices

Statistics

Views

Total Views
105
Views on SlideShare
105
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Database index Database index Presentation Transcript

  • DATABASE INDEX Sql Server, Mysql, Oracle
  • Database Without Index  Lets assume Sql Server, MySql and Oracle doesn’t support Index.  How to handle this siltation?  Why Indexes has introduced in the database?
  • Benefit of In Built Index  Data synchronization  When data is inserted in the table, same data is also inserted into the index.  When data is deleted from the table same, data is also deleted from the index.  When data is updated in the table, same data is also updated in the index.
  • How To Create Indexes? CREATE TABLE tblEmployee( ntEmployeeID BIGINT PRIMARY KEY, vcEmpName VARCHAR(200), dtDoj DATETIME, btGender BIT, vcSkill VARCHAR(2000), moSalary MONEY, ntHirarchyID BIGINT )
  • Common Misconception  Table design doesn’t decide index.  It may help some extent .  Primary key should be primary index (Clustered index) of a table.  All tables should have a clustered index on a primary key column
  • Where To Start?  OLAP (Online Analytical Processing)  OLTP (Online Transaction Processing)  What data is needed from a table?  What queries are executing on a table?
  • How To Know What Queries Are Executing ?  Find out all the in-lines queries.  Find out all the queries from stored procedures and database functions.  Use DMVS or metadata tables to get queries.
  • Steps To Create Indexes  Identify the application nature(OLAP OLTP)  OLAP: Create index if necessary.  OLTP: Create index if very necessary.  OLAP + OLTP  Sort the all queries on the base of frequency of execution and query execution time.
  • Rule 1  Create indexes on a table if the queries which fetch data from a table have at least following clauses: Clause Logical Processing Order ON 1 WHERE 2 GROUP BY 3 HAVING 4 ORDER BY 5 DISTINCT 6 UNION NA EXCEPT NA INTERSECT NA
  • Rule 2  Create index on the fields in which data are filtered only using following operators in WHERE clause, ON clause and HAVING clause: Operator Type = Equality < Range > Range <= Range >= Range IN Range BETWEEN Range LIKE Range
  • Rule 3  Don't create indexes on the columns which are expression in the query.  We will discuss later how to create indexes of such predicates.
  • Order of column name in an index  Index 1: CREATE INDEX NCI_a_b ON tblEmployee(a,b)  Index 2: CREATE INDEX NCI_b_a ON tblEmployee(b,a)  Here index NCI_a_b and NCI_b_a are two totally different indexes.
  • Rule 4  Try to avoid creating single column indexes if multi column index is possible.  If we are creating multi column index, keep columns order according to logical order of processing of different clauses.
  • Rule 5  Keep the all columns with equality operator first.  One columns with range operators is allowed .  If columns with range operator has not included then only columns in GROUP BY Clause or ORDER BY clause should be included.
  • Rule 6  If there are two columns with equality operator then column which has most number of distinct values should be included first.