High Performance MySQL-trang-2
High Performance MySQL-trang-2
MySQL is very different from other database servers, and its architectural characteris-
tics make it useful for a wide range of purposes as well as making it a poor choice for
others. MySQL is not perfect, but it is flexible enough to work well in very demanding
environments, such as web applications. At the same time, MySQL can power embed-
ded applications, data warehouses, content indexing and delivery software, highly
available redundant systems, online transaction processing (OLTP), and much more.
To get the most from MySQL, you need to understand its design so that you can
work with it, not against it. MySQL is flexible in many ways. For example, you can
configure it to run well on a wide range of hardware, and it supports a variety of data
types. However, MySQL’s most unusual and important feature is its storage-engine
architecture, whose design separates query processing and other server tasks from data
storage and retrieval. This separation of concerns lets you choose how your data is
stored and what performance, features, and other characteristics you want.
This chapter provides a high-level overview of the MySQL server architecture, the major
differences between the storage engines, and why those differences are important. We’ll
finish with some historical context and benchmarks. We’ve tried to explain MySQL by
simplifying the details and showing examples. This discussion will be useful for those
new to database servers as well as readers who are experts with other database servers.
1
Figure 1-1. A logical view of the MySQL server architecture
built-in functions (e.g., dates, times, math, and encryption). Any functionality provided
across storage engines lives at this level: stored procedures, triggers, and views, for
example.
The third layer contains the storage engines. They are responsible for storing and
retrieving all data stored “in” MySQL. Like the various filesystems available for GNU/
Linux, each storage engine has its own benefits and drawbacks. The server communi-
cates with them through the storage engine API. This interface hides differences
between storage engines and makes them largely transparent at the query layer. The
API contains a couple of dozen low-level functions that perform operations such as
“begin a transaction” or “fetch the row that has this primary key.” The storage engines
don’t parse SQL1 or communicate with each other; they simply respond to requests
from the server.
1. One exception is InnoDB, which does parse foreign key definitions, because the MySQL server doesn’t
yet implement them itself.
2. MySQL 5.5 and newer versions support an API that can accept thread-pooling plugins, so a small pool
of threads can service many connections.
Concurrency Control
Anytime more than one query needs to change data at the same time, the problem of
concurrency control arises. For our purposes in this chapter, MySQL has to do this at
two levels: the server level and the storage engine level. Concurrency control is a big
topic to which a large body of theoretical literature is devoted, so we will just give you
a simplified overview of how MySQL deals with concurrent readers and writers, so you
have the context you need for the rest of this chapter.
We’ll use an email box on a Unix system as an example. The classic mbox file format
is very simple. All the messages in an mbox mailbox are concatenated together, one
after another. This makes it very easy to read and parse mail messages. It also makes
mail delivery easy: just append a new message to the end of the file.
Concurrency Control | 3