SlideShare uma empresa Scribd logo
1 de 46
Baixar para ler offline
MySQL Query & Index Tuning


                        Keith Murphy

                  MySQL DBA - iContact, Inc
  Editor of MySQL Magazine @ http://www.paragon-cs.com/mag/

                    kmurphy@icontact.com

                blog: http://blog.paragon-cs.com
Rules




    Questions OK after any slide
●




The views and opinions expressed here are mine and may not
 reflect the views and opinions of iContact, Inc.
                           ●
What We'll Cover

    MySQL Server Overview
●

    Slow Query Logging
●

    The EXPLAIN statement
●

    Things to Avoid in Queries
●

    Indexing Strategies
●

    The MySQL Optimizer
●

    Schema Guidelines
●

    Query Cache
●

    Benchmarking
●
MySQL Query And Index Tuning
Slow Query Log
    logs queries that take more than a specified
●

    amount of time (defaults to ten seconds)
    resulting log can be “cat-ed/tail-ed” or you
●

    can use the mysqldumpslow command:
         mysqldumpslow -s t
     –
         mysqldumpslow -s -c
     –


    sample output from log file:
    # Time: 070911 2:06:37
    # User@Host: username @ hostname [ip_address]
    # Query_time: 20 Lock_time: 0 Rows_sent: 3979 Rows_examined: 3979
    use database;
    SELECT field_1 FROM table WHERE field_2 = '360973';
EXPLAIN command
    query tuners friend!!
●

    shows the execution path chosen by the MySQL
●

    optimizer for a specific SELECT statement
explain SELECT field_1 FROM table_1 WHERE field_2 = '360973'G
*************************** 1. row ***************************
         id: 1
 select_type: SIMPLE
      table: table_1
       type: ref
possible_keys: field_2
        key: field_2
    key_len: 4
        ref: const
       rows: 7174
      Extra: Using where
EXPLAIN Output Syntax
    id
●

    select_type
●

    table
●

    type
●

    possible_keys
●

    keys
●

    key_len
●

    ref
●

    rows
●

    extras
●
Explain Output

    id the sequential number of the table(s)
●

    select_type the type of SELECT
●

    table the name of the table or alias
●

    type the type of join for the query
●

    possible_keys which indexes MySQL could use
●

    keys which indexes MySQL will use
●

    key_len the length of keys used
●

    ref any columns used with the key to retrieve
●

    results
    rows estimated number of rows returned
●

    extra any additional information
●
Select Types – Part I
SIMPLE
          basic SELECT without UNION or sub-queries

          Example:

          SELECT * FROM customers WHERE last_name = “smith”

PRIMARY
       Outermost or highest level SELECT

DERIVED
          If a query involves a derived table (including a view) it is assigned a DERIVED select type.

UNION
          Second or later SELECT statement in a UNION

          Example:

          SELECT cm.customer_id FROM CUSTOMER_MASTER cm
          WHERE cm.date_joined_program > “2002-01-15” and cm.home_airport_code = 'SEA'
          UNION
          SELECT cm1.csutomer_id FROM CUSTOMER_MASTER cm1 WHERE cm1.sex = 'M';

DEPENDENT UNION
       Second or later SELECT statement in a UNION - dependent on outer query
Select Types – Part II
UNION RESULT
        When the optimizer needs to create a temporary intermediate table to hold the results of a UNION

SUBQUERY
      When a query includes a subquery the first SELECT in the subquery is identified as SUBQUERY

DEPENDENT SUBQUERY
       If a subquery relies on information from an outer query the first SELECT in the subquery is labeled
           DEPENDENT SUBQUERY.

         Example:

         SELECT cm.last_name, cm.first_name
         FROM customer_master cm
         WHERE cm.customer_id IN
         (
             SELECT ca.customer_id
             FROM customer_address ca
             WHERE ca.country = “Canada”
         );

UNCACHEABLE SUBQUERY
     A sub-query result which can’t be cached and is evaluated for each row of the outer query
Join Types – Part I
const – The query will have type of const (constant value) if the optimizer is able to fully use a unique index
or primary key to satisfy your search. Because there is only one row, values from the column in this row can
be regarded as constants by the rest of the optimizer. const tables are very fast because they are read only
once. A system join is just a const join type reading a system table.

Example:

SELECT * FROM tbl_name WHERE primary_key=1;
SELECT * FROM tbl_name WHERE primary_key_part1=1 AND primary_key_part2=2;

eq_ref - one row is read from this table for each combination of rows from the previous tables. Other than the
system and const types, this is the best possible join type. It is used when all parts of an index are used by
the join and the index is a PRIMARY KEY or UNIQUE index. eq_ref can be used for indexed columns that are
compared using the = operator. The comparison value can be a constant or an expression that uses columns
from tables that are read before this table. In the following examples, MySQL can use an eq_ref join to
process ref_table:

SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.COLUMN;

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.COLUMN AND ref_table.key_column_part2=1;
Join Types – Part II
 ref / ref_or_null – these types of joins use a nonunique index to find anywhere from one to many rows from
a table.

Example:

SELECT * FROM customer_address ca where ca.postal_code=”27713”;

If the join type is ref_or_null than it means that query is also looking for null values from an index column.


index_merge – in this type of join multiple indexes are used to locate the data. With the index_merge the
key column in the output row contains a list of indexes used.

unique_subquery - unique_subquery is just an index lookup function that replaces the subquery completely
for better efficiency. This type replaces ref for some IN subqueries of the following form:

value IN (SELECT primary_key FROM single_table WHERE some_expr)
Join Types – Part III

index_subquery – this kind of query takes advantage of an index to speed results on a subquery:

Example:

SELECT cm.last_name, cm.first_name FROM customer_master cm WHERE cm.customer_id IN
(SELECT ca.customer_id FROM customer_address ca WHERE ca.postal_code = “TVC15-3CPU”);

range - only rows that are in a given range are retrieved, using an index to select the rows. The key column in
the output row indicates which index is used. The key_len contains the longest key part that was used. The
ref column is NULL for this type. range can be used when a key column is compared to a constant using any
of the =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, or IN operators:

SELECT * FROM tbl_name WHERE key_column BETWEEN 10 AND 20;

index - this join type is the same as ALL, except that the index tree is scanned (instead of the entire table).
This usually is faster than ALL because the index file usually is smaller than the data file. MySQL can use this
join type when the query uses only columns that are part of a single index.

ALL – the optimizer is forced to perform a full-table scan and read all rows in a table to find your results.

Example:

SELECT last_name from customer_table where last_name LIKE “%john%”;
What to Avoid in Queries
    correlated subquery
●

    Version specific “gotchas”:
●

        Prior to MySQL 5.0 queries only use one index
    –
         Prior to MySQL 5.0 in the case of OR conditions
    –
        in a WHERE clause – MySQL uses a full-table
        scan
Indexing Strategies
    you should minimally use one index per table
●

    however, don't index every column!!
●

        indexes are more costly to update
    –
    always try and use indexes with high
●

    selectivity
Selectivity
Selectivity of an index - the ratio of the number of distinct
 values in the indexed column(s) to the number of records.
 The ideal selectivity is one. Such a selectivity can be
 reached only by unique indexes on NOT NULL columns
 (UNIQUE or PRIMARY KEY columns).
Example:

Query A select count (*) from users;
Query B select count(distinct username) from users;
B/A = selectivity

 A higher selectivity (closer to one) is more desirable. If
  selectivity is too low the query optimizer won't use it.
Full Table Scans – Part I
Full table scans are where the database will read
 the entire table without an index.

    almost always not the desired behavior
●




How do we determine if MySQL is going to
 perform a full table scan?

    EXPLAIN
●
Full Table Scans – Part II

Reasons why full table scans are performed

    no WHERE clause
●

    no index on any field in WHERE clause
●

    poor selectivity on an indexed field
●

    too many records meet WHERE conditions
●

    MySQL version less than 5.0 and using OR in a
●

    WHERE clause
    using SELECT * FROM
●
Covering Indexes – Part I

A covering index is an indexing strategy where the
  index encompasses every field returned by a SELECT
  statement.

Why is this a good thing? Because it is faster to read
 everything from the index than use the index to look
 up information in rows.
Example:

You have an index (a,b) on table_1

SELECT b from table_1 where a=5
Covering Indexes – Part II
Where do covering indexes benefit?

    When you have large tables
●

    When you have long rows (BLOBS for example)
●

    When extra columns do not increase key length
●

    significantly
    When you have a large join with a number of
●

    secondary table lookups
    When a lot of rows match the same key value
●

    MyISAM tables benefit more than Innodb because
●

    MyISAM does not cache rows
Duplicate Indexes – Part I

When tables have multiple indexes defined on
 the same columns it has duplicate indexes.

Example:

PRIMARY KEY (id)
UNIQUE KEY id(id)
KEY id2(id)
Duplicate Indexes – Part II
Notes:

Duplicate indexes apply to indexes of the same
 type. It may make sense to have indexes of
 different types created on the same column.


Example: BTREE index and a FULLTEXT index

Order of columns is important -
index (a,b) is not a duplicate of index (b,a)
Redundant Indexes – Part I

Redundant indexes are prefixes of other indexes

Example:

KEY (A)
KEY (A,B)
KEY (A(10))
Redundant indexes are almost never helpful.
 Queries that take advantage of redundant
 indexes will also be able to make use of the
 longer indexes.
Redundant Indexes – Part II
When are redundant indexes useful?

If an index is just too long
Example: if A is an int and B is a varchar(255)
 which holds a great deal of long data than
 using KEY(A) might be significantly faster
 than using KEY (A,B)

There are no tools currently bundled with the
 MySQL distribution to check schemas for
 redundant and duplicate indexes.
Complete Example
create database mysql_query optimization;

use mysql_query_optimization;

create table students (
student_id integer not null auto_increment primary key,
 name char(40));

create table tests (
test_id integer not null auto_increment primary key,
 total integer, name char(40));

create table grades (
test_id integer,
 student_id integer,
 score decimal(5,2));
Inserting Data
echo quot;insert into tests values (1,100, 'test 1');quot; > insert_tests.sql

echo quot;insert into tests values (2,100, 'test 2');quot; >> insert_tests.sql

echo quot;insert into tests values (3,300, 'test 3');quot; >> insert_tests.sql

echo quot;-- insert studentsquot; > insert_students.sql

for i in `seq 1 50000`
do
echo quot;insert into students values ($i, '$i diff');quot; >> insert_students.sql
done

mysql -u root -p mysql_query_optimization < insert_tests.sql

mysql -u root -p mysql_query_optimization < insert_students.sql
Inserting Grading Data


insert into grades select 1, student_id, rand()*100 from students
   order by rand() limit 40000 ;
insert into grades select 2, student_id, rand()*100 from students
   order by rand() limit 40000 ;
insert into grades select 3, student_id, rand()*300 from students
   order by rand() limit 40000 ;
Testing w/o Indexes – Part I
-- what was the average score on test 1?

mysql> select avg(score) from grades where test_id=1;
+---------------+
| avg(score) |
+---------------+
 | 49.887198 |
+---------------+
1 row in set (0.06 sec)

-- what students didn't take test 1?

select count(*) from students s left join grades g on (s.student_id =
  g.student_id and test_id=2) where g.student_id is null ;

*** canceled query after 4200 seconds
Testing w/o Indexes – Part I
              EXPLAINed
mysql> explain select count(*) from students s left join grades g on (s.student_id =
    g.student_id and test_id=2) where g.student_id is nullG
*************************** 1. row ***************************
         id: 1
 select_type: SIMPLE
      table: s
       type: index
possible_keys: NULL
        key: PRIMARY
    key_len: 4
        ref: NULL
       rows: 50000
      Extra: Using index
*************************** 2. row ***************************
         id: 1
 select_type: SIMPLE
      table: g
       type: ALL
possible_keys: NULL
        key: NULL
    key_len: NULL
        ref: NULL
       rows: 120000
      Extra: Using where
Testing w/o Indexes – Part II
-- how many students took zero tests?

select count(*) from students s left join grades g on (s.student_id =
  g.student_id) where g.student_id is null;

***canceled query after 5700 seconds
Testing w/o Indexes – Part II
             EXPLAINed
mysql> explain select count(*) from students s left join grades g on (s.student_id =
    g.student_id) where g.student_id is nullG
*************************** 1. row ***************************
         id: 1
 select_type: SIMPLE
      table: s
       type: index
possible_keys: NULL
        key: PRIMARY
    key_len: 4
        ref: NULL
       rows: 50000
      Extra: Using index
*************************** 2. row ***************************
         id: 1
 select_type: SIMPLE
      table: g
       type: ALL
possible_keys: NULL
        key: NULL
    key_len: NULL
        ref: NULL
       rows: 120000
      Extra: Using where
Changes!!!
-mysql> alter table grades add index(test_id);
Query OK, 120000 rows affected (0.52 sec)
Records: 120000 Duplicates: 0 Warnings: 0

mysql> alter table grades add index(student_id);
Query OK, 120000 rows affected (0.88 sec)
Records: 120000 Duplicates: 0 Warnings: 0
Testing with Indexes – Part I
-- what students didn't take test 1?

mysql> select count(*) from students s left join grades g on
   (s.student_id = g.student_id and test_id=2) where g.student_id is
   null ;
+----------+
| count(*) |
+----------+
| 10000 |
+----------+
1 row in set (1.40 sec)
Testing with Indexes – Part I
               EXPLAINed
mysql> explain select count(*) from students s left join grades g on (s.student_id = g.student_id and
    test_id=2) where g.student_id is nullG
*************************** 1. row ***************************
         id: 1
 select_type: SIMPLE
      table: s
       type: index
possible_keys: NULL
        key: PRIMARY
    key_len: 4
        ref: NULL
       rows: 50000
      Extra: Using index
*************************** 2. row ***************************
         id: 1
 select_type: SIMPLE
      table: g
       type: ref
possible_keys: test_id,student_id
        key: student_id
    key_len: 5
        ref: mysql_query_optimization.s.student_id
       rows: 2
      Extra: Using where
Testing with Indexes – Part II
-- how many students took zero tests?

mysql> select count(*) from students s left join grades g on
   (s.student_id = g.student_id) where g.student_id is null;
+----------+
| count(*) |
+----------+
|    443 |
+----------+
1 row in set (1.19 sec)
Testing with Indexes – Part II
           EXPLAINed
mysql> explain select count(*) from students s left join grades g on (s.student_id =
    g.student_id) where g.student_id is nullG
*************************** 1. row ***************************
         id: 1
 select_type: SIMPLE
      table: s
       type: index
possible_keys: NULL
        key: PRIMARY
    key_len: 4
        ref: NULL
       rows: 50000
      Extra: Using index
*************************** 2. row ***************************
         id: 1
 select_type: SIMPLE
      table: g
       type: ref
possible_keys: student_id
        key: student_id
    key_len: 5
        ref: mysql_query_optimization.s.student_id
       rows: 2
      Extra: Using where; Using index
MySQL Optimizer – Part I
There are two types of optimizers:
● Cost-based

● Rule-based



Cost-based optimizers estimates the execution times of
 performing a query various ways. It tries to choose the
 lowest execution time.
Rule-based optimizers build the best query based on a set
 of rules. Rule-based optimizers are quicker than cost-
 based optimizers but cost-based optimizers usually offer
 better performance. There are too many exceptions to
 the rules that the calculation overhead of cost-based
 optimizations is worthwhile.
MySQL Optimizer – Part II
Both cost-based and rule-based optimizers will
 return correct results.




The cost-based optimizer uses index selectivity to
 determine the best execution for joins.
Wrong Choices
The query optimizer will make wrong choices
 from time to time

    EXPLAIN is your friend!!
●
Schema Guidelines
Always use the smallest data type possible? Do you
  REALLY need that BIGINT?

    the smaller your data type the more index and data
●

    records can fit into a single block of memory
    normalize first and denormalize later
●
Query Cache
To use the QC effectively you must understand your
 applications read/write ratio.

    QC design is a compromise between CPU usage and
●

    read performance
    Bigger QC does not automatically mean better
●

    performance – even for heavy read applications
    Any modification of any table referenced in a SELECT
●


    will invalidate any QC entry that uses that table
Benchmarking – Part I

When performing benchmarks:

    change one item at a time
●

     – configuration (my.cnf) variable
     – addition of an index
     – modification to a schema table
     – change to a SQL script
    re-run the benchmark after making a change
●

    make comparisons apple–apple not apple-orange
●
Benchmarking – Part II
Isolate the benchmarking environment

        Disable non-essential services
●

         – Network traffic analyzers
         – Non essential daemons
         – MySQL query cache
        Use a testing machine if at all possible
●




    Save the benchmark results

         Save all benchmark files
    ●

          – Result files of test runs
          – Configuration files at time of runs
          – OS/hardware configuration changes
Benchmarking – Part III
Why a benchmarking framework?
● automates the most tedious part of testing

● standardizes benchmark results

● can customize to your needs

● framework can be analyzed by outside parties

      determine if framework provides consistent results
  –
      if framework is biased
  –
Benchmarking Tools

Sysbench
Supersmack
AB (ApacheBench)

Generic Frameworks:

Junit/Ant (Java)
MyBench (Perl)
thewench (PHP)
QPP (Query Processing Programs)
Resources
Books:

SQL Tuning by Dan Tow
MySQL Database Design and Tuning by Robert Schneider

Websites:

http://www.mysqlperformanceblog.com
http://www.planetmysql.org

Mailing Lists:

http://lists.mysql.com (general list)

Mais conteúdo relacionado

Mais procurados

MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsOSSCube
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZENorvald Ryeng
 
MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveSveta Smirnova
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZENorvald Ryeng
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorialGiuseppe Maxia
 
MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바NeoClova
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performancejkeriaki
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performanceoysteing
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain ExplainedJeremy Coates
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index CookbookMYXPLAIN
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksEDB
 
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricksQuery Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricksJaime Crespo
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer OverviewOlav Sandstå
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performanceMariaDB plc
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 MinutesSveta Smirnova
 
Open Source 101 2022 - MySQL Indexes and Histograms
Open Source 101 2022 - MySQL Indexes and HistogramsOpen Source 101 2022 - MySQL Indexes and Histograms
Open Source 101 2022 - MySQL Indexes and HistogramsFrederic Descamps
 

Mais procurados (20)

MySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 TipsMySQL Performance Tuning: Top 10 Tips
MySQL Performance Tuning: Top 10 Tips
 
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZEMySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
MySQL 8.0.18 latest updates: Hash join and EXPLAIN ANALYZE
 
MySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to HaveMySQL Database Monitoring: Must, Good and Nice to Have
MySQL Database Monitoring: Must, Good and Nice to Have
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZE
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
How to Design Indexes, Really
How to Design Indexes, ReallyHow to Design Indexes, Really
How to Design Indexes, Really
 
MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바MySQL Advanced Administrator 2021 - 네오클로바
MySQL Advanced Administrator 2021 - 네오클로바
 
Sql query patterns, optimized
Sql query patterns, optimizedSql query patterns, optimized
Sql query patterns, optimized
 
MySQL: Indexing for Better Performance
MySQL: Indexing for Better PerformanceMySQL: Indexing for Better Performance
MySQL: Indexing for Better Performance
 
Using Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query PerformanceUsing Optimizer Hints to Improve MySQL Query Performance
Using Optimizer Hints to Improve MySQL Query Performance
 
Get to know PostgreSQL!
Get to know PostgreSQL!Get to know PostgreSQL!
Get to know PostgreSQL!
 
Mysql Explain Explained
Mysql Explain ExplainedMysql Explain Explained
Mysql Explain Explained
 
MySQL Index Cookbook
MySQL Index CookbookMySQL Index Cookbook
MySQL Index Cookbook
 
MySQL Tuning
MySQL TuningMySQL Tuning
MySQL Tuning
 
How the Postgres Query Optimizer Works
How the Postgres Query Optimizer WorksHow the Postgres Query Optimizer Works
How the Postgres Query Optimizer Works
 
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricksQuery Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
Query Optimization with MySQL 5.7 and MariaDB 10: Even newer tricks
 
MySQL Optimizer Overview
MySQL Optimizer OverviewMySQL Optimizer Overview
MySQL Optimizer Overview
 
How to use histograms to get better performance
How to use histograms to get better performanceHow to use histograms to get better performance
How to use histograms to get better performance
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
 
Open Source 101 2022 - MySQL Indexes and Histograms
Open Source 101 2022 - MySQL Indexes and HistogramsOpen Source 101 2022 - MySQL Indexes and Histograms
Open Source 101 2022 - MySQL Indexes and Histograms
 

Destaque

MySQL Performance Tips & Best Practices
MySQL Performance Tips & Best PracticesMySQL Performance Tips & Best Practices
MySQL Performance Tips & Best PracticesIsaac Mosquera
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL QueriesAchievers Tech
 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexingYoshinori Matsunobu
 
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15oysteing
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng郁萍 王
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinaroysteing
 
My sql explain cheat sheet
My sql explain cheat sheetMy sql explain cheat sheet
My sql explain cheat sheetAchievers Tech
 
How mysql choose the execution plan
How mysql choose the execution planHow mysql choose the execution plan
How mysql choose the execution plan辛鹤 李
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningMYXPLAIN
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)Karthik .P.R
 
Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Dave Stokes
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performanceoysteing
 
What Your Database Query is Really Doing
What Your Database Query is Really DoingWhat Your Database Query is Really Doing
What Your Database Query is Really DoingDave Stokes
 
How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016oysteing
 
MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?Norvald Ryeng
 
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)Aurimas Mikalauskas
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQLDag H. Wanvik
 
MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions oysteing
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server DefaultsMorgan Tocker
 
What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...Sveta Smirnova
 

Destaque (20)

MySQL Performance Tips & Best Practices
MySQL Performance Tips & Best PracticesMySQL Performance Tips & Best Practices
MySQL Performance Tips & Best Practices
 
Optimizing MySQL Queries
Optimizing MySQL QueriesOptimizing MySQL Queries
Optimizing MySQL Queries
 
More mastering the art of indexing
More mastering the art of indexingMore mastering the art of indexing
More mastering the art of indexing
 
How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15How to analyze and tune sql queries for better performance percona15
How to analyze and tune sql queries for better performance percona15
 
MySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. RyengMySQL EXPLAIN Explained-Norvald H. Ryeng
MySQL EXPLAIN Explained-Norvald H. Ryeng
 
How to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinarHow to analyze and tune sql queries for better performance webinar
How to analyze and tune sql queries for better performance webinar
 
My sql explain cheat sheet
My sql explain cheat sheetMy sql explain cheat sheet
My sql explain cheat sheet
 
How mysql choose the execution plan
How mysql choose the execution planHow mysql choose the execution plan
How mysql choose the execution plan
 
Advanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema TuningAdvanced MySQL Query and Schema Tuning
Advanced MySQL Query and Schema Tuning
 
MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)MySQL Query Optimization (Basics)
MySQL Query Optimization (Basics)
 
Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016Polyglot Database - Linuxcon North America 2016
Polyglot Database - Linuxcon North America 2016
 
How to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better PerformanceHow to Analyze and Tune MySQL Queries for Better Performance
How to Analyze and Tune MySQL Queries for Better Performance
 
What Your Database Query is Really Doing
What Your Database Query is Really DoingWhat Your Database Query is Really Doing
What Your Database Query is Really Doing
 
How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016How to analyze and tune sql queries for better performance vts2016
How to analyze and tune sql queries for better performance vts2016
 
MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?MySQL 8.0: GIS — Are you ready?
MySQL 8.0: GIS — Are you ready?
 
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
 
SQL window functions for MySQL
SQL window functions for MySQLSQL window functions for MySQL
SQL window functions for MySQL
 
MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions MySQL 8.0: Common Table Expressions
MySQL 8.0: Common Table Expressions
 
MySQL Server Defaults
MySQL Server DefaultsMySQL Server Defaults
MySQL Server Defaults
 
What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...What you wanted to know about MySQL, but could not find using inernal instrum...
What you wanted to know about MySQL, but could not find using inernal instrum...
 

Semelhante a MySQL Query And Index Tuning

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007paulguerin
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)Hemant Kumar Singh
 
6.3 my sql queryoptimization_part2
6.3 my sql queryoptimization_part26.3 my sql queryoptimization_part2
6.3 my sql queryoptimization_part2Trần Thanh
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql queryvuhaininh88
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxArjayBalberan1
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-dbuncleRhyme
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standardsAlessandro Baratella
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance OptimizationMindfire Solutions
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsNirav Shah
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sqlN.Jagadish Kumar
 
Amazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Web Services
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql executionxKinAnx
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 

Semelhante a MySQL Query And Index Tuning (20)

Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007Myth busters - performance tuning 101 2007
Myth busters - performance tuning 101 2007
 
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)MySQL Indexing : Improving Query Performance Using Index (Covering Index)
MySQL Indexing : Improving Query Performance Using Index (Covering Index)
 
6.3 my sql queryoptimization_part2
6.3 my sql queryoptimization_part26.3 my sql queryoptimization_part2
6.3 my sql queryoptimization_part2
 
Tunning sql query
Tunning sql queryTunning sql query
Tunning sql query
 
Optimizing MySQL queries
Optimizing MySQL queriesOptimizing MySQL queries
Optimizing MySQL queries
 
Mysql Optimization
Mysql OptimizationMysql Optimization
Mysql Optimization
 
ADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptxADV Powepoint 3 Lec.pptx
ADV Powepoint 3 Lec.pptx
 
02 database oprimization - improving sql performance - ent-db
02  database oprimization - improving sql performance - ent-db02  database oprimization - improving sql performance - ent-db
02 database oprimization - improving sql performance - ent-db
 
Database development coding standards
Database development coding standardsDatabase development coding standards
Database development coding standards
 
MySQL Performance Optimization
MySQL Performance OptimizationMySQL Performance Optimization
MySQL Performance Optimization
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Top 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tipsTop 10 Oracle SQL tuning tips
Top 10 Oracle SQL tuning tips
 
Lab1 select statement
Lab1 select statementLab1 select statement
Lab1 select statement
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
Sql
SqlSql
Sql
 
Beginers guide for oracle sql
Beginers guide for oracle sqlBeginers guide for oracle sql
Beginers guide for oracle sql
 
Amazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and OptimizationAmazon Redshift: Performance Tuning and Optimization
Amazon Redshift: Performance Tuning and Optimization
 
Presentation top tips for getting optimal sql execution
Presentation    top tips for getting optimal sql executionPresentation    top tips for getting optimal sql execution
Presentation top tips for getting optimal sql execution
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
MySQL performance tuning
MySQL performance tuningMySQL performance tuning
MySQL performance tuning
 

Último

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxYounusS2
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UbiTrack UK
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1DianaGray10
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
RAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIRAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIUdaiappa Ramachandran
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfAnna Loughnan Colquhoun
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.francesco barbera
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServiceRenan Moreira de Oliveira
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 

Último (20)

IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptx
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
UWB Technology for Enhanced Indoor and Outdoor Positioning in Physiological M...
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1Secure your environment with UiPath and CyberArk technologies - Session 1
Secure your environment with UiPath and CyberArk technologies - Session 1
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
RAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AIRAG Patterns and Vector Search in Generative AI
RAG Patterns and Vector Search in Generative AI
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
Spring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdfSpring24-Release Overview - Wellingtion User Group-1.pdf
Spring24-Release Overview - Wellingtion User Group-1.pdf
 
Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.Digital magic. A small project for controlling smart light bulbs.
Digital magic. A small project for controlling smart light bulbs.
 
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer ServicePicPay - GenAI Finance Assistant - ChatGPT for Customer Service
PicPay - GenAI Finance Assistant - ChatGPT for Customer Service
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 

MySQL Query And Index Tuning

  • 1. MySQL Query & Index Tuning Keith Murphy MySQL DBA - iContact, Inc Editor of MySQL Magazine @ http://www.paragon-cs.com/mag/ kmurphy@icontact.com blog: http://blog.paragon-cs.com
  • 2. Rules Questions OK after any slide ● The views and opinions expressed here are mine and may not reflect the views and opinions of iContact, Inc. ●
  • 3. What We'll Cover MySQL Server Overview ● Slow Query Logging ● The EXPLAIN statement ● Things to Avoid in Queries ● Indexing Strategies ● The MySQL Optimizer ● Schema Guidelines ● Query Cache ● Benchmarking ●
  • 5. Slow Query Log logs queries that take more than a specified ● amount of time (defaults to ten seconds) resulting log can be “cat-ed/tail-ed” or you ● can use the mysqldumpslow command: mysqldumpslow -s t – mysqldumpslow -s -c – sample output from log file: # Time: 070911 2:06:37 # User@Host: username @ hostname [ip_address] # Query_time: 20 Lock_time: 0 Rows_sent: 3979 Rows_examined: 3979 use database; SELECT field_1 FROM table WHERE field_2 = '360973';
  • 6. EXPLAIN command query tuners friend!! ● shows the execution path chosen by the MySQL ● optimizer for a specific SELECT statement explain SELECT field_1 FROM table_1 WHERE field_2 = '360973'G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: table_1 type: ref possible_keys: field_2 key: field_2 key_len: 4 ref: const rows: 7174 Extra: Using where
  • 7. EXPLAIN Output Syntax id ● select_type ● table ● type ● possible_keys ● keys ● key_len ● ref ● rows ● extras ●
  • 8. Explain Output id the sequential number of the table(s) ● select_type the type of SELECT ● table the name of the table or alias ● type the type of join for the query ● possible_keys which indexes MySQL could use ● keys which indexes MySQL will use ● key_len the length of keys used ● ref any columns used with the key to retrieve ● results rows estimated number of rows returned ● extra any additional information ●
  • 9. Select Types – Part I SIMPLE basic SELECT without UNION or sub-queries Example: SELECT * FROM customers WHERE last_name = “smith” PRIMARY Outermost or highest level SELECT DERIVED If a query involves a derived table (including a view) it is assigned a DERIVED select type. UNION Second or later SELECT statement in a UNION Example: SELECT cm.customer_id FROM CUSTOMER_MASTER cm WHERE cm.date_joined_program > “2002-01-15” and cm.home_airport_code = 'SEA' UNION SELECT cm1.csutomer_id FROM CUSTOMER_MASTER cm1 WHERE cm1.sex = 'M'; DEPENDENT UNION Second or later SELECT statement in a UNION - dependent on outer query
  • 10. Select Types – Part II UNION RESULT When the optimizer needs to create a temporary intermediate table to hold the results of a UNION SUBQUERY When a query includes a subquery the first SELECT in the subquery is identified as SUBQUERY DEPENDENT SUBQUERY If a subquery relies on information from an outer query the first SELECT in the subquery is labeled DEPENDENT SUBQUERY. Example: SELECT cm.last_name, cm.first_name FROM customer_master cm WHERE cm.customer_id IN ( SELECT ca.customer_id FROM customer_address ca WHERE ca.country = “Canada” ); UNCACHEABLE SUBQUERY A sub-query result which can’t be cached and is evaluated for each row of the outer query
  • 11. Join Types – Part I const – The query will have type of const (constant value) if the optimizer is able to fully use a unique index or primary key to satisfy your search. Because there is only one row, values from the column in this row can be regarded as constants by the rest of the optimizer. const tables are very fast because they are read only once. A system join is just a const join type reading a system table. Example: SELECT * FROM tbl_name WHERE primary_key=1; SELECT * FROM tbl_name WHERE primary_key_part1=1 AND primary_key_part2=2; eq_ref - one row is read from this table for each combination of rows from the previous tables. Other than the system and const types, this is the best possible join type. It is used when all parts of an index are used by the join and the index is a PRIMARY KEY or UNIQUE index. eq_ref can be used for indexed columns that are compared using the = operator. The comparison value can be a constant or an expression that uses columns from tables that are read before this table. In the following examples, MySQL can use an eq_ref join to process ref_table: SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.COLUMN; SELECT * FROM ref_table,other_table WHERE ref_table.key_column_part1=other_table.COLUMN AND ref_table.key_column_part2=1;
  • 12. Join Types – Part II ref / ref_or_null – these types of joins use a nonunique index to find anywhere from one to many rows from a table. Example: SELECT * FROM customer_address ca where ca.postal_code=”27713”; If the join type is ref_or_null than it means that query is also looking for null values from an index column. index_merge – in this type of join multiple indexes are used to locate the data. With the index_merge the key column in the output row contains a list of indexes used. unique_subquery - unique_subquery is just an index lookup function that replaces the subquery completely for better efficiency. This type replaces ref for some IN subqueries of the following form: value IN (SELECT primary_key FROM single_table WHERE some_expr)
  • 13. Join Types – Part III index_subquery – this kind of query takes advantage of an index to speed results on a subquery: Example: SELECT cm.last_name, cm.first_name FROM customer_master cm WHERE cm.customer_id IN (SELECT ca.customer_id FROM customer_address ca WHERE ca.postal_code = “TVC15-3CPU”); range - only rows that are in a given range are retrieved, using an index to select the rows. The key column in the output row indicates which index is used. The key_len contains the longest key part that was used. The ref column is NULL for this type. range can be used when a key column is compared to a constant using any of the =, <>, >, >=, <, <=, IS NULL, <=>, BETWEEN, or IN operators: SELECT * FROM tbl_name WHERE key_column BETWEEN 10 AND 20; index - this join type is the same as ALL, except that the index tree is scanned (instead of the entire table). This usually is faster than ALL because the index file usually is smaller than the data file. MySQL can use this join type when the query uses only columns that are part of a single index. ALL – the optimizer is forced to perform a full-table scan and read all rows in a table to find your results. Example: SELECT last_name from customer_table where last_name LIKE “%john%”;
  • 14. What to Avoid in Queries correlated subquery ● Version specific “gotchas”: ● Prior to MySQL 5.0 queries only use one index – Prior to MySQL 5.0 in the case of OR conditions – in a WHERE clause – MySQL uses a full-table scan
  • 15. Indexing Strategies you should minimally use one index per table ● however, don't index every column!! ● indexes are more costly to update – always try and use indexes with high ● selectivity
  • 16. Selectivity Selectivity of an index - the ratio of the number of distinct values in the indexed column(s) to the number of records. The ideal selectivity is one. Such a selectivity can be reached only by unique indexes on NOT NULL columns (UNIQUE or PRIMARY KEY columns). Example: Query A select count (*) from users; Query B select count(distinct username) from users; B/A = selectivity A higher selectivity (closer to one) is more desirable. If selectivity is too low the query optimizer won't use it.
  • 17. Full Table Scans – Part I Full table scans are where the database will read the entire table without an index. almost always not the desired behavior ● How do we determine if MySQL is going to perform a full table scan? EXPLAIN ●
  • 18. Full Table Scans – Part II Reasons why full table scans are performed no WHERE clause ● no index on any field in WHERE clause ● poor selectivity on an indexed field ● too many records meet WHERE conditions ● MySQL version less than 5.0 and using OR in a ● WHERE clause using SELECT * FROM ●
  • 19. Covering Indexes – Part I A covering index is an indexing strategy where the index encompasses every field returned by a SELECT statement. Why is this a good thing? Because it is faster to read everything from the index than use the index to look up information in rows. Example: You have an index (a,b) on table_1 SELECT b from table_1 where a=5
  • 20. Covering Indexes – Part II Where do covering indexes benefit? When you have large tables ● When you have long rows (BLOBS for example) ● When extra columns do not increase key length ● significantly When you have a large join with a number of ● secondary table lookups When a lot of rows match the same key value ● MyISAM tables benefit more than Innodb because ● MyISAM does not cache rows
  • 21. Duplicate Indexes – Part I When tables have multiple indexes defined on the same columns it has duplicate indexes. Example: PRIMARY KEY (id) UNIQUE KEY id(id) KEY id2(id)
  • 22. Duplicate Indexes – Part II Notes: Duplicate indexes apply to indexes of the same type. It may make sense to have indexes of different types created on the same column. Example: BTREE index and a FULLTEXT index Order of columns is important - index (a,b) is not a duplicate of index (b,a)
  • 23. Redundant Indexes – Part I Redundant indexes are prefixes of other indexes Example: KEY (A) KEY (A,B) KEY (A(10)) Redundant indexes are almost never helpful. Queries that take advantage of redundant indexes will also be able to make use of the longer indexes.
  • 24. Redundant Indexes – Part II When are redundant indexes useful? If an index is just too long Example: if A is an int and B is a varchar(255) which holds a great deal of long data than using KEY(A) might be significantly faster than using KEY (A,B) There are no tools currently bundled with the MySQL distribution to check schemas for redundant and duplicate indexes.
  • 25. Complete Example create database mysql_query optimization; use mysql_query_optimization; create table students ( student_id integer not null auto_increment primary key, name char(40)); create table tests ( test_id integer not null auto_increment primary key, total integer, name char(40)); create table grades ( test_id integer, student_id integer, score decimal(5,2));
  • 26. Inserting Data echo quot;insert into tests values (1,100, 'test 1');quot; > insert_tests.sql echo quot;insert into tests values (2,100, 'test 2');quot; >> insert_tests.sql echo quot;insert into tests values (3,300, 'test 3');quot; >> insert_tests.sql echo quot;-- insert studentsquot; > insert_students.sql for i in `seq 1 50000` do echo quot;insert into students values ($i, '$i diff');quot; >> insert_students.sql done mysql -u root -p mysql_query_optimization < insert_tests.sql mysql -u root -p mysql_query_optimization < insert_students.sql
  • 27. Inserting Grading Data insert into grades select 1, student_id, rand()*100 from students order by rand() limit 40000 ; insert into grades select 2, student_id, rand()*100 from students order by rand() limit 40000 ; insert into grades select 3, student_id, rand()*300 from students order by rand() limit 40000 ;
  • 28. Testing w/o Indexes – Part I -- what was the average score on test 1? mysql> select avg(score) from grades where test_id=1; +---------------+ | avg(score) | +---------------+ | 49.887198 | +---------------+ 1 row in set (0.06 sec) -- what students didn't take test 1? select count(*) from students s left join grades g on (s.student_id = g.student_id and test_id=2) where g.student_id is null ; *** canceled query after 4200 seconds
  • 29. Testing w/o Indexes – Part I EXPLAINed mysql> explain select count(*) from students s left join grades g on (s.student_id = g.student_id and test_id=2) where g.student_id is nullG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: s type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 50000 Extra: Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table: g type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 120000 Extra: Using where
  • 30. Testing w/o Indexes – Part II -- how many students took zero tests? select count(*) from students s left join grades g on (s.student_id = g.student_id) where g.student_id is null; ***canceled query after 5700 seconds
  • 31. Testing w/o Indexes – Part II EXPLAINed mysql> explain select count(*) from students s left join grades g on (s.student_id = g.student_id) where g.student_id is nullG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: s type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 50000 Extra: Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table: g type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 120000 Extra: Using where
  • 32. Changes!!! -mysql> alter table grades add index(test_id); Query OK, 120000 rows affected (0.52 sec) Records: 120000 Duplicates: 0 Warnings: 0 mysql> alter table grades add index(student_id); Query OK, 120000 rows affected (0.88 sec) Records: 120000 Duplicates: 0 Warnings: 0
  • 33. Testing with Indexes – Part I -- what students didn't take test 1? mysql> select count(*) from students s left join grades g on (s.student_id = g.student_id and test_id=2) where g.student_id is null ; +----------+ | count(*) | +----------+ | 10000 | +----------+ 1 row in set (1.40 sec)
  • 34. Testing with Indexes – Part I EXPLAINed mysql> explain select count(*) from students s left join grades g on (s.student_id = g.student_id and test_id=2) where g.student_id is nullG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: s type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 50000 Extra: Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table: g type: ref possible_keys: test_id,student_id key: student_id key_len: 5 ref: mysql_query_optimization.s.student_id rows: 2 Extra: Using where
  • 35. Testing with Indexes – Part II -- how many students took zero tests? mysql> select count(*) from students s left join grades g on (s.student_id = g.student_id) where g.student_id is null; +----------+ | count(*) | +----------+ | 443 | +----------+ 1 row in set (1.19 sec)
  • 36. Testing with Indexes – Part II EXPLAINed mysql> explain select count(*) from students s left join grades g on (s.student_id = g.student_id) where g.student_id is nullG *************************** 1. row *************************** id: 1 select_type: SIMPLE table: s type: index possible_keys: NULL key: PRIMARY key_len: 4 ref: NULL rows: 50000 Extra: Using index *************************** 2. row *************************** id: 1 select_type: SIMPLE table: g type: ref possible_keys: student_id key: student_id key_len: 5 ref: mysql_query_optimization.s.student_id rows: 2 Extra: Using where; Using index
  • 37. MySQL Optimizer – Part I There are two types of optimizers: ● Cost-based ● Rule-based Cost-based optimizers estimates the execution times of performing a query various ways. It tries to choose the lowest execution time. Rule-based optimizers build the best query based on a set of rules. Rule-based optimizers are quicker than cost- based optimizers but cost-based optimizers usually offer better performance. There are too many exceptions to the rules that the calculation overhead of cost-based optimizations is worthwhile.
  • 38. MySQL Optimizer – Part II Both cost-based and rule-based optimizers will return correct results. The cost-based optimizer uses index selectivity to determine the best execution for joins.
  • 39. Wrong Choices The query optimizer will make wrong choices from time to time EXPLAIN is your friend!! ●
  • 40. Schema Guidelines Always use the smallest data type possible? Do you REALLY need that BIGINT? the smaller your data type the more index and data ● records can fit into a single block of memory normalize first and denormalize later ●
  • 41. Query Cache To use the QC effectively you must understand your applications read/write ratio. QC design is a compromise between CPU usage and ● read performance Bigger QC does not automatically mean better ● performance – even for heavy read applications Any modification of any table referenced in a SELECT ● will invalidate any QC entry that uses that table
  • 42. Benchmarking – Part I When performing benchmarks: change one item at a time ● – configuration (my.cnf) variable – addition of an index – modification to a schema table – change to a SQL script re-run the benchmark after making a change ● make comparisons apple–apple not apple-orange ●
  • 43. Benchmarking – Part II Isolate the benchmarking environment Disable non-essential services ● – Network traffic analyzers – Non essential daemons – MySQL query cache Use a testing machine if at all possible ● Save the benchmark results Save all benchmark files ● – Result files of test runs – Configuration files at time of runs – OS/hardware configuration changes
  • 44. Benchmarking – Part III Why a benchmarking framework? ● automates the most tedious part of testing ● standardizes benchmark results ● can customize to your needs ● framework can be analyzed by outside parties determine if framework provides consistent results – if framework is biased –
  • 45. Benchmarking Tools Sysbench Supersmack AB (ApacheBench) Generic Frameworks: Junit/Ant (Java) MyBench (Perl) thewench (PHP) QPP (Query Processing Programs)
  • 46. Resources Books: SQL Tuning by Dan Tow MySQL Database Design and Tuning by Robert Schneider Websites: http://www.mysqlperformanceblog.com http://www.planetmysql.org Mailing Lists: http://lists.mysql.com (general list)