Sunteți pe pagina 1din 17

MySQL Architecture

Course Name:
Faculty Name:
Database
Administration
Ms. Zaiba Khan
with MySQL
Assistant Professor(CSE)
Branch-
School of
B.Tech-VI Semester Engineering &
Course Code: Technology
19009300 1
MySQL Architecture

MySQL architecture is broken into three


layers basically which can be defined by,

1. Application Layer
2. Logical Layer
3. Physical Layer
Application Layer

The application layer is where the clients and


users interact with the MySQL RDBMS. All the
services needed for connection handling,
authentication, security are here.
There are three main components in this layer
namely Administrators, Clients, Query Users as
shown in the below figure.
Application Layer
a. Administrators
Administrators use various administrative interface and utilities like
mysqladmin which performs tasks like shutting down the server and
creating or dropping databases, mysqldump for backing up the database
or copying databases to another server.
b. Clients
Clients communicate with MySQL through various interfaces and utilities
like MySQL API’s. The MySQL API sends the query to the server as a series
of tokens.
c. Query User
The query users interact with MySQL RDBMS through a query interface
that is mysql.
Logical Layer
The Logical Layer takes the data from the Application Layer.
It is divided into subsystems like
Query Processor,
Transaction Management,
Recovery Management,
Storage Management.
These subsystems work together to process the requests issued to the MySQL
database server.
The output of one of the above subsystems becomes the input for another.
Logical Layer
Query Processor –
 SELECT * FROM Manufacture.Orders WHERE order_id = 1009;
 In this query, we want to know order details for the order id 1009.

 a. When a query request is received from a client in the application layer, we


would execute this query using MySQL client session (MySQL Client is a
utility to execute all MySQL commands and return an output).
◦ It is the responsibility of the first component in Query Processor i.e. embedded DML
(Data Manipulation Language) pre-compiler to extract the relevant SQL statements
embedded in the client API command or to translate the client commands into the
corresponding SQL statements.

b. When a client session is established a “MySQL THREAD” is created in
MySQL to handle all the command executions and sessions.

c. The query is submitted to the MySQL server which is a “mysql” process
running on the remote or local server. “mysql” is MySQL Server domain
program which runs in background and manages database related incoming
and outgoing requests from clients.
Logical Layer
d. Once entering these server process the query is checked against a list of
previously executed SELECTs maintained by MySQL DBMS engine in an area
of memory known as “QUERY CACHE”.
If the same query was executed early the result set will be stored in
the Query cache. This saves MySQL from doing any work of parsing,
preprocessing and retrieving data from disk or memory resulting in fast
output.

e. If the same exact query is not present in QUERY CACHE, MySQL will move
to the next component i.e “PARSER” which will create a parse tree structure
based on the query so that it can be easily understood by the other components.

f. Once the parse tree structure is ready, the Query Preprocessor will check the
SQL syntax and check the semantics of the MySQL query to determine if the
query is valid.

g. Once the MySQL query is deemed to be valid, the security integration manager
is responsible for checking the GRANTS of user on the objects they want to
access through the query. For example, we want to retrieve data from
‘Manufacture’ database’s ‘Orders’ table.
Logical Layer

h. After determining the proper permissions and access, MySQL will take the
query to the most important component i.e “QUERY OPTIMIZER”. This is
the place where MySQL will decide on the execution plan to access and
retrieve the data as fast as possible.
i. Query Optimizer is responsible for checking which indexes exist on the
specified table and if any exist whether it can use the index to retrieve data
faster.
j. An index here on ‘order_id’ column of ‘Orders’ table will help the storage
engine layer to locate and retrieve data faster. Once confirmed MySQL will
create an “EXECUTION PLAN” and pass on the plan to “Storage engine”
layer (which I will be discussing in the next paragraphs).

Transaction Management – Next comes the Transaction Management which


is responsible for making sure that the transaction is logged and executed
automatically. It does so through the aid of the log manager and the
concurrency-control manager. It is also responsible for issuing the COMMIT
and the ROLLBACK SQL commands.
Logical Layer

 Recovery management – Here the log Manager logs every operation


executed in the database and stores the operation logs as MySQL Commands.
In case of system crash executing these commands will bring back the
database to the last stable stage. The recovery manager is responsible for
restoring the database to its last stable state. It does so by using the log for the
database, which is acquired from the buffer manager and executing each
operation in the log. Since the log manager logs all operations performed on
the database (from the beginning of the database’s life), executing each
command in the log file would recover the database to its last stable state.

 Storage Management – Storage Management has buffer manager which


allocates memory resources. It accepts the request from the execution engine,
requests the details from buffer manager, receives references to data with
memory from buffer manager. Returns this data to the upper layer.
Physical Layer
 The third layer is the Physical Layer which contains the storage engines. They
are responsible for storing and retrieving all data stored in MySQL. Physical
Layer of MySQL is slightly different from other RDBMS.

 The various engines available are MyISAM, InnoDB, CSV, Archive etc.
Starting from MySQL 5.5.5, the default storage engine for new tables is InnoDB
(The CREATE TABLE statement in MySQL creates InnoDB tables by default).

 Each storage engine has different characteristics and based on the requirement
of application we can choose an appropriate storage engine.

 MySQL stores each database (also called a schema) as a subdirectory of its data
directory in the underlying file system. Every database has a corresponding data
directory. When you create a table, MySQL stores the table definition in a .frm
file with the same name as the table.
 Thus, when you create a table named Orders, MySQL stores the table definition
in Orders.frm. These .frm files does not store data but only have the format that
contains the description of the table structure.
MySQL Architecture
MySQL Architecture

Client- Utility to connect MySQL server.

Server -
MySQL instance where actual data getting
stored and data processing is happening.
MySQL Architecture
Mysqld-
MySQL Server daemon program which runs in the
background and manages database related incoming
and outgoing requests from clients. mysqld is a multi-
threaded process which allows connection to multiple
sessions listen for all connections and manages MySQL
instance.

MySQL memory allocation-


Main MySQL memory is dynamic, examples
innodb_buffer_pool_size , key_buffer_size etc. Working
on shared nothing principal which means, every
session has unique execution plan and we can share
data sets only for the same session.
MySQL Architecture
Parser:
Check for SQL syntax by checking every character in
SQL query and generate SQL_ID for each SQL query.
◦ Also, Authentication check (user credentials ) will happen at
this stage.

Optimizer :
Creates an efficient query execution plan as per the
storage engine. It will rewrite a query. Example:
InnoDB has shared buffer so optimizer will get pre-
cached data from it. Using table statistics optimizer will
generate an execution plan for a SQL query.
Authorization Check (User access privileges) will
happen at this stage.
MySQL Architecture
Metadata cache:
Cache for object metadata information and statistics.

Query cache:
Shared identical queries from memory. If an identical query from client
found in query cache then, the MySQL server retrieves the results from the
query cache rather than parsing and executing that query again.
It’s a shared cache for sessions, so a result set generated by one client can
be sent in response to the same query issued by another client.
Query cache based on SQL_ID.SELECT data into view is the best example
of pre-cache data using query cache.

key cache:
Cache table indexes.In MySQL keys are indexes(In oracle keys are
constraints) if index size is small then it will cache index structure and data
leaf.If an index is large then it will only cache index structure.Used by
MyISAM storage engine.
MySQL Architecture
Storage engine:
MySQL component that manages physical data (file management) and
locations. Storage engine responsible for SQL statement execution and
fetching data from data files. Use as a plugin and can load/unload from
running MySQL server.
Few of them as following,
InnoDB :
◦ Fully transactional ACID.
◦ Offers REDO and UNDO for transactions.
◦ Data storage in tablespace:
– Multiple data files
– Logical object structure using InnoDB data and log buffer
◦ Row-level locking.
NDB (For MySQL Cluster):
◦ Fully Transactional and ACID Storage engine.
◦ Distribution execution of data and using multiple mysqld.
◦ NDB use logical data with own buffer for each NDB engine.
◦ Offers REDO and UNDO for transactions.
◦ Row-level locking.
MySQL Architecture
MyISAM:
◦ Non-transactional storage engine
◦ Speed for read
◦ Data storage in files and use key, metadata and query cache
– FRM  for table structure
– MYI for table index
– MYD for table data
◦ Table-level locking.
MEMORY:
◦ Non-transactional storage engine
◦ All data stored in memory other than table metadata and structure.
◦ Table-level locking.
ARCHIVE:
◦ Non-transactional storage engine,
◦ Store large amounts of compressed and unindexed data.
◦ Allow INSERT, REPLACE, and SELECT, but not DELETE or UPDATE sql operations.
◦ Table-level locking.
CSV:
◦ Stores data in flat files using comma-separated values format.
◦ Table structure need be created within MySQL server (.frm)

S-ar putea să vă placă și