Query Optimization
Query Optimization
http://www.percona.com/training/
Type is Range. BETWEEN, IN() and < > are also ranges.
Number of rows to examine has increased; we are not specific enough.
“How many releases per year, and what are their types”
http://dev.mysql.com/doc/refman/5.7/en/index-hints.html
© 2011 - 2016 Percona, Inc. 88 / 138
USE INDEX
Tell the optimizer to consider only the named index.
mysql> SELECT * FROM title USE INDEX (py_t)
-> WHERE production_year = 2009;
Example:
See which columns in composite index are used:
"used_columns": [
"first_name",
"last_name"
]
Intersection
SELECT * FROM title where title = 'Bambi' and production_year = 1927
SELECT id FROM title WHERE (title ='Pilot' and production_year > 1977)
UNION
SELECT id FROM title WHERE (episode_nr = 1 and production_year > 1977);
Create a filter:
INSERT INTO query_rewrite.rewrite_rules (pattern, replacement)
VALUES (
"SELECT name.id, name.name FROM imdb.name
LEFT JOIN imdb.aka_name ON name.id = aka_name.person_id
WHERE name.name = ?",
"SELECT name.id, name.name FROM imdb.name
WHERE name.name = ?");
CALL query_rewrite.flush_rewrite_rules();
SELECT * FROM title WHERE season_nr != 6 AND title LIKE 'Best of%';
SELECT t.title
FROM name n
INNER JOIN cast_info i ON n.id = i.person_id
INNER JOIN title t ON i.movie_id = t.id
INNER JOIN char_name c ON c.id = i.person_role_id
WHERE n.name = 'Brosnan, Pierce'
AND t.kind_id = 1
AND c.name='James Bond';
http://bit.ly/220ln2V