SlideShare a Scribd company logo
1 of 66
Download to read offline
MySQL Optimization
    MySQL User Conference

        Jeremy Zawodny
            Yahoo!

        April 12th, 2003
      San Jose, California

http://jeremy.zawodny.com/mysql/
          Copyright 2003, Jeremy Zawodny
About Me
•   Engineer in Y! Search (prev. Y! Finance)
•   MySQL user for over 5 years
•   Active in MySQL community
•   Write about LAMP for Linux Magazine
•   MySQL advocacy & support at Yahoo!

Home: Jeremy@Zawodny.com
Work: jzawodn@yahoo-inc.com
http://jeremy.zawodny.com/mysql/


                  Copyright 2003, Jeremy Zawodny
Outline
•   Introduction
•   Why Optimize?
•   Goals
•   Database Design
•   Application Design
•   Writing Fast Queries
•   MySQL Server Tuning
•   Operating System Tuning
•   Hardware Tuning
•   Network & Replication
•   Where to Learn More
•   Questions and Answers
                   Copyright 2003, Jeremy Zawodny
Starting Questions
•   What version of MySQL are you using?
•   What languages are being used?
•   Which operating systems?
•   Familiarity with other RDBMS servers?
•   Role? DBA? Developer? SysAdmin?
•   MySQL dedicated or shared servers?
•   How fast is your growth?
    – Transaction rates
    – Data volume

                    Copyright 2003, Jeremy Zawodny
What you Need to Know
• You should ask questions at any time
  – There should be sufficient time
• MySQL usage
  – Basic queries (SELECT, UPDATE, INSERT)
  – Installation or where files are located
• Basic programming concepts
  – Any language will do
• Operating system basics
  – Memory usage, swapping, etc.

                  Copyright 2003, Jeremy Zawodny
MySQL at Yahoo!
•   Roughly 200-400 servers world-wide
•   FreeBSD and Linux
•   Commodity hardware
•   Replaces home-grown “database” systems
•   Replaces Oracle in a few cases
•   Typical install uses between 1-20GB
•   Used both “live” and in batch processing
•   Replication and load-balancing


                  Copyright 2003, Jeremy Zawodny
Why Optimize?
• You can do more with less
  – MySQL on “normal” hardware scales well
  – A little time can save thousands in hardware
  – The classic story goes…
• As you data grows, you’ll need to
  – Performance will degrade over time
  – You’re probably not monitoring it anyway
• It is easier than re-coding you apps
• Your users will notice if you don’t!

                  Copyright 2003, Jeremy Zawodny
MySQL’s Defaults
•   Tuned for small and medium data sets
•   Uses very little memory even if available
•   Suitable for use in a shared environment
•   Assumes little about your hardware
•   Begins to slow as growth continues
•   Uses non-transactional tables (MyISAM)
    – That’s what most people need (90%)
    – Very low overhead


                  Copyright 2003, Jeremy Zawodny
Scaling MySQL
•   Like Linux, MySQL scales up and down
•   Can run many MySQL instances at once
•   Can run one very big MySQL instance
•   Can run with only a few MB of memory
    – Suitable for small devices
    – Will be disk-bound
• Can embed using libmysqld (MySQL 4.x)
• Can recompile to add/remove features
    – Table types, query cache, etc.

                    Copyright 2003, Jeremy Zawodny
Using Less Hardware
• Hardware is rarely the bottleneck
  – Well-tuned servers are often disk-bound
• MySQL isn’t using it aggressively
  – You must configure it
• Modern CPUs are very fast
  – What you have is probably sufficient
• Memory is plentiful
  – You’re probably not using what you have
• Upgrades do little to solve most problems!

                 Copyright 2003, Jeremy Zawodny
Goals
•   Learn to write fast queries and applications
•   Learn to design and use the right tables
•   Know where to look for bottlenecks
•   Predict behavior as load increases
•   Understand what to monitor over time
•   Understand how MySQL uses system resources
•   Learn what settings you can adjust
    – In your operating system
    – In MySQL
    – In your applications
• Know where to learn more…

                     Copyright 2003, Jeremy Zawodny
Database Design
• Normalize your data by default
  – Sometime you need to de-normalize
  – When in doubt, benchmark
     •   MySQL super-smack
     •   MySQL benchmark suite
     •   Home-grown tools
     •   Use your real apps!




                    Copyright 2003, Jeremy Zawodny
Database Design
• Select the right column types
  –   No bigger than you need
  –   MySQL provides a ton of column types
  –   Use NOT NULL where it makes sense
  –   Use fixed column sizes if you can
       • MyISAM tables with fixed rows are faster
       • Concurrency improvements
  – Store compressed data when possible

  See: http://www.mysql.com/doc/S/t/Storage_requirements.html



                       Copyright 2003, Jeremy Zawodny
Database Design
• Select the right table types
  – What locking model do you need?
       • Table (MyISAM)
       • Row (InnoDB)
       • Page (BDB)
  –   Consider ratio of reads to writes
  –   Foreign key constraints?
  –   Do you need transactions?
  –   Can you afford to lose records in a crash?
  –   Do you know MySQL’s table types?

                    Copyright 2003, Jeremy Zawodny
Database Design
• MyISAM Tables
  –   Very efficient
  –   Compact storage
  –   In-memory key cache for index data
  –   Table locking
  –   No transactions
  –   Good for
       • High volume logging (write)
       • High volume reads
       • Not both
  – Variations: Compressed, RAID, Merge

                     Copyright 2003, Jeremy Zawodny
Database Design
• Compressed MyISAM Tables
  – Read-only
  – Good for CD-ROMs and archives
• MyISAM RAID Tables
  – Break the 2GB/4GB/whatever barrier
• MyISAM Merge Tables
  – Many physically identical MyISAM tables
  – Can treat as a single table (or not)



                Copyright 2003, Jeremy Zawodny
Database Design
• HEAP Tables
  – Stored in memory
       • They will vanish at server shutdown
  – Very fast hash-based lookups
       • Limited index use
       • Range queries are slower
  –   B-Tree available in 4.1
  –   Table locking
  –   Great for static lookups
  –   Size can be limited to prevent disaster

                     Copyright 2003, Jeremy Zawodny
Database Design
• BDB Tables
  –   Transactional
  –   Automatic recovery
  –   Tables grow as needed
  –   Page-level locking (8KB page)
       • Single READ-COMMITTED isolation level
  –   Uses Berkeley DB under the hood
  –   Few users actually use BDB
  –   Works well for small - medium transaction rate
  –   Locking on the last page can be a problem

                   Copyright 2003, Jeremy Zawodny
Database Design
• InnoDB Tables
  – Modeled after Oracle
       • Row-level locking
       • Non-locking SELECTs
       • Uses pre-allocated tablespace files
  – Multiple isolation levels
       • Easily changed with a SET command
  –   Referential integrity - foreign keys
  –   High performance
  –   Very high concurrency
  –   Automatic recovery after crash

                      Copyright 2003, Jeremy Zawodny
Database Design
• Use Indexes wisely
  – Don’t use several indexes when one will do
  – Understand the “leftmost prefix” rule
       • Index on (col1, col2, col3) vs. 3 indexes
  –   Don’t index columns until you need to
  –   Verify that indexes are used (difficult)
  –   Use partial indexes on large (text) fields
  –   Index a hash rather than a large value (URL)
       • MD5 is an excellent choice
       • It’s even built-in

                      Copyright 2003, Jeremy Zawodny
Database Design
• Use full-text indexing if you need it
  –   MyISAM tables only
  –   Very fast
  –   Excellent in MySQL 4.x
  –   Results are ranked (like a search engine might)
  –   Boolean queries
       • Flexible
       • Mostly feature-complete
  – Works on any textual data
       • Other character sets will need 4.1 or 5.0

                      Copyright 2003, Jeremy Zawodny
Full-Text Search
• Use 4.0 if possible
  – Indexing is much faster
  – Stop word list customization
  – Min word size easily changed
       • Remember to rebuild indexes after changing
• In 5.0 we should see
  –   Per-table stop word lists
  –   Per-table word length options
  –   Per-table word characters lists
  –   These might be per-index!
                     Copyright 2003, Jeremy Zawodny
Application Design
• Don’t store data you don’t need
  – Compress it
  – Get rid of it
• Don’t store computable data
  – MySQL can do it
  – Your app can do it
• Don’t ask for data you don’t need…
  – Do you really need all fields?
    SELECT * FROM…


                    Copyright 2003, Jeremy Zawodny
Application Design
• Use MySQL extensions for speed
   –   REPLACE queries
       Bundled INSERT queries
   –
   –   Multi-table deletes
   –   User variables
• Use logging to track bottlenecks
• Don’t perform unnecessary queries
   – Cache data (static lookup tables)
   – Use the Query Cache if you must
• Benchmark your application
   – Know where the bottlenecks are
   – Know how a slow db affects your application

                     Copyright 2003, Jeremy Zawodny
Application Design
• Use transactions
  – Prevents data loss
  – Server does less random I/O
  – Performance and reliability
• Keep the clients “near” the server
  –   Network latency is a killer
  –   Replication can solve geography problems
  –   Can also help solve geology problems (quake)
  –   Running app and MySQL on same hardware


                   Copyright 2003, Jeremy Zawodny
Application Design
• Think about growth
  – There are size limits that you might hit
  – InnoDB and MyISAM both have them (sort of)
• Keep primary keys short for InnoDB




                Copyright 2003, Jeremy Zawodny
Application Design
• Use prepared queries and placeholders
  –   MySQL doesn’t yet support them
  –   Your API may
  –   When MySQL does, you benefit!
  –   The API may be more efficient anyway
  –   MySQL 4.1 and PHP 5.0 benefit

      SELECT name, address, state, zip
       FROM customers
      WHERE id = ?



                     Copyright 2003, Jeremy Zawodny
Application Design
• Web apps
  – Use (but don’t over-use) connection pooling
  – Use middleware to abstract the database
     • May also provide caching and pooling
  – Don’t keep everything in the database!
     • Images can live on the file system
     • But you might want to replicate them
  – Pick the fastest driver you can
     • Java has several, Perl has two
     • On Windows, use the “most native”


                   Copyright 2003, Jeremy Zawodny
Break!




Copyright 2003, Jeremy Zawodny
Writing Fast Queries
•   Use Indexes
    Use EXPLAIN SELECT
•
•   Simplify where clause
•   Watch Slow query log
•   Bundle INSERTs
•   UNIONs




                Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• Understanding how MySQL runs queries
• You need to think like MySQL does
• Some of its goals are…
  –   Eliminate as many rows as possible
  –   Use indexes where possible
  –   Avoid table scans
  –   Consider many join orders
  –   Avoid hitting the disk
  –   Avoid using the data records if the index has it


                    Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• EXPLAIN SELECT
 –   Tells you what MySQL is thinking
 –   Which keys (indexes) can it use
 –   Which keys will it use
 –   How many rows must it examine (roughly)
      • ANALYZE TABLE can help
 – How hard must MySQL work?




                 Copyright 2003, Jeremy Zawodny
Writing Fast Queries
      • EXPLAIN SELECT
mysql> EXPLAIN SELECT * FROM Headlines H, S2H S WHERE S.Symbol = 'YHOO' and H.Id = S.HeadlineId;
+-------+--------+-------------------+---------+---------+--------------+------+-------------------------+
| table | type   | possible_keys     | key      | key_len | ref         | rows | Extra                    |
+-------+--------+-------------------+---------+---------+--------------+------+-------------------------+
|S      | ref    | HeadlineId,Symbol | Symbol   |     75 | const        |   383 | where used; Using index |
|H      | eq_ref | PRIMARY           | PRIMARY |       4 | S.HeadlineId |     1 | where used              |
+-------+--------+-------------------+---------+---------+--------------+------+-------------------------+
2 rows in set (0.00 sec)


mysql> EXPLAIN SELECT * FROM Headlines H, S2H S WHERE S.Symbol = 'YHOO' and H.Id = S.HeadlineId ORDER BY Time DESC;
+-------+--------+-------------------+---------+---------+--------------+------+-----------------------------------------+
| table | type   | possible_keys     | key      | key_len | ref         | rows | Extra                                    |
+-------+--------+-------------------+---------+---------+--------------+------+-----------------------------------------+
|S      | ref    | HeadlineId,Symbol | Symbol   |     75 | const        |   383 | where used; Using index; Using temporary;
                                                                                  Using filesort
|H      | eq_ref | PRIMARY           | PRIMARY |       4 | S.HeadlineId |    1 | where used                               |
+-------+--------+-------------------+---------+---------+--------------+------+-----------------------------------------+




                                                Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• EXPLAIN SELECT
 – Table
   • Order is significant
   • Aliases appear
 – Type
   • System
      – Table has one row
      – Easily optimized
   • Const
      – Only a single row matches
      – Read once



                  Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• EXPLAIN SELECT (continued)
  – Type (continued)
     • eq_ref
         – One row matches per combination
         – Unique index match
     • ref
         – Several matching rows per combination
         – Non-unique index
     • range
         – A range of rows will be retrieved
     • index
         – Index will be scanned for matches
         – Like a table scan, but faster
     • all
         – Full table scan
         – Worst case

                        Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• EXPLAIN SELECT (continued)
  – Possible keys
     • What MySQL had to choose from
  – Key
     • What it decided to use
  – Key length
     • Length (in bytes) of the longest key
  – Ref
     • Which column it will match with
  – Rows
     • Approximately how many rows must be examined


                    Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• EXPLAIN SELECT (continued)
  – Extra information
     • Using filesort
        – An extra pass is required to sort the records
        – This can be slow at times
     • Using index
        – Data will come from the index rather than rows
        – This can speed things up
     • Using temporary
        – MySQL will create a temporary table
        – It’ll be a disk-based table if it’s too large
     • Where used
        – The where clause will be applied to this table


                       Copyright 2003, Jeremy Zawodny
Writing Fast Queries
• Optimizer tips and tricks
  – It’s smart, but not perfect
  – Only one index per table per query
     • You may need to de-normalize to get performance
     • You may need to write two queries instead of one
  – Don’t compute in the WHERE
     • MySQL doesn’t know how to optimize constant
       expressions

 SELECT * FROM Headlines
 WHERE Time > SUBDATE(NOW(), INTERVAL 7 DAY);

                   Copyright 2003, Jeremy Zawodny
Insert Speed
• In 4.1 and beyond, use prepared statements
• In older versions
  – Single inserts are the slowest
  – Multi-rows inserts are faster
  – Bulk-loading (LOAD DATA or mysqlimport)
    are very, very, very fast
• Using InnoDB, use transactions wisely
  – Many inserts in AUTOCOMMIT mode are
    very, very slow


                Copyright 2003, Jeremy Zawodny
Query Cache
•   Part of MySQL 4.0
•   Can seriously boost performance
•   Might save legacy apps you can’t change
•   Use query cache selectively if you have lots
    of writes
    – SELECT SQL_CACHE …
• Use mytop to watch query cache stats
    – Version 1.3 and 1.4 will have more stats



                    Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• Watching performance
• Benchmarking
• Tunable Parameters
  – Most bang, least effort
  – Incremental gains
• Methodology
  – Iterative testing
  – Long-term monitoring



                  Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
Watching Performance




             Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• Key Performance Numbers
 – Queries per second
   • Min, Max, Short-term, Long-Term
 – Bytes per second
   • Inbound vs. Outbound
 – New connections per second
 – Idle vs. Active clients
 – Key cache efficiency
 – Query cache efficiency
              Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• How MySQL uses memory
  – Main Global Caches and Buffers
     •   Query cache
     •   Key buffer
     •   Table cache
     •   InnoDB buffer pool
     •   InnoDB log buffer
  – Main Thread-specific Caches and Buffers
     • Record buffer
     • Sort buffer
     • Join buffer

                    Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• SHOW STAUTS
  – Created_tmp_disk_tables
     • If large, increase temp table size
  – Handler_*
     • Determine key buffer effectiveness
  – Com_*
     • Find the commands that are most often run
  – Questions and Uptime
     • Compute queries/second
  – Select_*
     • How many types of each SELECT are executed
  – Qcache_*
     • Query cache performance


                       Copyright 2003, Jeremy Zawodny
On-the-Fly Tuning
• Use MySQL’s SET syntax to change
  parameters on the fly (new in 4.0)
  –   max_connections
  –   wait_timeout
  –   thread_cache
  –   key_buffer_size
  –   table_cache
• Don’t change too much at once
• Persistent connections aren’t always fast!
• Changes may take time to notice

                  Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• SHOW STATUS
 – Table_locks_*
   • How many times are queries waiting for locks?
   • Concurrency problems show up here
 – Bytes_*
   • How much data are you pumping out
   • Compare with inbound traffic
 – Qcache_*
   • Query cache performance
   • Memory usage


                 Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• my.cnf file parameters
  –   key_buffer
  –   tmp_table_size
  –   Table_cache
  –   Max_connections
  –   Max_user_connections
  –   Long_query_time
  –   Thread_concurrency



                  Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• my.cnf file parameters
  –   innodb_buffer_pool_size
  –   innodb_log_file_size
  –   innodb_file_io_threads
  –   innodb_flush_log_at_trx_commit
  –   innodb_log_buffer_size
  –   innodb_flush_method
       • fdatasync
       • O_DSYNC



                     Copyright 2003, Jeremy Zawodny
InnoDB Performance
• Transaction log flushing has three options
  – (1) Flush on commit
  – (0) Never flush
  – (2) Flush once per second




                 Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• Fileysystem Issues
   – Spread data among disks
      •   Put heavily used and lightly used databases together
      •   RAID-5 or RAID-10 for data (w/batter-backed cache)
      •   RAID-1 for logs
          New CREATE TABLE makes this easier
      •
   – Logs separate from data
      • Logs are mostly serialized writes
      • Tables are updated and used in mostly random fashion
   – If you have a lot of tables in a database
      • Use a filesystem designed to handle it
      • ResiserFS is a good choice
   – A journaling filesystem
      • Makes crash recovery faster
      • Better utilizes disk I/O (usually)
                         Copyright 2003, Jeremy Zawodny
MySQL Server Tuning
• Upgrade once in a while
  – New versions are often faster
  – Better optimizations in query parser
  – New and enhanced caching
• Convert older tables to newer format
  – ISAM to MyISAM
  – BDB to InnoDB (or not)
  – ALTER TABLE my_table TYPE=InnoDB
• Don’t flush the transaction logs on commit

                  Copyright 2003, Jeremy Zawodny
Upgrade Testing
• It’s often a good idea to keep up-to-date
• Performance tweaks and optimizations are
  introduced during the maintenance process
• Be sure to test your critical queries carefully
• Always use a real load test or read the
  EXPLAIN output
• Without load, “slow” queries are often fast



                 Copyright 2003, Jeremy Zawodny
Operating System Tuning
• Virtual Memory Use
  – FreeBSD - excellent
  – Linux - varies wildly
     • 2.4.9 good
     • >= 2.4.16 good
     • Others not good
• Per-process limits on:
  – Memory
  – File descriptors
• Network duplex settings
• Competing processes on the machine?
                   Copyright 2003, Jeremy Zawodny
Operating System Tuning
• Key Metrics
  – Memory used/free/cache/buffer
     • Swapping is very bad
     • You might even disable swap
  – Paging and page faults
     • Make sure there’s no memory pressure
     • Server variables might be wrong if many page faults
  – Disk I/O
     • Make sure the I/O is where you expect
     • Disk I/O tuning (see your OS docs)
  – Processes running, sleeping, blocked/waiting
  – Actual CPU usage (might be too low)
                   Copyright 2003, Jeremy Zawodny
Operating System Tuning
• Useful Unix Tools
  – top, ps, vmstat
  – iostat, sar
  – mrtg, rrdtool
• Windows Tools
  – Performance Monitor (perfmeter)
  – Task Manager
  – Others I don’t know (not a Windows guy)



                  Copyright 2003, Jeremy Zawodny
Hardware Tuning
• CPU Issues
  – Speed
  – Single vs. Dual
• RAM Issues
• Disks
  – IDE vs. SCSI
  – RAID (hardware or software)
  – Battery-backed cache on controller is best



                  Copyright 2003, Jeremy Zawodny
Hardware Tuning
• Network
  – The faster the better (watch latency)
  – Duplex settings
• I/O Channels
  –   The more the merrier
  –   Most PC motherboards suck
  –   Server-class boards are better
  –   High-end hardware (IBM, Sun) are best
  –   You’ll be lucky to have this problem!


                   Copyright 2003, Jeremy Zawodny
Network & Replication
• Put clients near servers
• Redundancy is very good
• Put slaves near master(s)
  – Unless that’s stupid
• Use load-balancing technology
  – High(er) availability MySQL
  – Easy scaling of traffic
• Pick the correct replication topology
• Backup slaves instead of the master

                  Copyright 2003, Jeremy Zawodny
Network & Replication
• Replication is quite flexible
• Can build a topology to solve most
  problems
• Only a few nagging issues
  – Auto-increment fields
  – Automatic Fail-over
  – Need to build health checks
     • Performance/Latency
     • Slave stopped?
• Come to my replication talk to learn more!
                  Copyright 2003, Jeremy Zawodny
Stupid Query Tricks
• Use SQL_CALC_ROWS and
  FOUND_ROWS() rather than double-
  queries:
  – SELECT … LIMIT N, M
  – SELECT COUNT(*)
• Instead:
  – SELECT … LIMIT N, M
  – SELECT FOUND_ROWS()
• Requires far less overhead on MySQL

               Copyright 2003, Jeremy Zawodny
Stupid Query Tricks
• Use a UNION to re-write a slow OR query
  SELECT * FROM mytable
  WHERE col1 = ‘foo’ OR col2 = ‘bar’

  (SELECT * FROM mytable
  WHERE col1 = ‘foo’)
  UNION
  (SELECT * FROM mytable
  WHERE col2 = ‘bar’)



                Copyright 2003, Jeremy Zawodny
Stupid Query Tricks
• Ordering, limiting, and ordering again

  (SELECT * FROM mytable
  WHERE col1 = ‘foo’
  ORDER BY col2 LIMIT 50)
  ORDER BY col3




                Copyright 2003, Jeremy Zawodny
Final Advice
•   Read
•   Learn
•   Test
•   Ask
•   Monitor
•   Benchmark




                Copyright 2003, Jeremy Zawodny
For More Info…
• MySQL mailing lists
  – Visit lists.mysql.com
• Books
  – MySQL Manual
  – MySQL (Paul’s Book)
  – Managing & Using MySQL
• Web searching




                  Copyright 2003, Jeremy Zawodny
Questions and Answers




       Copyright 2003, Jeremy Zawodny

More Related Content

What's hot

Indexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningIndexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuning
OSSCube
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Aaron Shilo
 

What's hot (20)

ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 
Index in sql server
Index in sql serverIndex in sql server
Index in sql server
 
Oracle sql high performance tuning
Oracle sql high performance tuningOracle sql high performance tuning
Oracle sql high performance tuning
 
MySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer GuideMySQL 8.0 Optimizer Guide
MySQL 8.0 Optimizer Guide
 
Query logging with proxysql
Query logging with proxysqlQuery logging with proxysql
Query logging with proxysql
 
MySQL INDEXES
MySQL INDEXESMySQL INDEXES
MySQL INDEXES
 
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
 
Indexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuningIndexing the MySQL Index: Key to performance tuning
Indexing the MySQL Index: Key to performance tuning
 
RocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesRocksDB Performance and Reliability Practices
RocksDB Performance and Reliability Practices
 
The MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer TraceThe MySQL Query Optimizer Explained Through Optimizer Trace
The MySQL Query Optimizer Explained Through Optimizer Trace
 
Highly efficient backups with percona xtrabackup
Highly efficient backups with percona xtrabackupHighly efficient backups with percona xtrabackup
Highly efficient backups with percona xtrabackup
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Parallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQLParallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQL
 
All of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL DeveloperAll of the Performance Tuning Features in Oracle SQL Developer
All of the Performance Tuning Features in Oracle SQL Developer
 
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
Exploring Oracle Database Performance Tuning Best Practices for DBAs and Deve...
 
MySQL for beginners
MySQL for beginnersMySQL for beginners
MySQL for beginners
 
Oracle SQL 1 Day Tutorial
Oracle SQL 1 Day TutorialOracle SQL 1 Day Tutorial
Oracle SQL 1 Day Tutorial
 
Sq lite presentation
Sq lite presentationSq lite presentation
Sq lite presentation
 
Always on in sql server 2017
Always on in sql server 2017Always on in sql server 2017
Always on in sql server 2017
 

Similar to Mysql Optimization

Greatdebate Postgres vs Mysql
Greatdebate Postgres vs MysqlGreatdebate Postgres vs Mysql
Greatdebate Postgres vs Mysql
Krishna Infosoft
 
My S Q L Replication Getting The Most From Slaves
My S Q L  Replication  Getting  The  Most  From  SlavesMy S Q L  Replication  Getting  The  Most  From  Slaves
My S Q L Replication Getting The Most From Slaves
PerconaPerformance
 
Scalarea Aplicatiilor Web - 2009
Scalarea Aplicatiilor Web - 2009Scalarea Aplicatiilor Web - 2009
Scalarea Aplicatiilor Web - 2009
Andrei Gheorghe
 
Optimizing MySQL for Cascade Server
Optimizing MySQL for Cascade ServerOptimizing MySQL for Cascade Server
Optimizing MySQL for Cascade Server
hannonhill
 
From One to a Cluster
From One to a ClusterFrom One to a Cluster
From One to a Cluster
guestd34230
 
[Roblek] Distributed computing in practice
[Roblek] Distributed computing in practice[Roblek] Distributed computing in practice
[Roblek] Distributed computing in practice
javablend
 
Handling Massive Writes
Handling Massive WritesHandling Massive Writes
Handling Massive Writes
Liran Zelkha
 
What every developer should know about database scalability, PyCon 2010
What every developer should know about database scalability, PyCon 2010What every developer should know about database scalability, PyCon 2010
What every developer should know about database scalability, PyCon 2010
jbellis
 
Colvin exadata mistakes_ioug_2014
Colvin exadata mistakes_ioug_2014Colvin exadata mistakes_ioug_2014
Colvin exadata mistakes_ioug_2014
marvin herrera
 

Similar to Mysql Optimization (20)

Greatdebate Postgres vs Mysql
Greatdebate Postgres vs MysqlGreatdebate Postgres vs Mysql
Greatdebate Postgres vs Mysql
 
The Great Debate: PostgreSQL vs MySQL
The Great Debate: PostgreSQL vs MySQLThe Great Debate: PostgreSQL vs MySQL
The Great Debate: PostgreSQL vs MySQL
 
My S Q L Replication Getting The Most From Slaves
My S Q L  Replication  Getting  The  Most  From  SlavesMy S Q L  Replication  Getting  The  Most  From  Slaves
My S Q L Replication Getting The Most From Slaves
 
Under the Hood with MySQL
Under the Hood with MySQLUnder the Hood with MySQL
Under the Hood with MySQL
 
Practical MySQL
Practical MySQLPractical MySQL
Practical MySQL
 
MySQL Tuning
MySQL TuningMySQL Tuning
MySQL Tuning
 
Scalarea Aplicatiilor Web - 2009
Scalarea Aplicatiilor Web - 2009Scalarea Aplicatiilor Web - 2009
Scalarea Aplicatiilor Web - 2009
 
Scaling with sync_replication using Galera and EC2
Scaling with sync_replication using Galera and EC2Scaling with sync_replication using Galera and EC2
Scaling with sync_replication using Galera and EC2
 
Storage Systems for High Scalable Systems Presentation
Storage Systems for High Scalable Systems PresentationStorage Systems for High Scalable Systems Presentation
Storage Systems for High Scalable Systems Presentation
 
Optimizing MySQL for Cascade Server
Optimizing MySQL for Cascade ServerOptimizing MySQL for Cascade Server
Optimizing MySQL for Cascade Server
 
Advanced Deployment
Advanced DeploymentAdvanced Deployment
Advanced Deployment
 
Development to Production with Sharded MongoDB Clusters
Development to Production with Sharded MongoDB ClustersDevelopment to Production with Sharded MongoDB Clusters
Development to Production with Sharded MongoDB Clusters
 
From One to a Cluster
From One to a ClusterFrom One to a Cluster
From One to a Cluster
 
[Roblek] Distributed computing in practice
[Roblek] Distributed computing in practice[Roblek] Distributed computing in practice
[Roblek] Distributed computing in practice
 
Handling Massive Writes
Handling Massive WritesHandling Massive Writes
Handling Massive Writes
 
What every developer should know about database scalability, PyCon 2010
What every developer should know about database scalability, PyCon 2010What every developer should know about database scalability, PyCon 2010
What every developer should know about database scalability, PyCon 2010
 
Colvin exadata mistakes_ioug_2014
Colvin exadata mistakes_ioug_2014Colvin exadata mistakes_ioug_2014
Colvin exadata mistakes_ioug_2014
 
Scalabe MySQL Infrastructure
Scalabe MySQL InfrastructureScalabe MySQL Infrastructure
Scalabe MySQL Infrastructure
 
Scaling Drupal: Not IF... HOW
Scaling Drupal: Not IF... HOWScaling Drupal: Not IF... HOW
Scaling Drupal: Not IF... HOW
 
MySQL Aquarium Paris
MySQL Aquarium ParisMySQL Aquarium Paris
MySQL Aquarium Paris
 

More from Renato Shirakashi

Sistemas Baseados em Conhecimento
Sistemas Baseados em ConhecimentoSistemas Baseados em Conhecimento
Sistemas Baseados em Conhecimento
Renato Shirakashi
 

More from Renato Shirakashi (9)

Direct Labs - 4 erros estratégicos simples que arruinam projetos de criação d...
Direct Labs - 4 erros estratégicos simples que arruinam projetos de criação d...Direct Labs - 4 erros estratégicos simples que arruinam projetos de criação d...
Direct Labs - 4 erros estratégicos simples que arruinam projetos de criação d...
 
Avaliação Heurística
Avaliação HeurísticaAvaliação Heurística
Avaliação Heurística
 
Naked Objects
Naked ObjectsNaked Objects
Naked Objects
 
A riqueza das Redes - segundo Yochai Benkler
A riqueza das Redes - segundo Yochai BenklerA riqueza das Redes - segundo Yochai Benkler
A riqueza das Redes - segundo Yochai Benkler
 
Sistemas de Recomendação
Sistemas de Recomendação Sistemas de Recomendação
Sistemas de Recomendação
 
Sistemas Baseados em Conhecimento
Sistemas Baseados em ConhecimentoSistemas Baseados em Conhecimento
Sistemas Baseados em Conhecimento
 
Comparação entre frameworks PHP
Comparação entre frameworks PHPComparação entre frameworks PHP
Comparação entre frameworks PHP
 
Desenvolva rapidamente utilizando o framework Desenvolva rapidamente utiliz...
Desenvolva rapidamente
utilizando o framework
Desenvolva rapidamente utiliz...Desenvolva rapidamente
utilizando o framework
Desenvolva rapidamente utiliz...
Desenvolva rapidamente utilizando o framework Desenvolva rapidamente utiliz...
 
Via6 - Assinantes e Minha Rede
Via6 - Assinantes e Minha RedeVia6 - Assinantes e Minha Rede
Via6 - Assinantes e Minha Rede
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 

Mysql Optimization

  • 1. MySQL Optimization MySQL User Conference Jeremy Zawodny Yahoo! April 12th, 2003 San Jose, California http://jeremy.zawodny.com/mysql/ Copyright 2003, Jeremy Zawodny
  • 2. About Me • Engineer in Y! Search (prev. Y! Finance) • MySQL user for over 5 years • Active in MySQL community • Write about LAMP for Linux Magazine • MySQL advocacy & support at Yahoo! Home: Jeremy@Zawodny.com Work: jzawodn@yahoo-inc.com http://jeremy.zawodny.com/mysql/ Copyright 2003, Jeremy Zawodny
  • 3. Outline • Introduction • Why Optimize? • Goals • Database Design • Application Design • Writing Fast Queries • MySQL Server Tuning • Operating System Tuning • Hardware Tuning • Network & Replication • Where to Learn More • Questions and Answers Copyright 2003, Jeremy Zawodny
  • 4. Starting Questions • What version of MySQL are you using? • What languages are being used? • Which operating systems? • Familiarity with other RDBMS servers? • Role? DBA? Developer? SysAdmin? • MySQL dedicated or shared servers? • How fast is your growth? – Transaction rates – Data volume Copyright 2003, Jeremy Zawodny
  • 5. What you Need to Know • You should ask questions at any time – There should be sufficient time • MySQL usage – Basic queries (SELECT, UPDATE, INSERT) – Installation or where files are located • Basic programming concepts – Any language will do • Operating system basics – Memory usage, swapping, etc. Copyright 2003, Jeremy Zawodny
  • 6. MySQL at Yahoo! • Roughly 200-400 servers world-wide • FreeBSD and Linux • Commodity hardware • Replaces home-grown “database” systems • Replaces Oracle in a few cases • Typical install uses between 1-20GB • Used both “live” and in batch processing • Replication and load-balancing Copyright 2003, Jeremy Zawodny
  • 7. Why Optimize? • You can do more with less – MySQL on “normal” hardware scales well – A little time can save thousands in hardware – The classic story goes… • As you data grows, you’ll need to – Performance will degrade over time – You’re probably not monitoring it anyway • It is easier than re-coding you apps • Your users will notice if you don’t! Copyright 2003, Jeremy Zawodny
  • 8. MySQL’s Defaults • Tuned for small and medium data sets • Uses very little memory even if available • Suitable for use in a shared environment • Assumes little about your hardware • Begins to slow as growth continues • Uses non-transactional tables (MyISAM) – That’s what most people need (90%) – Very low overhead Copyright 2003, Jeremy Zawodny
  • 9. Scaling MySQL • Like Linux, MySQL scales up and down • Can run many MySQL instances at once • Can run one very big MySQL instance • Can run with only a few MB of memory – Suitable for small devices – Will be disk-bound • Can embed using libmysqld (MySQL 4.x) • Can recompile to add/remove features – Table types, query cache, etc. Copyright 2003, Jeremy Zawodny
  • 10. Using Less Hardware • Hardware is rarely the bottleneck – Well-tuned servers are often disk-bound • MySQL isn’t using it aggressively – You must configure it • Modern CPUs are very fast – What you have is probably sufficient • Memory is plentiful – You’re probably not using what you have • Upgrades do little to solve most problems! Copyright 2003, Jeremy Zawodny
  • 11. Goals • Learn to write fast queries and applications • Learn to design and use the right tables • Know where to look for bottlenecks • Predict behavior as load increases • Understand what to monitor over time • Understand how MySQL uses system resources • Learn what settings you can adjust – In your operating system – In MySQL – In your applications • Know where to learn more… Copyright 2003, Jeremy Zawodny
  • 12. Database Design • Normalize your data by default – Sometime you need to de-normalize – When in doubt, benchmark • MySQL super-smack • MySQL benchmark suite • Home-grown tools • Use your real apps! Copyright 2003, Jeremy Zawodny
  • 13. Database Design • Select the right column types – No bigger than you need – MySQL provides a ton of column types – Use NOT NULL where it makes sense – Use fixed column sizes if you can • MyISAM tables with fixed rows are faster • Concurrency improvements – Store compressed data when possible See: http://www.mysql.com/doc/S/t/Storage_requirements.html Copyright 2003, Jeremy Zawodny
  • 14. Database Design • Select the right table types – What locking model do you need? • Table (MyISAM) • Row (InnoDB) • Page (BDB) – Consider ratio of reads to writes – Foreign key constraints? – Do you need transactions? – Can you afford to lose records in a crash? – Do you know MySQL’s table types? Copyright 2003, Jeremy Zawodny
  • 15. Database Design • MyISAM Tables – Very efficient – Compact storage – In-memory key cache for index data – Table locking – No transactions – Good for • High volume logging (write) • High volume reads • Not both – Variations: Compressed, RAID, Merge Copyright 2003, Jeremy Zawodny
  • 16. Database Design • Compressed MyISAM Tables – Read-only – Good for CD-ROMs and archives • MyISAM RAID Tables – Break the 2GB/4GB/whatever barrier • MyISAM Merge Tables – Many physically identical MyISAM tables – Can treat as a single table (or not) Copyright 2003, Jeremy Zawodny
  • 17. Database Design • HEAP Tables – Stored in memory • They will vanish at server shutdown – Very fast hash-based lookups • Limited index use • Range queries are slower – B-Tree available in 4.1 – Table locking – Great for static lookups – Size can be limited to prevent disaster Copyright 2003, Jeremy Zawodny
  • 18. Database Design • BDB Tables – Transactional – Automatic recovery – Tables grow as needed – Page-level locking (8KB page) • Single READ-COMMITTED isolation level – Uses Berkeley DB under the hood – Few users actually use BDB – Works well for small - medium transaction rate – Locking on the last page can be a problem Copyright 2003, Jeremy Zawodny
  • 19. Database Design • InnoDB Tables – Modeled after Oracle • Row-level locking • Non-locking SELECTs • Uses pre-allocated tablespace files – Multiple isolation levels • Easily changed with a SET command – Referential integrity - foreign keys – High performance – Very high concurrency – Automatic recovery after crash Copyright 2003, Jeremy Zawodny
  • 20. Database Design • Use Indexes wisely – Don’t use several indexes when one will do – Understand the “leftmost prefix” rule • Index on (col1, col2, col3) vs. 3 indexes – Don’t index columns until you need to – Verify that indexes are used (difficult) – Use partial indexes on large (text) fields – Index a hash rather than a large value (URL) • MD5 is an excellent choice • It’s even built-in Copyright 2003, Jeremy Zawodny
  • 21. Database Design • Use full-text indexing if you need it – MyISAM tables only – Very fast – Excellent in MySQL 4.x – Results are ranked (like a search engine might) – Boolean queries • Flexible • Mostly feature-complete – Works on any textual data • Other character sets will need 4.1 or 5.0 Copyright 2003, Jeremy Zawodny
  • 22. Full-Text Search • Use 4.0 if possible – Indexing is much faster – Stop word list customization – Min word size easily changed • Remember to rebuild indexes after changing • In 5.0 we should see – Per-table stop word lists – Per-table word length options – Per-table word characters lists – These might be per-index! Copyright 2003, Jeremy Zawodny
  • 23. Application Design • Don’t store data you don’t need – Compress it – Get rid of it • Don’t store computable data – MySQL can do it – Your app can do it • Don’t ask for data you don’t need… – Do you really need all fields? SELECT * FROM… Copyright 2003, Jeremy Zawodny
  • 24. Application Design • Use MySQL extensions for speed – REPLACE queries Bundled INSERT queries – – Multi-table deletes – User variables • Use logging to track bottlenecks • Don’t perform unnecessary queries – Cache data (static lookup tables) – Use the Query Cache if you must • Benchmark your application – Know where the bottlenecks are – Know how a slow db affects your application Copyright 2003, Jeremy Zawodny
  • 25. Application Design • Use transactions – Prevents data loss – Server does less random I/O – Performance and reliability • Keep the clients “near” the server – Network latency is a killer – Replication can solve geography problems – Can also help solve geology problems (quake) – Running app and MySQL on same hardware Copyright 2003, Jeremy Zawodny
  • 26. Application Design • Think about growth – There are size limits that you might hit – InnoDB and MyISAM both have them (sort of) • Keep primary keys short for InnoDB Copyright 2003, Jeremy Zawodny
  • 27. Application Design • Use prepared queries and placeholders – MySQL doesn’t yet support them – Your API may – When MySQL does, you benefit! – The API may be more efficient anyway – MySQL 4.1 and PHP 5.0 benefit SELECT name, address, state, zip FROM customers WHERE id = ? Copyright 2003, Jeremy Zawodny
  • 28. Application Design • Web apps – Use (but don’t over-use) connection pooling – Use middleware to abstract the database • May also provide caching and pooling – Don’t keep everything in the database! • Images can live on the file system • But you might want to replicate them – Pick the fastest driver you can • Java has several, Perl has two • On Windows, use the “most native” Copyright 2003, Jeremy Zawodny
  • 30. Writing Fast Queries • Use Indexes Use EXPLAIN SELECT • • Simplify where clause • Watch Slow query log • Bundle INSERTs • UNIONs Copyright 2003, Jeremy Zawodny
  • 31. Writing Fast Queries • Understanding how MySQL runs queries • You need to think like MySQL does • Some of its goals are… – Eliminate as many rows as possible – Use indexes where possible – Avoid table scans – Consider many join orders – Avoid hitting the disk – Avoid using the data records if the index has it Copyright 2003, Jeremy Zawodny
  • 32. Writing Fast Queries • EXPLAIN SELECT – Tells you what MySQL is thinking – Which keys (indexes) can it use – Which keys will it use – How many rows must it examine (roughly) • ANALYZE TABLE can help – How hard must MySQL work? Copyright 2003, Jeremy Zawodny
  • 33. Writing Fast Queries • EXPLAIN SELECT mysql> EXPLAIN SELECT * FROM Headlines H, S2H S WHERE S.Symbol = 'YHOO' and H.Id = S.HeadlineId; +-------+--------+-------------------+---------+---------+--------------+------+-------------------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------+--------+-------------------+---------+---------+--------------+------+-------------------------+ |S | ref | HeadlineId,Symbol | Symbol | 75 | const | 383 | where used; Using index | |H | eq_ref | PRIMARY | PRIMARY | 4 | S.HeadlineId | 1 | where used | +-------+--------+-------------------+---------+---------+--------------+------+-------------------------+ 2 rows in set (0.00 sec) mysql> EXPLAIN SELECT * FROM Headlines H, S2H S WHERE S.Symbol = 'YHOO' and H.Id = S.HeadlineId ORDER BY Time DESC; +-------+--------+-------------------+---------+---------+--------------+------+-----------------------------------------+ | table | type | possible_keys | key | key_len | ref | rows | Extra | +-------+--------+-------------------+---------+---------+--------------+------+-----------------------------------------+ |S | ref | HeadlineId,Symbol | Symbol | 75 | const | 383 | where used; Using index; Using temporary; Using filesort |H | eq_ref | PRIMARY | PRIMARY | 4 | S.HeadlineId | 1 | where used | +-------+--------+-------------------+---------+---------+--------------+------+-----------------------------------------+ Copyright 2003, Jeremy Zawodny
  • 34. Writing Fast Queries • EXPLAIN SELECT – Table • Order is significant • Aliases appear – Type • System – Table has one row – Easily optimized • Const – Only a single row matches – Read once Copyright 2003, Jeremy Zawodny
  • 35. Writing Fast Queries • EXPLAIN SELECT (continued) – Type (continued) • eq_ref – One row matches per combination – Unique index match • ref – Several matching rows per combination – Non-unique index • range – A range of rows will be retrieved • index – Index will be scanned for matches – Like a table scan, but faster • all – Full table scan – Worst case Copyright 2003, Jeremy Zawodny
  • 36. Writing Fast Queries • EXPLAIN SELECT (continued) – Possible keys • What MySQL had to choose from – Key • What it decided to use – Key length • Length (in bytes) of the longest key – Ref • Which column it will match with – Rows • Approximately how many rows must be examined Copyright 2003, Jeremy Zawodny
  • 37. Writing Fast Queries • EXPLAIN SELECT (continued) – Extra information • Using filesort – An extra pass is required to sort the records – This can be slow at times • Using index – Data will come from the index rather than rows – This can speed things up • Using temporary – MySQL will create a temporary table – It’ll be a disk-based table if it’s too large • Where used – The where clause will be applied to this table Copyright 2003, Jeremy Zawodny
  • 38. Writing Fast Queries • Optimizer tips and tricks – It’s smart, but not perfect – Only one index per table per query • You may need to de-normalize to get performance • You may need to write two queries instead of one – Don’t compute in the WHERE • MySQL doesn’t know how to optimize constant expressions SELECT * FROM Headlines WHERE Time > SUBDATE(NOW(), INTERVAL 7 DAY); Copyright 2003, Jeremy Zawodny
  • 39. Insert Speed • In 4.1 and beyond, use prepared statements • In older versions – Single inserts are the slowest – Multi-rows inserts are faster – Bulk-loading (LOAD DATA or mysqlimport) are very, very, very fast • Using InnoDB, use transactions wisely – Many inserts in AUTOCOMMIT mode are very, very slow Copyright 2003, Jeremy Zawodny
  • 40. Query Cache • Part of MySQL 4.0 • Can seriously boost performance • Might save legacy apps you can’t change • Use query cache selectively if you have lots of writes – SELECT SQL_CACHE … • Use mytop to watch query cache stats – Version 1.3 and 1.4 will have more stats Copyright 2003, Jeremy Zawodny
  • 41. MySQL Server Tuning • Watching performance • Benchmarking • Tunable Parameters – Most bang, least effort – Incremental gains • Methodology – Iterative testing – Long-term monitoring Copyright 2003, Jeremy Zawodny
  • 42. MySQL Server Tuning Watching Performance Copyright 2003, Jeremy Zawodny
  • 43. MySQL Server Tuning • Key Performance Numbers – Queries per second • Min, Max, Short-term, Long-Term – Bytes per second • Inbound vs. Outbound – New connections per second – Idle vs. Active clients – Key cache efficiency – Query cache efficiency Copyright 2003, Jeremy Zawodny
  • 44. MySQL Server Tuning • How MySQL uses memory – Main Global Caches and Buffers • Query cache • Key buffer • Table cache • InnoDB buffer pool • InnoDB log buffer – Main Thread-specific Caches and Buffers • Record buffer • Sort buffer • Join buffer Copyright 2003, Jeremy Zawodny
  • 45. MySQL Server Tuning • SHOW STAUTS – Created_tmp_disk_tables • If large, increase temp table size – Handler_* • Determine key buffer effectiveness – Com_* • Find the commands that are most often run – Questions and Uptime • Compute queries/second – Select_* • How many types of each SELECT are executed – Qcache_* • Query cache performance Copyright 2003, Jeremy Zawodny
  • 46. On-the-Fly Tuning • Use MySQL’s SET syntax to change parameters on the fly (new in 4.0) – max_connections – wait_timeout – thread_cache – key_buffer_size – table_cache • Don’t change too much at once • Persistent connections aren’t always fast! • Changes may take time to notice Copyright 2003, Jeremy Zawodny
  • 47. MySQL Server Tuning • SHOW STATUS – Table_locks_* • How many times are queries waiting for locks? • Concurrency problems show up here – Bytes_* • How much data are you pumping out • Compare with inbound traffic – Qcache_* • Query cache performance • Memory usage Copyright 2003, Jeremy Zawodny
  • 48. MySQL Server Tuning • my.cnf file parameters – key_buffer – tmp_table_size – Table_cache – Max_connections – Max_user_connections – Long_query_time – Thread_concurrency Copyright 2003, Jeremy Zawodny
  • 49. MySQL Server Tuning • my.cnf file parameters – innodb_buffer_pool_size – innodb_log_file_size – innodb_file_io_threads – innodb_flush_log_at_trx_commit – innodb_log_buffer_size – innodb_flush_method • fdatasync • O_DSYNC Copyright 2003, Jeremy Zawodny
  • 50. InnoDB Performance • Transaction log flushing has three options – (1) Flush on commit – (0) Never flush – (2) Flush once per second Copyright 2003, Jeremy Zawodny
  • 51. MySQL Server Tuning • Fileysystem Issues – Spread data among disks • Put heavily used and lightly used databases together • RAID-5 or RAID-10 for data (w/batter-backed cache) • RAID-1 for logs New CREATE TABLE makes this easier • – Logs separate from data • Logs are mostly serialized writes • Tables are updated and used in mostly random fashion – If you have a lot of tables in a database • Use a filesystem designed to handle it • ResiserFS is a good choice – A journaling filesystem • Makes crash recovery faster • Better utilizes disk I/O (usually) Copyright 2003, Jeremy Zawodny
  • 52. MySQL Server Tuning • Upgrade once in a while – New versions are often faster – Better optimizations in query parser – New and enhanced caching • Convert older tables to newer format – ISAM to MyISAM – BDB to InnoDB (or not) – ALTER TABLE my_table TYPE=InnoDB • Don’t flush the transaction logs on commit Copyright 2003, Jeremy Zawodny
  • 53. Upgrade Testing • It’s often a good idea to keep up-to-date • Performance tweaks and optimizations are introduced during the maintenance process • Be sure to test your critical queries carefully • Always use a real load test or read the EXPLAIN output • Without load, “slow” queries are often fast Copyright 2003, Jeremy Zawodny
  • 54. Operating System Tuning • Virtual Memory Use – FreeBSD - excellent – Linux - varies wildly • 2.4.9 good • >= 2.4.16 good • Others not good • Per-process limits on: – Memory – File descriptors • Network duplex settings • Competing processes on the machine? Copyright 2003, Jeremy Zawodny
  • 55. Operating System Tuning • Key Metrics – Memory used/free/cache/buffer • Swapping is very bad • You might even disable swap – Paging and page faults • Make sure there’s no memory pressure • Server variables might be wrong if many page faults – Disk I/O • Make sure the I/O is where you expect • Disk I/O tuning (see your OS docs) – Processes running, sleeping, blocked/waiting – Actual CPU usage (might be too low) Copyright 2003, Jeremy Zawodny
  • 56. Operating System Tuning • Useful Unix Tools – top, ps, vmstat – iostat, sar – mrtg, rrdtool • Windows Tools – Performance Monitor (perfmeter) – Task Manager – Others I don’t know (not a Windows guy) Copyright 2003, Jeremy Zawodny
  • 57. Hardware Tuning • CPU Issues – Speed – Single vs. Dual • RAM Issues • Disks – IDE vs. SCSI – RAID (hardware or software) – Battery-backed cache on controller is best Copyright 2003, Jeremy Zawodny
  • 58. Hardware Tuning • Network – The faster the better (watch latency) – Duplex settings • I/O Channels – The more the merrier – Most PC motherboards suck – Server-class boards are better – High-end hardware (IBM, Sun) are best – You’ll be lucky to have this problem! Copyright 2003, Jeremy Zawodny
  • 59. Network & Replication • Put clients near servers • Redundancy is very good • Put slaves near master(s) – Unless that’s stupid • Use load-balancing technology – High(er) availability MySQL – Easy scaling of traffic • Pick the correct replication topology • Backup slaves instead of the master Copyright 2003, Jeremy Zawodny
  • 60. Network & Replication • Replication is quite flexible • Can build a topology to solve most problems • Only a few nagging issues – Auto-increment fields – Automatic Fail-over – Need to build health checks • Performance/Latency • Slave stopped? • Come to my replication talk to learn more! Copyright 2003, Jeremy Zawodny
  • 61. Stupid Query Tricks • Use SQL_CALC_ROWS and FOUND_ROWS() rather than double- queries: – SELECT … LIMIT N, M – SELECT COUNT(*) • Instead: – SELECT … LIMIT N, M – SELECT FOUND_ROWS() • Requires far less overhead on MySQL Copyright 2003, Jeremy Zawodny
  • 62. Stupid Query Tricks • Use a UNION to re-write a slow OR query SELECT * FROM mytable WHERE col1 = ‘foo’ OR col2 = ‘bar’ (SELECT * FROM mytable WHERE col1 = ‘foo’) UNION (SELECT * FROM mytable WHERE col2 = ‘bar’) Copyright 2003, Jeremy Zawodny
  • 63. Stupid Query Tricks • Ordering, limiting, and ordering again (SELECT * FROM mytable WHERE col1 = ‘foo’ ORDER BY col2 LIMIT 50) ORDER BY col3 Copyright 2003, Jeremy Zawodny
  • 64. Final Advice • Read • Learn • Test • Ask • Monitor • Benchmark Copyright 2003, Jeremy Zawodny
  • 65. For More Info… • MySQL mailing lists – Visit lists.mysql.com • Books – MySQL Manual – MySQL (Paul’s Book) – Managing & Using MySQL • Web searching Copyright 2003, Jeremy Zawodny
  • 66. Questions and Answers Copyright 2003, Jeremy Zawodny