2. Agenda
Scenario
Index Optimization
Explain
Explain Types
Explain Extra
Optimizer Hints
Selecting Queries To Optimize
General Query Log
3. Scenario
Poor indexes are same has not having indexes and is the best way to kill the
application.
Good selectivity on index fields.
Selectivity= no. of distinct records in the table.
On multiple column indexes the order fields within the index definitions is
important.
Over indexing is cumbersome and can be overhead.
The more index (the data) records can fit into one single block of memory the
faster will be queries.
4. Index Optimization
An index on the whole column is not always necessary.
Composite indexes ca be used for searches on the first column(s) index.
Instead index just a prefix of a column.
Prefix indexes take less space and the operations are faster.
Minimize the size of PRIMARY KEYs that are used as references in other tables.
Using and auto_increment column can be more optimal.
A FULLTEXT index is useful for
Word searches in text
Searches on several columns
5. Continue…
Query execution plan
With EXPLAIN the query is sent all the way to the optimizer, but not to the storage
engine
Instead EXPLAIN returns the query execution plan
6. Explain
Explain tells about
In which order tables are read
What type of read operations are made
Which indexes could have been used
How tables refer to each other
How many rows the optimizer estimates to retrieve from each table
7. Explain Types
Term Usage
system The table has only one row
const At the most one matching row, treated as a constant
eq_ref One row per row from previous tables
ref Several rows with matching index value
ref_or_null Like ref, plus NULL values
index_merge Several index searches are merged
unique_subquery Same as ref for some subqueries
index_subquery As above for non-unique indexes
range A range index scan
index The whole index is scanned
ALL A full table scan
8. Explain Extra
Term Usage
Using index The result is created straight from the index
Using where Not all rows are used in the result
Distinct Only a single row is read per row combination
Not exists A LEFT JOIN missing rows optimization is used
Using filesort An extra row sorting step is done
Using temporary A temporary table is used
Range checked for each
record
The read type is optimized individually for each combination of rows from the
previous tables
9. Optimizer Hints
Term Usage
STRAIGHT_JOIN Forces the optimizer to join the tables in the given order
SQL_BIG_RESULTS Together with GROUP BY or DISTINCT tells the server to use disk-based temp
tables
SQL_BUFFER_RESULTS Tells the server to use a temp table, thus releasing locks early (for table-locks)
USE INDEX Hints to the optimizer to use the given index
FORCE INDEX Forces the optimizer to use the index (if possible)
IGNORE INDEX Forces the optimizer not the use the index
10. Selecting Queries To Optimize
The slow query log
Logs all queries that take longer than long_query_time
Can also log all queries that don’t use indexes with
--log-queries-not-using-indexes
To log slow administrative commands use
--log-slow-admin-statements
To analyze the contents of the slow log use mysqldumpslow
11. General Query Log
Reads vs. writes
Simple queries vs. complex queries
etc