The document discusses MySQL optimization and indexing principles. It provides examples of queries with and without indexes and uses the EXPLAIN statement to analyze query performance. Indexing types for MyISAM and InnoDB storage engines are explained. Considerations for when indexes can and cannot be used are also covered. The document is presented by someone who works with MySQL and offers their contact information for questions.
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
mysql optimization
1. MySQL Optimization
Ha noi php day 2009
vanchinh@gmail.com
19/12/2009 Ha Noi - 2009 1
2. Content
Introduction
Example
Indexing Principles
Considerations
Explain
General discussion
19/12/2009 Ha Noi - 2009 2
3. Introduction
Ha noi php day 2009
vanchinh@gmail.com
19/12/2009 Ha Noi - 2009 3
4. Who am i
I working in NaisCorp (socbay.com)
I’ve been using MySQL personally for
2 years and professionally for over 1 years.
Contact info:
Personal: vanchinh@gmail.com
work: chinhnv@socbay.com
Web: http://ffx.socbay.com
19/12/2009 Ha Noi - 2009 4
5. Example
Ha noi php day 2009
vanchinh@gmail.com
19/12/2009 Ha Noi - 2009 5
6. Example
SELECT * FROM news WHERE cate_id =1
ORDER BY createdate DESC LIMIT 0,10;
SELECT * FROM news WHERE cate_id =1 or
cate_id=2 ORDER BY createdate DESC
LIMIT 0,10;
SELECT * FROM news WHERE id <
12345678 ORDER BY createdate DESC LIMIT
0, 10
19/12/2009 Ha Noi - 2009 6
7. Example
Query Parsing and Optimizing
Query
Parse
Thread 1
Thread 2
Thread 3 Optimize
Thread 4
MyISAM Key Buffer
Engine API
MyISAM
…
Thread 5
Storage
Execute (ha_myisam)
(fetch) *.MYD, *.MYI
Thread n
(update) InnoDB Key Buffer
InnoDB
(ha_innodb) ibdataN
Results
Group By
Having
Order By
Group By and Order By can be done with indexes instead.
19/12/2009 Ha Noi - 2009 7
8. Example
Query Parsing and Optimizing
All parsing and optimizing steps are the same for all
storage engines
All ORDER BY / GROUP BY steps are the same
The only difference for a given storage engine is in
actually fetching the rows
Some types of indexes don’t exist on some table
types: e.g. FULLTEXT and SPATIAL indexes don’t
exist for InnoDB
19/12/2009 Ha Noi - 2009 8
9. Example
Explain SELECT * FROM news WHERE
cate_id =1 ORDER BY createdate DESC
LIMIT 0,10;
Run time: 0.9724s
19/12/2009 Ha Noi - 2009 9
10. Example
WHERE cate_id =1 Order By createdate Limit 0,10
2.000.000 2.000.000
10
19/12/2009 Ha Noi - 2009 10
11. Indexing Principles
Ha noi php day 2009
vanchinh@gmail.com
19/12/2009 Ha Noi - 2009 11
12. Optimization – Indexing Principles
Indexing principles
Single-column index
Multi-column index
Table news
19/12/2009 Ha Noi - 2009 12
13. Optimization – Indexing Principles
Single index
Table news
Multi index
19/12/2009 Ha Noi - 2009 13
14. Optimization – Indexing Principles
Basic Index Principles
Only one index can be used, per table, per query
Index definitions must match your real-world use
You don’t have to do anything special to maintain indexes
after creation
Creating/Dropping Indexes
CREATE INDEX cate ON news (cate_id)
DROP INDEX cate ON news;
ALTER TABLE news ADD INDEX cate
(cate_id);
ALTER TABLE news DROP INDEX cate;
19/12/2009 Ha Noi - 2009 14
15. Optimization – Indexing Principles
CREATE INDEX cate_date ON news (cate_id,
createdate)
Explain SELECT * FROM news WHERE
cate_id =1 ORDER BY createdate DESC
LIMIT 0,10
Run time: 0.0007 s
19/12/2009 Ha Noi - 2009 15
16. Optimization – Indexing Principles
WHERE cate_id =1 Order By createdate Limit 0,10
2.000.000 2.000.000
10
19/12/2009 Ha Noi - 2009 16
17. Optimization – Indexing Principles
Explain SELECT * FROM news WHERE
cate_id =1 or cate_id=2 ORDER BY createdate
DESC LIMIT 0,10
Run time: 1.0067S
19/12/2009 Ha Noi - 2009 17
18. Optimization – Indexing Principles
Cate_id=1 Order by Limit 0,10
1.000.000
Cate_id=2 3.000.000 10
2.000.000
19/12/2009 Ha Noi - 2009 18
19. Optimization – Indexing Principles
SELECT news.* FROM (
(SELECT id FROM news WHERE cate_id=1
ORDER BY createdate LIMIT 0,10)
UNION
(SELECT id FROM news WHERE cate_id=2
ORDER BY createdate LIMIT 0,10)
) as temp, news where news.id=temp.id ORDER
BY createdate DESC LIMIT 0,10;
19/12/2009 Ha Noi - 2009 19
27. Optimization – Considerations
When can indexes be used?
Simple PRIMARY KEY lookups
PRIMARY KEY(i) :: WHERE i = 5
Secondary KEY (index) lookups
INDEX(i) :: WHERE i = 7
Prefix lookups
INDEX(s) :: WHERE s LIKE “foo%”
INDEX(i, j) :: WHERE i = 5
Joins – index either table
19/12/2009 Ha Noi - 2009 27
28. Optimization – Considerations
When can’t indexes be used?
LIKE that starts with a wildcard
INDEX(s) :: WHERE s LIKE “%foo”
Bitwise operations
INDEX(i) :: WHERE i & 4
Non-prefix lookups
INDEX(i, j) :: WHERE j = 5
Using a function containing column ref
INDEX(s) :: WHERE foo(s) = “FOO”
INDEX(dt) :: WHERE unix_timestamp(dt) = 1
19/12/2009 Ha Noi - 2009 28
29. Optimization – Explain
Explain SELECT * FROM news WHERE
cate_id = 218103808 ORDER BY id DESC
LIMIT 0,10;
19/12/2009 Ha Noi - 2009 29
30. Optimization – Explain
Fields in EXPLAIN
id – (4.1+) The ID number of this SELECT
select_type – (4.1+) The type of this SELECT
table – The name of the table in question
type – The type of reading done on this table (system,
const, eq_ref, ref, ref_or_null, index_merge,
unique_subquery, index_subquery, range,
index, ALL)
possible_keys – The keys which were examined for
possible use
key – The key that was finally chosen for use
19/12/2009 Ha Noi - 2009 30
31. Optimization – Explain
Fields in EXPLAIN
key_len – The length of the part of the key that
was finally chosen for use
ref – Row reference; What is used to look up
row values from the index
rows – The approximate number of rows which
must be examined from this table using the chosen
optimizations
Extra – Any extra comments the server wanted
to add about this optimization path (free form)
19/12/2009 Ha Noi - 2009 31
32. General Discussion
Got any questions for me?
Ha noi php day 2009
vanchinh@gmail.com
19/12/2009 Ha Noi - 2009 32