SQL Query Optimization
SQL Query Optimization
and Indexing
How does a
database server
run a query ?
Server process
SQL Query
analysis
If new query?
Execution plan?
(Optimizer )
If cpu?
Table scan ?
index?
Require high
performance ??
Tradeoffs !!
Whats an Index??
adata structure
Retrieval
Inserts
Also
Denormalized db
=
faster 4 some
queries
+
slower for others
Simple is good
Integers easier to compare than characters
E.g.: Use MySQL built in types for date/time
String Types
VARCHAR and CHAR types
Their storage on disk is storage engine dependent
Usually the storage is different for disk, memory and
after retrieval from the storage engine
VARCHAR
-
-
length
e
grow
work!!!
-
Use VARCHAR , max col length > avg length, updates are
rare
CHAR
Fixed length
For data changing frequently, char better than varchar
For very short columns, CHAR(1) = 1 BYTE and
VARCHAR(1) = 2 BYTES
The siblings of char and varchar are binary and
varbinary data types
Good for comparing as bytes that characters.
IP Address
Checks
syntax
Query
process
or tree
is output
of parse
PARSE
OPTIMIZE
Calculate
cost and
gives out
estimated
plan and
an actual
plan
DATA STATISTICS
1. How many rows?
2. Unique data?
3. Does table span
over more than
one page?
EXECUTE
As per
plan
executio
n is
done
Into Indexing
TYPES
B-Tree Indexes
Hash Indexes
B-Tree
We use the term "B-Tree" for these indexes because
that's what MySQL uses in CREATE TABLE and other
statements
All the values are stored in order, and each leaf page is
the same distance from the root
Hash Indexes
Built on hash tables and useful for exact lookups that
use every column in the index
Memory storage engine only supports this in MySQL
Forms hash codes of the indexed columns and stores a
pointer to each row in hash table
E.g. :
Fname
Darshan
Bijesh
Jophin
Vivek
lname
Raj
Chandran
Joseph
Babu
Value
2323
Pointer to row 1
2458
Pointer to row 4
7437
Pointer to row 2
8784
Pointer to row 3
Clustered Indexes
Data blocks arranged in order to match the index
Only one clustered index possible on a given table
Faster retrieval if data accessed in asc or desc order
Using indices
Indexing the primary key
Usually automatically indexed to facilitate effective
information retrieval
Most effective access path
Other columns or combination of columns = secondary
index to improve performance in data retrieval
Secondary indexes
Indexes on other columns other than primary key
column
Create secondary indexes on tables that have more
reads than writes
Just copy of the db table but containing only the fields
specified in the index
Selec
t_typ
e
Tabl
e
Type Possibl
e_
Keys
Key
Key_
Length
Ref
Row
s
Id : select identifier
Select_type : type of select (Simple, Primary , Union ,
Dependent Union, Subquery, Dependent Subquery etc)
extr
a
Lock Contention??
User_id
(PK)
Name
status
100
100000
2) UPDATE user SET status=9 WHERE
user_id =100
DATA CONSISTENCY IS
BROKEN !!
If STATUS column is
indexed
1) DELETE FROM user WHERE
status = 9
Status
PK
100
101
12345
100000
User_id
(PK)
Name
status
Roger
100
Rafael
01
100000
Andy
Covering Index??
DB Engine ??
References
THANK YOU