The problem with frameworks and ORMs is the lackadaisical manner with which they write queries. Their focus is to straddle multiple database engines, not to write queries in the optimal way. The database layer is always the hardest layer to scale. Why would you put additional strain on your database to avoid writing SQL? This session focuses on understanding indices, queries, and optimization in MySQL.
Driving Behavioral Change for Information Management through Data-Driven Gree...
ORM or SQL? A Better Way to Query in MySQL
1. ORM OR SQL?
A BETTER WAY TO QUERY IN MYSQL
Matthew Barlocker
Lucid Software Inc
2. ABOUT “THE BARLOCKER”
•
•
•
Chief Architect at
Lucid Software Inc
Bachelors degree from
BYU in Computer
Science
I love to
•
•
•
•
•
play board games
go 4-wheeling
wrestle my sons
fly airplanes
Follow me on
nineofclouds.blogspot.com
14. FRAMEWORKS CREATE SIDE EFFECTS
Extra Joins
Wrong Kind of Joins
Default Ordering
Empty Array turned
into “IS NULL”
15. FRAMEWORKS HAVE OTHER GOALS
Make it easy to create a website for novices
Make it easy to access your data
Support all databases (by making an abstraction
layer for data persistence)
19. EXPLAIN HELPS WITH OPTIMIZATION
The select_type will vary based on what your
query is. SIMPLE is the best type.
The optimizer will select from possible_keys the
best key to index on. It’s choice will be in key.
If possible_keys is NULL, a full table scan will be
performed.
The rows column will give the estimated number
of rows to check, using the selected index.
EXPLAIN SELECT * FROM mytable;
http://dev.mysql.com/doc/refman/5.6/en/select-optimization.html
20. SIMPLE QUERIES ARE… SIMPLE
INSERT INTO (…) VALUES (…);
UPDATE … WHERE indexedfield = 5;
DELETE … WHERE indexedfield = 5;
SELECT * FROM … WHERE indexedfield = 5;
21. COMPLEX INDICES FIX SLOWNESS
Complex indices can fix file sorts and temporary
tables (from the explain statement)
A complex index contains multiple fields, in a
particular order.
The left-most prefix index can be used on its own.
SELECT * FROM t1 ORDER BY keypart1,
keypart2;
SELECT * FROM t1 WHERE keypart1 <
constant ORDER BY keypart1 DESC;
SELECT kp1, kp2 FROM t1 GROUP BY kp1,
kp2;
22. COVERED INDICES ARE THE FASTEST
A covered index prevents a query from getting
rows from the disk.
Covered indices will have “Using Index” in the
explain output.
SELECT kp1, kp2 FROM t1 WHERE kp1 IS
NOT NULL;
SELECT MAX(kp1) FROM t1;
23. JOINS ARE BAD
Joins require temporary table space and join
buffer.
Temporary (joined) tables don’t have indices.
Joins don’t always work in a sharded
environment.
Joins are less likely to be in the query cache.