Top Banner
Query Tuning, System Tuning, and Little Known Tricks Using MySQL Workbench [email protected] @stoker Slideshare.net/davestokes Mountain Moodle Moot
59

Mountain Moodle Moot -- MySQL Server & Query Tuing

Sep 08, 2014

Download

Technology

Davie Stokes

Presentation from Mountain Moodle Moot, Helena MT
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Mountain Moodle Moot -- MySQL Server & Query Tuing

Query Tuning, System Tuning, and Little Known Tricks Using MySQL

Workbench [email protected]

@stokerSlideshare.net/davestokes

Mountain Moodle Moot

Page 2: Mountain Moodle Moot -- MySQL Server & Query Tuing

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

● Select What you need for speed

● Yes, YOU do need to do backups● And know how to restore from them

Page 3: Mountain Moodle Moot -- MySQL Server & Query Tuing

Server Tuning

Run latest release of MySQL Better performance Bug Fixes New features

Don't skimp on hardware Memory is better ROI than disk New SSDs are amazing FusionIO cards OMG

Page 4: Mountain Moodle Moot -- MySQL Server & Query Tuing

Configuration

Configuration should match hardware Remove Query Cache Nasty Mutex But what if you have 'repeatable data', i.e. sports score

Memcached In Application

Know your parameters Session, Global Needs reboot to change or on the fly

Page 5: Mountain Moodle Moot -- MySQL Server & Query Tuing

I_S, P_S, SYS

Information_Schema Performance_Schema Similar to V$ variables for Oracle 2-5% performance overhead

SYS Predefined for most common questions

You will not need them (now) but know that they are there when you need to eek out last %

Page 6: Mountain Moodle Moot -- MySQL Server & Query Tuing

Intro to Query Tuning

Reading from disk to memory is 100,000 slower than memory alone

'SELECT *' on a wide table with many col-umns when you need only two columns MUL-TIPLED by N thousands access a minute adds up

Read what you need for speed, no SELECT * Use joins to get desired sets

Page 7: Mountain Moodle Moot -- MySQL Server & Query Tuing
Page 8: Mountain Moodle Moot -- MySQL Server & Query Tuing

Our First Query

SELECT City.Name, Country.name, City.PopulationFROM CityJOIN Country ON

(Country.code = City.CountryCode)

Page 9: Mountain Moodle Moot -- MySQL Server & Query Tuing

Results

Page 10: Mountain Moodle Moot -- MySQL Server & Query Tuing

Our First Query

SELECT City.Name, Country.name, City.PopulationFROM CityJOIN Country ON

(Country.code = City.CountryCode)

Columns Selected (note Name & Name)

Page 11: Mountain Moodle Moot -- MySQL Server & Query Tuing

Our First Query

SELECT City.Name, Country.name, City.PopulationFROM CityJOIN Country ON

(Country.code = City.CountryCode)

Tables used

Page 12: Mountain Moodle Moot -- MySQL Server & Query Tuing

Venn Diagram

City is A, Country is B

Page 13: Mountain Moodle Moot -- MySQL Server & Query Tuing

Make query match diagram

SELECT a.Name, b.name, a.PopulationFROM City AS a JOIN Country AS b

ON (b.code = a.CountryCode)

Page 14: Mountain Moodle Moot -- MySQL Server & Query Tuing

Make query match diagram

SELECT a.Name, b.name, a.PopulationFROM City a JOIN Country b

ON (b.code = a.CountryCode)

Can also use AS to alias column names

Page 15: Mountain Moodle Moot -- MySQL Server & Query Tuing

Aliased Column Names

SELECT a.Name as 'City', b.name as 'Country' , a.Population as 'Pop.'FROM City aJOIN Country b ON (b.code = a.CountryCode)

Page 16: Mountain Moodle Moot -- MySQL Server & Query Tuing

Updated Results

Page 17: Mountain Moodle Moot -- MySQL Server & Query Tuing

Query Plan

Page 18: Mountain Moodle Moot -- MySQL Server & Query Tuing

EXPLAIN

EXPLAIN is pre-pended to a SQL statement and produces output like:

Page 19: Mountain Moodle Moot -- MySQL Server & Query Tuing

EXPLAIN

We see how many roads to be read:

Guess on rows needing to be read

4079 x 1 = 4079 rows to be read

Page 20: Mountain Moodle Moot -- MySQL Server & Query Tuing

EXPLAIN

In English – We read all the rows in the City table and find via an index to find the match in the Country table:

Page 21: Mountain Moodle Moot -- MySQL Server & Query Tuing

So what is an index?

http://en.wikipedia.org/wiki/Database_indexA database index is a data structure that improves the speed of data retrieval operations on a database table at the cost of additional writes and the use of more storage space to main-tain the extra copy of data. Indexes are used to quickly locate data without having to search every row in a database table every time a database table is accessed. Indexes can be cre-ated using one or more columns of a database table, provid-ing the basis for both rapid random lookups and efficient ac-cess of ordered records.

Page 22: Mountain Moodle Moot -- MySQL Server & Query Tuing

Uh, in English

Indexes let you go directly to the record you want Rolodex Card Catalog

Overhead Maintenance Sometimes you

DO have read all the table

Page 23: Mountain Moodle Moot -- MySQL Server & Query Tuing

B-Tree Index

Page 24: Mountain Moodle Moot -- MySQL Server & Query Tuing

A Peek at an Index

SHOW INDEX FROM <table>;

Page 25: Mountain Moodle Moot -- MySQL Server & Query Tuing

Statistics

Optimizer uses table statistics to generate cost data.

Out of data stats can make optimizer plan poorly

See 14.13.17.1 Con-figuring Persistent Optimizer Statistics Parameters in MySQL Manual

Run ANALYSE TA-BLE to update stats

Run ANALYSE TA-BLE after adding a new index

Store stat data at SHUTDOWN, Re-store at REBOOT

Page 26: Mountain Moodle Moot -- MySQL Server & Query Tuing

Lets look at the tables

EER Map of tables in World Database

Page 27: Mountain Moodle Moot -- MySQL Server & Query Tuing

Just City and Country

The two tables we have been using

Page 28: Mountain Moodle Moot -- MySQL Server & Query Tuing

Just City and Country

JOIN Country b ON (b.code = a.CountryCode)

Page 29: Mountain Moodle Moot -- MySQL Server & Query Tuing

Without Indexes

Plural of Moose List of definitions not alphabetized Duplicates

Page 30: Mountain Moodle Moot -- MySQL Server & Query Tuing

Compound Index

CREATE TABLE test ( id INT NOT NULL, last_name CHAR(30) NOT NULL, first_name CHAR(30) NOT NULL, PRIMARY KEY (id), INDEX name (last_name,first_name));

This index can use be used for searching on last_name + first_name or just last_name

Page 31: Mountain Moodle Moot -- MySQL Server & Query Tuing

Multi Column Index

CREATE INDEX CityStateZip ONCity (City, State, Zip);

This index can be used to search onCity + State + Zip, City + State, or City

This index can not be used to search onZip, State + Zip

Speeds up finding Zip when used in City + State

Page 32: Mountain Moodle Moot -- MySQL Server & Query Tuing

Why is it Important to Optimize?

MySQL, unlike other databases, has no way to lock a query plan. That means each time a query is submitted it goes through the opti-mizer.

Page 33: Mountain Moodle Moot -- MySQL Server & Query Tuing

Optimize Data

Ideally you want to allocate just enough stor-age for any possible record.

Have to use best judgment Human age range 0-999 Postal codes Address lines Phone numbers 01-123-123-1234 and extension ?! PROCEDURE ANALYSE will examine records, make suggestion

Not all Integers BIGINTs Not all text fields VARCHAR(255) Excess column widths are extra baggage

Page 34: Mountain Moodle Moot -- MySQL Server & Query Tuing

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 student id numbers?

Page 35: Mountain Moodle Moot -- MySQL Server & Query Tuing

Making it up in volume

Consider the worst case Data needs to be read off disk Transferred into memory Sent across network to application server Application server loads data into memory Application sends that data over network to end user

INT VS BIGINT – 4 extra bytes each times X number of records X number of users

Page 36: Mountain Moodle Moot -- MySQL Server & Query Tuing

Flexibility

Making changes to a schema is costly. It is a lot better for 5.6 Much better for 5.7

Must update replication masters and slaves (double check if done through replication)

Gender Example 2, 3, 17, or 58+

Page 37: Mountain Moodle Moot -- MySQL Server & Query Tuing

Replication

Changes made to master Replicated to slave server(s) Three threads

BINLOG dump thread Slave I/O thread Slave SQL thread

Page 38: Mountain Moodle Moot -- MySQL Server & Query Tuing

Read/Write Splits

Writes to Master Reads from slave(s)

Page 39: Mountain Moodle Moot -- MySQL Server & Query Tuing

Types of Replication

Statement Based – SQL Row Based – Binary changes after SQL exe-

cuted Mixed – Uses Statement Based but switches

when advantageous

Page 40: Mountain Moodle Moot -- MySQL Server & Query Tuing

Smarter Row Based Replication

MySQL 5.6 and later No need to copy unmodified parts of a row

Page 41: Mountain Moodle Moot -- MySQL Server & Query Tuing

Global Transaction ID

Previously had to match Master's log position to when starting replication

Each transaction unique Point slave at master and ask it to catch up

Page 42: Mountain Moodle Moot -- MySQL Server & Query Tuing
Page 43: Mountain Moodle Moot -- MySQL Server & Query Tuing

MySQL Utilities

Free, open sources Python scripts to auto-mate functions

mysqldbcompare Compare databases on two servers or the same server Compare definitions and data Generate a difference report Generate SQL transformation statements

mysqldbcopy Copy databases between servers Clone databases on the same server Supports rename

mysqldbexport Export metadata and/or data from one or more databases Formats: SQL, CSV, TAB, Grid, Vertical

mysqldbimport Import metadata and data from one or more files Reads all formats from mysqldbexport

mysqldiff Compare object definitions Generate a difference report

Page 44: Mountain Moodle Moot -- MySQL Server & Query Tuing

These utilities are those designed to perform general operations such as

reporting and searching.

mysqldiskusage Show disk usage for databases Generate reports in SQL, CSV, TAB, Grid, Vertical

mysqlfrm Reads .frm files, optionally in byte-by-byte diagnostic mode Generates CREATE statements from table definition data

mysqlindexcheck Read indexes for one or more tables Check for redundant and duplicate indexes Generate reports in SQL, CSV, TAB, Grid, Vertical

mysqlmetagrep Search metadata Regexp, database search Generate SQL statement for search query

mysqlprocgrep Search process information Generate SQL statement for search Kill processes that match query

mysqluserclone Clone a user account, to the same or different server Show user grants

mysqluc Command line client for running MySQL Utilities Allows a persistent connection to a MySQL Server Tab completion for utility names and options Allows calling the commands with shorter names, such as using "serverinfo" instead of mysqlserverinfo

Page 45: Mountain Moodle Moot -- MySQL Server & Query Tuing

These utilities are those designed to support replication and high availability operations

for MySQL servers.

mysqlfailover Provides automatic failover on a replication topology Uses Global Transaction Identifiers (GTID, MySQL Server 5.6.5+)

mysqlreplicate Setup replication Start from beginning, current, specific binlog, pos

mysqlrplms Provides round-robin multi-source replication (a slave server continually cycles through multiple masters in order to store

a consolidated data set) Uses Global Transaction Identifiers (GTID, MySQL Server 5.6.9+)

mysqlrpladmin Administers the replication topology Allows recovery of the master Commands include elect, failover, gtid, health, start, stop, and switchover

mysqlrplcheck Check replication configuration Tests binary logging on master

mysqlrplshow Show slaves attached to master Can search recursively Show the replication topology as a graph or list

mysqlrplsync Check data consistency between servers in a replicated setup Uses Global Transaction Identifiers (GTID) Requires MySQL Server 5.6.14 and higher

Page 46: Mountain Moodle Moot -- MySQL Server & Query Tuing

MySQL Fabric

Also written in Python and free Sharding High

Availability

Page 47: Mountain Moodle Moot -- MySQL Server & Query Tuing

Exercise

Mysqldbcopy –source=root:pass@localhost --destination=root:pass@localhost world:xworld

Use MySQL Utility mysqldbcopy to make a copy of world database

Page 48: Mountain Moodle Moot -- MySQL Server & Query Tuing

Exercise

Using world database, run query from earlier to see query plan.

SELECT a.Name as 'City', b.name as 'Country' , a.Population as 'Pop.'FROM City aJOIN Country b ON (b.code = a.CountryCode);

Page 49: Mountain Moodle Moot -- MySQL Server & Query Tuing

Remove Indexes

Use worldx schema which redefines the World database without indexes

Page 50: Mountain Moodle Moot -- MySQL Server & Query Tuing

Exercise

Using worldx database again, run query from earlier to see query plan to see cost.

SELECT a.Name as 'City', b.name as 'Country' , a.Population as 'Pop.'FROM City aJOIN Country b ON (b.code = a.CountryCode)

Page 51: Mountain Moodle Moot -- MySQL Server & Query Tuing

Back to World

How does a 'qualifier' effect query plan?

SELECT a.Name as 'City', b.name as 'Country' , a.Population as 'Pop.'FROM City aJOIN Country b ON (b.code = a.CountryCode)WHERE a.Population > 500000;

Page 52: Mountain Moodle Moot -- MySQL Server & Query Tuing

Back to World Again

How does a second 'qualifier' effect query plan?

SELECT a.Name as 'City', b.name as 'Country' , a.Population as 'Pop.'FROM City aJOIN Country b ON (b.code = a.CountryCode)WHERE a.Population > 500000ORDER By b.name;

Page 53: Mountain Moodle Moot -- MySQL Server & Query Tuing

Run those last two queries on worldx

Did indexes help/hurt? Are the qualifiers run before/after join?

Page 54: Mountain Moodle Moot -- MySQL Server & Query Tuing

Moodle 2.7

A quick look at the schema

Page 55: Mountain Moodle Moot -- MySQL Server & Query Tuing

More Info on Tuning

High Performance MySQL - 3rd Edition Schwartz, Zaitsev, & Tkachenko

Effective MySql Optimizing Sql Statements Bradford

Page 56: Mountain Moodle Moot -- MySQL Server & Query Tuing

MySQL Marinate

Free on-line Virtual class Boston MySQL Users Group One Chapter a week Homework Checked by

Mozilla's senior DBA

Page 57: Mountain Moodle Moot -- MySQL Server & Query Tuing

MySQL Training from Oracle

Introduction to MySQL MySQL Quickstart Fundamentals MySQL for Beginners MySQL for Database Administrators MySQL for Developers MySQL Performance Tuning MySQL High Availability MySQL Cluster MySQL Developer Techniques MySQL and PHP - Developing Dynamic Web Applica-

tions MySQL Advanced Stored Procedures

Page 58: Mountain Moodle Moot -- MySQL Server & Query Tuing

MySQL Central @ Oracle Open World

Keynotes from Booking.com, Twitter & Dropbox 50+ session, tutorials, BOFs and reception

Page 59: Mountain Moodle Moot -- MySQL Server & Query Tuing

Q/A

[email protected] @stoker

MySQL Central at Oracle Open WorldSan Francisco Septermber 29th to October 3rd