MySQL Queries --
Damned
Lies and Best Guesses
Very few programmers really understand SQL or how to speed queries to their ...
SQL Structured Query Language
Structured Query Language (/ˈɛs kjuː ˈɛl/,[4] or /ˈsiːkwəl/; (SQL)[5][6][7][8]) is a
special...
You send SQL to the server …
The mysqld process will take your input
and parse it for VALID syntax.
Then it will build a q...
GOALs
1. Get the data that you need and only what
you need as fast as possible. No ‘SELECT *
FROM’
2. Avoid unnecessary di...
Cost Based Optimizer
C.5.6. Optimizer-Related Issues
MySQL uses a cost-based optimizer to determine the best way to resolv...
Query Plan not lockable with MySQL
Each time MySQL gets a query it will optimise
it!
It builds a list of statistics over t...
EXPLAIN
EXPLAIN is a tool to ask the server how it
wants to optimize the query.
Prepend to a QUERY
Example Table
Example Query
Example EXPLAIN
Example EXPLAIN with G
Q
u
er
y
#
A Quick Word on Indexes
Indexes allow you to go directly to the
record(s) you want (think SSN) instead of
reading all reco...
What the heck is a B-Tree?!??!?!
select_type
Using WHERE
274 Records Read!
Previous query w/o INDEX
No index used and
all records in table read
to find 274 re
How to find index(es) already in use
OR ...
A more common example
Optimizer estimates 8 reads to get desired info
Could use the PRIMARY key but does not!!
Has to read...
Slightly more complex query
SELECT a.Name as 'City',
b.Name as 'Country',
a.population
FROM City a
JOIN Country b
ON (a.Co...
Gee, we add all those qualifiers and it still has to read
All those records AND we get a temp table plus a file sort!
And ...
Visual Explain
MySQL 5.6 and
Workbench 6.1 use
JSON format output
to generate diagram.
Costs published with
5.7 and 6.1!
Yet a little deeper into complexity
SELECT CONCAT(customer.last_name, ', ', customer.first_name) AS
customer,
address.phon...
***************************1.row***************************
id:1
select_type:SIMPLE
table:film
type:ALL
possible_keys:PRIM...
A little easier to understand
Compound Indexes
We can use this index searching on
1. City, State, and Zip
2. City, State
3. City
Covering Indexes
ALTER TABLE city ADD INDEX
country_idx (CountryCode, Population);
The INDEX contains all the data we are
...
Musts for better queries
1. Read chapter 8 of the MySQL Manual
2. Join on like data types, INTs with INTS
3. Keep columns ...
Hard to teach all in a few minutes
MySQL Central @ Oracle Open WorldMySQL Central @ Oracle Open World
Five days with the MySQL Engineers,
innovative customer...
Questions and Answers
David.Stokes@Oracle.com
@stoker
Slideshare.net/davestokes
Southeast Linuxfest -- MySQL Queries-- - Damned Lies and Best Guesses
Upcoming SlideShare
Loading in...5
×

Southeast Linuxfest -- MySQL Queries-- - Damned Lies and Best Guesses

245

Published on

This session covers how to understand what MySQL is trying to tell you about queries.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
245
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Southeast Linuxfest -- MySQL Queries-- - Damned Lies and Best Guesses

  1. 1. MySQL Queries -- Damned Lies and Best Guesses Very few programmers really understand SQL or how to speed queries to their databases. This session covers that basics of relational calculus (no actual math/calculus will be demanded of attendees), how a RDMS like MySQL tries to optimize the query, and introduces query tuning. Dave Stokes david.stokes@oracle.com @stoker slideshare.net/davestokes
  2. 2. SQL Structured Query Language Structured Query Language (/ˈɛs kjuː ˈɛl/,[4] or /ˈsiːkwəl/; (SQL)[5][6][7][8]) is a special-purpose programming language designed for managing data held in a relational database management system (RDBMS). Originally based upon relational algebra and tuple relational calculus, SQL consists of a data definition language and a data manipulation language. The scope of SQL includes data insert, query, update and delete, schema creation and modification, and data access control. Although SQL is often described as, and to a great extent is, a declarative language (4GL), it also includesprocedural elements. relational algebra is an offshoot of first-order logic and of algebra of sets concerned with operations over finitary relations, usually made more convenient to work with by identifying the components of a tuple by a name (called attribute) rather than by a numeric column index, which is called a relation in database terminology. --Wikipedia
  3. 3. You send SQL to the server … The mysqld process will take your input and parse it for VALID syntax. Then it will build a query plan on how best to retrieve the data. Finally it goes to fetch the data. MySQL’s NoSQL queries that skip these steps are MUCH faster
  4. 4. GOALs 1. Get the data that you need and only what you need as fast as possible. No ‘SELECT * FROM’ 2. Avoid unnecessary disk/memory reads and disk writes. 3. Make data as compact as is usefull, no BIGINTs for zipcodes.
  5. 5. Cost Based Optimizer C.5.6. Optimizer-Related Issues MySQL uses a cost-based optimizer to determine the best way to resolve a query. In many cases, MySQL can calculate the best possible query plan, but sometimes MySQL does not have enough information about the data at hand and has to make “educated” guesses about the data. So MySQL wants to get your data as cheaply as possible and plans accordingly.
  6. 6. Query Plan not lockable with MySQL Each time MySQL gets a query it will optimise it! It builds a list of statistics over time to help keep track of data & speed retrieval of the data (5.6 lets you save/restore this information)
  7. 7. EXPLAIN EXPLAIN is a tool to ask the server how it wants to optimize the query. Prepend to a QUERY
  8. 8. Example Table
  9. 9. Example Query
  10. 10. Example EXPLAIN
  11. 11. Example EXPLAIN with G Q u er y #
  12. 12. A Quick Word on Indexes Indexes allow you to go directly to the record(s) you want (think SSN) instead of reading all records to find the one(s) wanted. But they require maintenance and overhead. Not a panacea!
  13. 13. What the heck is a B-Tree?!??!?!
  14. 14. select_type
  15. 15. Using WHERE 274 Records Read!
  16. 16. Previous query w/o INDEX No index used and all records in table read to find 274 re
  17. 17. How to find index(es) already in use
  18. 18. OR ...
  19. 19. A more common example Optimizer estimates 8 reads to get desired info Could use the PRIMARY key but does not!! Has to read all records 239 x 8 = 1,912 records to read
  20. 20. Slightly more complex query SELECT a.Name as 'City', b.Name as 'Country', a.population FROM City a JOIN Country b ON (a.CountryCode = b.Code) WHERE a.population > 3000000 AND b.LifeExpectancy > 66 ORDER BY b.name, a.Population LIMIT 20;
  21. 21. Gee, we add all those qualifiers and it still has to read All those records AND we get a temp table plus a file sort! And we ONLY wanted 20 records!!!
  22. 22. Visual Explain MySQL 5.6 and Workbench 6.1 use JSON format output to generate diagram. Costs published with 5.7 and 6.1!
  23. 23. Yet a little deeper into complexity SELECT CONCAT(customer.last_name, ', ', customer.first_name) AS customer, address.phone, film.title FROM rental INNER JOIN customer ON rental.customer_id = customer.customer_id INNER JOIN address ON customer.address_id = address.address_id INNER JOIN inventory ON rental.inventory_id = inventory.inventory_id INNER JOIN film ON inventory.film_id = film.film_id WHERE rental.return_date IS NULL AND rental_date + INTERVAL film.rental_duration DAY < CURRENT_DATE() LIMIT 5;
  24. 24. ***************************1.row*************************** id:1 select_type:SIMPLE table:film type:ALL possible_keys:PRIMARY key:NULL key_len:NULL ref:NULL rows:1000 Extra:NULL ***************************2.row*************************** id:1 select_type:SIMPLE table:inventory type:ref possible_keys:PRIMARY,idx_fk_film_id key:idx_fk_film_id key_len:2 ref:sakila.film.film_id rows:2 Extra:Usingindex ***************************3.row*************************** id:1 select_type:SIMPLE table:rental type:ref possible_keys:idx_fk_inventory_id,idx_fk_customer_id key:idx_fk_inventory_id key_len:3 ref:sakila.inventory.inventory_id rows:1 Extra:Usingwhere ***************************4.row*************************** id:1 select_type:SIMPLE table:customer type:eq_ref possible_keys:PRIMARY,idx_fk_address_id key:PRIMARY key_len:2 ref:sakila.rental.customer_id rows:1 Extra:NULL ***************************5.row*************************** id:1 select_type:SIMPLE table:address type:eq_ref possible_keys:PRIMARY key:PRIMARY key_len:2 ref:sakila.customer.address_id rows:1 Extra:NULL 5rowsinset(0.00sec)
  25. 25. A little easier to understand
  26. 26. Compound Indexes We can use this index searching on 1. City, State, and Zip 2. City, State 3. City
  27. 27. Covering Indexes ALTER TABLE city ADD INDEX country_idx (CountryCode, Population); The INDEX contains all the data we are searching for which means less data to look-up
  28. 28. Musts for better queries 1. Read chapter 8 of the MySQL Manual 2. Join on like data types, INTs with INTS 3. Keep columns as small as practical (PROCEDURE ANALYSE) 4. Maintain B-tree index with ANALYSE TABLE when things are quiet 5. Keep looking for improvments
  29. 29. Hard to teach all in a few minutes
  30. 30. MySQL Central @ Oracle Open WorldMySQL Central @ Oracle Open World Five days with the MySQL Engineers, innovative customers (Facebook, Twitter, Playful Play, Verizon, Paypal, & you), and the top professionals from the MySQL Community. Early Bird registration saves $500 before July 18th ! Starts September 28th in San Francisco!
  31. 31. Questions and Answers David.Stokes@Oracle.com @stoker Slideshare.net/davestokes
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×