Tool it Up! - Session #3 - MySQL
-
Upload
toolitup -
Category
Technology
-
view
88 -
download
2
Transcript of Tool it Up! - Session #3 - MySQL
![Page 1: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/1.jpg)
Date: August 30 2013By: Patrick Demers
![Page 2: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/2.jpg)
Query Process
![Page 3: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/3.jpg)
Query Cache• Add some overhead to every query• Will invalidate any cache referencing a table
that changed• Cannot cache query using :
![Page 4: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/4.jpg)
Query Cache• Current Setting [ON]
must specify SQL_NO_CACHE to skip cache• Will speed up repeating query with the SAME
SYNTAX.• Compact with
FLUSH NO_WRITE_TO_BINLOG QUERY CACHE
![Page 5: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/5.jpg)
Example
![Page 6: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/6.jpg)
Variable Type• Smaller is usually better
● less space on disk / memory and CPU cache. Fewer CPU cycles to process
• Simple is good● Use Integer instead of string (collation / char. set)
• Avoid Null Column● Harder to optimize● Use More Space , require special processing.
![Page 7: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/7.jpg)
Variable Type • Current Table size 12gig• Test Table
● Initial : 312meg● After Type change : 297meg (+-
5%)
![Page 8: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/8.jpg)
Variable Type• Size of a Varchar is 65535• Why use Text then
● Stored separately● Allow innodb to store more row in a block● Use text if not retrieved most of the time● Will require an On Disk Temporary table (group by / order
by)● Use varchar for columns retrieved in most query
![Page 9: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/9.jpg)
Index• B-Tree Index
![Page 10: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/10.jpg)
Index• MySQL will let you index the same column
multiple times• Optimizer will check every index available• Size of the index depends on Variable Type• Indexing a null column requires more
processing
![Page 11: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/11.jpg)
Index• Optimize InnoDB Table when there's a lot of
Insert / Delete.• Don’t create index only thinking of your
'current query'• Test your query often. Adding record in a table
might change an index performance.• InnoDB will lock every row it thinks he needs.
No Index will lock the table.
![Page 12: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/12.jpg)
Index• Mysql can use part of an index as long as it's
left to right• Using a range in a where might prevent the
index to be used fully .● where o.date >= '2013-08-01' AND o.date <= '2013-08-30'
• String can be indexed partially (ex: 10 char on a 25 char field)
![Page 13: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/13.jpg)
EXPLAIN• Useful to check a query performance• Give you Info on what you should change on
your query•
![Page 14: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/14.jpg)
Explain Cheat Sheet part 1
http://www.pythian.com/blog/wp-content/uploads/explain-diagram1.pdf
![Page 15: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/15.jpg)
Explain Cheat Sheet part 2
![Page 16: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/16.jpg)
Explain Cheat Sheet part 3
![Page 17: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/17.jpg)
Profile and Session Status• Useful to find where MySQL Spend time• Show you the real work MySQL is doing , more
precise than EXPLAIN
![Page 18: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/18.jpg)
Example
![Page 19: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/19.jpg)
Other• Information_Schema.Columns
● Useful to search all columns in all databases● Sql injection will try to fetch data from there most of the
time.
• Limit● Try not to use big offset. Use a combination of where and
limit when possible
• Try not to use sub-query● The result of a sub-query is copied to a temp. table with
no index, most of the time it should be avoided.
![Page 20: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/20.jpg)
Other• See the optimized Query
● EXPLAIN EXTENDED● SHOW WARNINGS
• Query are not always the problem• Test your Doctrine query
![Page 21: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/21.jpg)
Reminder• Before Optimizing everything, test what your
doing• If you change a Column Type, make sure it will
never get over that value. (skin_id example)• (Paysite) If you change a column type for a
client, change it everywhere to stay constant• Use new Relic Slow Query Report.• Stay Up to date with new feature.
![Page 22: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/22.jpg)
Resource• MySQL Performance Blog
• MySQL Blog
• Percona Webinar
• Percona Youtube channel
• Pythian Varchar vs Text
• MySQL Group By Optimization
• MySQL Order By Optimization
• High Performance MySQL [book]
• Relational Database Index Design and the Optimizers [book]
![Page 23: Tool it Up! - Session #3 - MySQL](https://reader033.fdocuments.net/reader033/viewer/2022053012/554fb9a4b4c9053d018b467d/html5/thumbnails/23.jpg)
Thank You !
Comment , Suggestion and Critics are
Welcome :)