Converge -- MySQL Workshop
-
Upload
davie-stokes -
Category
Technology
-
view
97 -
download
1
description
Transcript of Converge -- MySQL Workshop
Databases Can Be a Mystery, Misery, or Magnificent
● You do not need to be a DBA
● Nothing wrong with being a DBA● You should think in sets
● 'Relational Calculus for $500, Alex.'● Let the Database do the Heavy Lifting
● Check Return Codes!!!● Select What you need for speed
● Yes, YOU do need to do backups● And know how to restore from them
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
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 – 9 times faster!
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 useful, no BIGINTs for age.
18,446,744,073,709,551,615 is pretty old!
Integers
Type Storage (bytes)
Minimum Signed
Maximum Signed
Minimum Unsigned
Maximum Unsigned
TINYINT 1 -128 127 0 255
SMALLINT 2 -32768 32768 0 65535
MEDIUMINT
3 -8388608 8388607 0 16777215
INT 4 -2147483648 2147483647 0 4294967295
BIGINT 8 -9223372036854
775808
9223372036854775807
0 18446744073709551615
Are you really going to have 18,446,744,073,709,551,615 customer id numbers?
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.
Query Plan not lockable with MySQL
Each time MySQL gets a query it will optimize 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)
Clue: You want FAST!
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
Read ALL rows in table
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!
What the heck is a B-Tree?!??!?!
Using WHERE
274 Records Read!
Previous query w/o INDEX
No index used and all records in table readto find 274 records
How to find index(es) already in use
OR ...
\G forVertical
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
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;
Gee, we add all those qualifiersand it still has to readAll those records AND we get a temp table plus a file sort!
And we ONLY wanted 20 records!!!
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 complexitySELECT 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;
****
****
****
****
****
****
***
1. r
ow *
****
****
****
****
****
****
**
id
: 1 s
ele
ct_t
ype
: SIM
PL
E
t
abl
e: f
ilm
type
: ALL
poss
ible
_key
s: P
RIM
AR
Y
key
: NU
LL
key
_len
: NU
LL
re
f: N
UL
L
ro
ws:
100
0
Ext
ra: N
ULL
****
****
****
****
****
****
***
2. r
ow *
****
****
****
****
****
****
**
id
: 1 s
ele
ct_t
ype
: SIM
PL
E
tabl
e: i
nven
tory
ty
pe: r
efpo
ssib
le_k
eys:
PR
IMA
RY,
idx_
fk_f
ilm_i
d
ke
y: id
x_fk
_film
_id
key
_len
: 2
re
f: s
akila
.film
.film
_id
r
ow
s: 2
E
xtra
: Usi
ng in
dex
****
****
****
****
****
****
***
3. r
ow *
****
****
****
****
****
****
**
id
: 1 s
ele
ct_
type
: SIM
PL
E
tab
le: r
ent
al
ty
pe: r
efpo
ssib
le_k
eys:
idx_
fk_i
nve
ntor
y_id
,idx_
fk_c
ust
omer
_id
key:
idx_
fk_i
nven
tory
_id
key
_len
: 3
re
f: s
akila
.inve
ntor
y.in
vent
ory
_id
r
ow
s: 1
E
xtra
: Usi
ng w
here
****
****
****
****
****
****
***
4. r
ow *
****
****
****
****
****
****
**
id
: 1 s
ele
ct_t
ype
: SIM
PL
E
t
abl
e: c
usto
mer
ty
pe: e
q_re
fpo
ssib
le_k
eys:
PR
IMA
RY,
idx_
fk_a
ddre
ss_
id
ke
y: P
RIM
AR
Y
k
ey_l
en: 2
ref:
sak
ila.r
enta
l.cus
tom
er_
id
ro
ws:
1
Ext
ra: N
ULL
****
****
****
****
****
****
***
5. r
ow *
****
****
****
****
****
****
**
id
: 1 s
ele
ct_t
ype
: SIM
PL
E
tabl
e: a
ddre
ss
type
: eq_
ref
poss
ible
_key
s: P
RIM
AR
Y
ke
y: P
RIM
AR
Y
k
ey_l
en: 2
ref:
sak
ila.c
usto
me
r.add
ress
_id
r
ow
s: 1
E
xtra
: NU
LL5
row
s in
se
t (0
.00
sec
)
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 searching for which means less data to look-up,
one less read into the data
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
Check Return Codes (please)<?php$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit();}
if (!mysqli_query($link, "SET a=1")) { printf("Errorcode: %d\n", mysqli_errno($link));}
Hard to teach all in a few minutes
MySQL Central @ Oracle Open WorldMySQL Central @ Oracle Open World
Five days with the MySQL Engineers, innovative customers (Facebook, Twitter, Playful Play, Booking, DropBox, Paypal, & you), and the top professionals from the MySQL Community.
Starts September 28th in San Francisco!