Performance Tuning (2)
Performance Tuning (2)
Advanced
Database
Systems
[email protected]
1
MySQL
Performance
Tuning
[email protected]
MySQL Performance Tuning
One of the ways we measure
applications is through performance. One
of the metrics for application
performance is user experience, which
generally translates to “did the user need
to wait longer than a reasonable amount
of time to get what they wanted.”
[email protected] 3
MySQL Performance Tuning
This metric can mean different things in
different scenarios.
For a mobile shopping app, response times
can’t be more than a couple of seconds.
For an employee’s HR page, responses
might be allowed to take a few seconds
longer.
[email protected] 4
MySQL Performance Tuning
Whatever the standard, it is essential to
maintain good performance for applications.
Otherwise, users will complain (or worse, go to a
different application). One of the factors that
affects application performance is database
performance. The interaction between
applications, websites, and databases is critical
in establishing the level of application
performance.
[email protected] 5
MySQL Performance Tuning
A central component of this interaction is how
applications query the database and how the
database responds to requests.
By any measure, MySQL is one of the most
popular database management systems. More
enterprises are shifting to MySQL (and other open
source databases) as a database solution in their
production environments.
[email protected] 6
Benefits of MySQL Performance Tuning
One of the ways we measure
applications is through performance. One
of the metrics for application
performance is user experience, which
generally translates to “did the user need
to wait longer than a reasonable amount
of time to get what they wanted.”
[email protected] 7
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
To function, a database server needs
four fundamental resources: CPU, Memory,
Disk, and Network.
If any of these is weak, erratic, or
overloaded, then the database server is
very likely to perform poorly.
[email protected] 8
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
▸ CPU (Processor)
Processors are usually considered the measure
of how fast a system is. Pay attention to the
MySQL processes and the percentage of
processor usage they require.
Processors are more expensive to upgrade, but
if the CPU is a bottleneck, an upgrade might be
necessary
[email protected] 9
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
▸ Memory
Memory represents the total amount of RAM in
your MySQL server. Adjusting the memory cache
can improve performance. If the server don’t
have enough memory, or if the existing memory
isn’t optimized, it can end up damaging the
database performance instead of improving it.
[email protected] 10
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
▸ Memory
Like other bottlenecks, if the server is
constantly running out of memory, it can be
upgraded by adding more. If the server run short
of memory, the server will cache data storage
(like a hard drive) to act as memory. Database
caching slows down performance.
[email protected] 11
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
▸ Disk/Storage
Take a moment to evaluate the storage. If the
server is using traditional hard disk drives (HDD),
it must be upgraded to solid-state drives (SSD)
for a performance improvement.
If disk usage is much higher than usage of other
resources, consider adding more storage or
upgrading to faster storage.
[email protected] 12
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
▸ Disk/Storage
Take a moment to evaluate the storage. If the
server is using traditional hard disk drives (HDD),
it must be upgraded to solid-state drives (SSD)
for a performance improvement.
If disk usage is much higher than usage of other
resources, consider adding more storage or
upgrading to faster storage.
[email protected] 13
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
▸ Network
It’s important to monitor network traffic to make
sure you have sufficient infrastructure to manage
the load.
Overloading your network can lead to latency,
dropped packets, and even server outages. Make
sure you have enough network bandwidth to
accommodate your normal levels of database
traffic.
[email protected] 14
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
When choosing hardware for MySQL,
ensure good-performing components all
around. Just as important, balance them
reasonably well against each other. Often,
organizations will select servers with fast
CPUs and disks but that are starved for
memory.
[email protected] 15
Performance Tuning for MySQL
1. Balance the 4 Fundamental Resources
When it comes to troubleshooting, check
the performance and utilization of all four
resources, with a careful eye toward
determining whether they are performing
poorly or are simply being asked to do too
much work.
[email protected] 16
Performance Tuning for MySQL
2. Use InnoDB, Not MyISAM
MyISAM is an older database style used for
some MySQL databases. It is a less
efficient database design. The newer
InnoDB supports more advanced features
and has in-built optimization mechanics.
[email protected] 17
Performance Tuning for MySQL
2. Use InnoDB, Not MyISAM
InnoDB uses a clustered index and keeps data
in pages, which are stored in consecutive
physical blocks. If a value is too large for a
page, InnoDB moves it to another location,
then indexes the value. This feature helps keep
relevant data in the same place on the storage
device, meaning it takes the physical hard
drive less time to access the data.
[email protected] 18
Performance Tuning for MySQL
3. Use the Latest Version of MySQL
Using the latest version is not always
feasible for older and legacy databases.
But whenever possible, you should
check the version of MySQL in use and
upgrade to the latest.
[email protected] 19
Performance Tuning for MySQL
3. Use the Latest Version of MySQL
A part of the ongoing development
includes performance enhancements.
Some common performance adjustments
may be rendered obsolete by newer
versions of MySQL. In general, it’s always
better to use native MySQL performance
enhancement over scripting and
configuration files.
[email protected] 20
Performance Tuning for MySQL
4. Functions in Predicates
Avoid using a function in the predicate of a query.
For example:
[email protected] 23
Performance Tuning for MySQL
7. Use ORDER BY Appropriately
The ORDER BY expression sorts results by the
specified column. It can be used to sort by two
columns at once. These should be sorted in
the same order, ascending or descending. If
you try to sort different columns in different
order, it will slow down performance. You may
combine this with an index to speed up the
sorting.
[email protected] 24
Performance Tuning for MySQL
8. JOIN, UNION, DISTINCT
Try to use an inner join whenever
possible. An outer join looks at
additional data outside the specified
columns. That’s fine if you need that
data, but it’s a waste of performance to
include data that won’t be required.
[email protected] 25
Performance Tuning for MySQL
8. JOIN, UNION, DISTINCT
The UNION and DISTINCT commands are
sometimes included in queries. Like an
outer join, it’s fine to use these expressions
if they are necessary. However, they add
additional sorting and reading of the
database. If you don’t need them, it’s better
to find a more efficient expression.
[email protected] 26
Performance Tuning for MySQL
9. Use the EXPLAIN Function
Modern MySQL databases include an EXPLAIN
function.
Appending the EXPLAIN expression to the
beginning of a query will read and evaluate the
query. If there are inefficient expressions or
confusing structures, EXPLAIN can help you find
them. You can then adjust the phrasing of your
query to avoid unintentional table scans or other
performance hits.
[email protected] 27
Performance Tuning for MySQL
10. MySQL Server Configuration
This configuration involves making changes to your
/etc/mysql/my.cnf file. Proceed with caution and make minor
changes at a time.
[email protected] 28
Performance Tuning for MySQL
10. MySQL Server Configuration
query_cache_size – Specifies the size of the cache of MySQL
queries waiting to run. The recommendation is to start with
small values around 10MB and then increase to no more than 100-
200MB. With too many cached queries, you can experience a
cascade of queries “Waiting for cache lock.” If
your queries keep backing up, a better
procedure is to use EXPLAIN to evaluate each
query and find ways to make them more
efficient.
[email protected] 29
Performance Tuning for MySQL
10. MySQL Server Configuration
max_connection – Refers to the number of connections
allowed into the database. If you’re getting errors citing “Too
many connections,” increasing this value may help.
[email protected] 31
Performance Tuning for MySQL
11. Don’t focus too much on configuration
DBAs tend to spend a huge amount of time
tweaking configurations. The result is usually
not a big improvement and can sometimes
even be very damaging. I’ve seen a lot of
“optimized” servers that crashed constantly,
ran out of memory, and performed poorly
when the workload got a little more intense.
[email protected] 32
Performance Tuning for MySQL
11. Don’t focus too much on configuration
The defaults that ship with MySQL are one-size-
fits-none and badly outdated, but you don’t need to
configure everything. It’s better to get the
fundamentals right and change other settings only
if needed. In most cases, you can get 95 percent of
the server’s peak performance by setting about 10
options correctly. The few situations where this
doesn’t apply are going to be edge cases unique to
your circumstances.
[email protected] 33
Performance Tuning for MySQL
11. Avoid Pagination Queries
Applications that paginate tend to bring
the server to its knees. In showing you a
page of results, with a link to go to the next
page, these applications typically group
and sort in ways that can’t use indexes, and
they employ a LIMIT and offset that cause
the server to do a lot of work generating,
then discarding rows.
[email protected] 34
Performance Tuning for MySQL
11. Avoid Pagination Queries
Optimizations can often be found in the
user interface itself. Instead of showing
the exact number of pages in the results
and links to each page individually, you can
just show a link to the next page. You can
also prevent people from going to pages
too far from the first page.
[email protected] 35
Performance Tuning for MySQL
12. Learn the three rules of indexing
Indexing is probably the most
misunderstood topic in databases
because there are so many ways to get
confused about how indexes work and
how the server uses them. It takes a
lot of effort to really understand what’s
going on.
[email protected] 36
Performance Tuning for MySQL
12. Learn the three rules of indexing
Indexes, when properly designed, serve three important
purposes in a database server:
1. Indexes let the server find groups of adjacent
rows instead of single rows. Many people think the
purpose of an index is to find individual rows, but
finding single rows leads to random disk
operations, which is slow. It’s much better to find
groups of rows, all or most of which are
interesting, than to find rows one at a time.
[email protected] 37
Performance Tuning for MySQL
12. Learn the three rules of indexing
2. Indexes let the server avoid sorting by reading the
rows in a desired order. Sorting is costly. Reading
rows in the desired order is much faster.
[email protected] 38
Performance Tuning for MySQL
13. Leverage the expertise of your peers
Don’t try to go it alone. If you’re puzzling over a
problem and doing what seems logical and
sensible to you, that’s great. This will work about
19 times out of 20. The other time, you’ll go down
a rabbit hole that will be very costly and time-
consuming, precisely because the solution you’re
trying seems to make a lot of sense.