<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tuan Tran</title>
    <description>The latest articles on Forem by Tuan Tran (@tuantran12).</description>
    <link>https://forem.com/tuantran12</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1508086%2F1d1955b8-a11d-48e0-85ea-e6d93a7a980e.png</url>
      <title>Forem: Tuan Tran</title>
      <link>https://forem.com/tuantran12</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tuantran12"/>
    <language>en</language>
    <item>
      <title>Research for MySQL architecture and Optimize performance</title>
      <dc:creator>Tuan Tran</dc:creator>
      <pubDate>Sun, 11 Aug 2024 12:04:00 +0000</pubDate>
      <link>https://forem.com/tuantran12/research-for-mysql-architecture-and-optimize-performance-5261</link>
      <guid>https://forem.com/tuantran12/research-for-mysql-architecture-and-optimize-performance-5261</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;MySQL is a relationship database management system developed on C and C++ programming languages, created by MySQL AB, which was founded by David Axmark, Allan Larsson, and Michael Widenius. The newest version is MySQL 9.0.&lt;br&gt;&lt;br&gt;
MySQL is an open source project with a GNU license.&lt;br&gt;&lt;br&gt;
MySQL is the most popular and useful database management system now because it's open source, has full features, and has high performance.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;MySQL architecture diagram: &lt;a href="https://dev.mysql.com/doc/refman/8.4/en/pluggable-storage-overview.html" rel="noopener noreferrer"&gt;Source&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg74j75akxergqmsgos5m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg74j75akxergqmsgos5m.png" alt="MySQL Architecture" width="500" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Client Layer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The first layer of MySQL system architecture.&lt;/li&gt;
&lt;li&gt;The important services of this layer are Connection Handling, Authentication and Security.

&lt;ul&gt;
&lt;li&gt;Connection Handling: Manages connections from clients. When a client wants to connect to the MySQL server, a new thread is created for the connection. The server caches threads when new connections are created.&lt;/li&gt;
&lt;li&gt;Authentication: When a client connects to a server, it must provide authentication information to establish a connection. Typically, clients authenticate using a username and password.&lt;/li&gt;
&lt;li&gt;Security: MySQL provides many features to protect the database and verify user permissions after authentication.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Server Layer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The next layer of MySQL is responsible for processing query statements and providing many utilities.&lt;/li&gt;
&lt;li&gt;The main features:

&lt;ul&gt;
&lt;li&gt;Parser: When a client requests a query statement, the server analyzes it to create a query tree, rewrites the query, and define the order and indexes to be used to execute the query statement.&lt;/li&gt;
&lt;li&gt;Optimizer: It optimizes the query statement before execution. The optimizer doesn't care what engine is used, but the storage engine can impact optimization for the specific query.&lt;/li&gt;
&lt;li&gt;Query cache: The query cache stores the result of a query statement. If a client requests the same query again, the server returns the cached result, bypassing the parsing and optimization steps. This feature was deprecated in version 8.0 or higher due to its bottleneck problem. &lt;/li&gt;
&lt;li&gt;Services &amp;amp; Utitlity:

&lt;ul&gt;
&lt;li&gt;Backup &amp;amp; Restore&lt;/li&gt;
&lt;li&gt;Security: provider user and permissions system&lt;/li&gt;
&lt;li&gt;Replication: This is a process to copy and sync data from the main server to many child servers.&lt;/li&gt;
&lt;li&gt;Cluster&lt;/li&gt;
&lt;li&gt;Partitioning: Split the table into many parts using a specific logic.&lt;/li&gt;
&lt;li&gt;Workbench: It is a visual tool designed for interacting with and utilizing MySQL.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Storage Layer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It's a layer of responsibility for the way we store data in the database.&lt;/li&gt;
&lt;li&gt;By default, MySQL uses the InnoDB storage engine.&lt;/li&gt;
&lt;li&gt;MySQL supports many different storage engines:

&lt;ul&gt;
&lt;li&gt;InnoDB (default)&lt;/li&gt;
&lt;li&gt;MyISAM&lt;/li&gt;
&lt;li&gt;MEMORY&lt;/li&gt;
&lt;li&gt;CSV&lt;/li&gt;
&lt;li&gt;ARCHIVE&lt;/li&gt;
&lt;li&gt;BLACKHOLE&lt;/li&gt;
&lt;li&gt;MERGE&lt;/li&gt;
&lt;li&gt;FEDERATED&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Optimize Performance for MySQL
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Optimize SQL statement
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Explain Statement
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Query prediction without execution
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;explain&lt;/span&gt; &lt;span class="n"&gt;sql_statememnt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Explain after execution query
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;explain&lt;/span&gt; &lt;span class="k"&gt;analyze&lt;/span&gt; &lt;span class="n"&gt;sql_statement&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Indexing Technique
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It upgrades performance when querying and searching.&lt;/li&gt;
&lt;li&gt;When creating an index for multiple columns, the first column is very important. It decides the performance of a query statement. For example, if you create an index on (name, birthday), a query using the 'name' column will utilize this index, but a query using the 'birthday' column will not.&lt;/li&gt;
&lt;li&gt;Syntax for creating new index:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="n"&gt;idx_birthday&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;birthday&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The command will render the specified index inaccessible to the query optimizer, thus preventing it from being considered for query execution plans:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;alter&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt; &lt;span class="n"&gt;customers&lt;/span&gt; &lt;span class="k"&gt;alter&lt;/span&gt; &lt;span class="k"&gt;index&lt;/span&gt; &lt;span class="n"&gt;idx_birthday&lt;/span&gt; &lt;span class="n"&gt;invisible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Partition
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When the size of a database is larger than 2GB (or higher than 10 million records), you should use partitioning for that database.&lt;/li&gt;
&lt;li&gt;When using partitioning, performance will improve.&lt;/li&gt;
&lt;li&gt;The partition plan should be based on the column that is frequently used in &lt;code&gt;WHERE&lt;/code&gt; clauses.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Optimize database parameters
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Buffer Cache Hit
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It's the percentage of data that the system loads from the buffer cache instead of from the physical storage.&lt;/li&gt;
&lt;li&gt;If the percentage is lower than 90%, then you must optimize it.&lt;/li&gt;
&lt;li&gt;Depend on 2 parameters:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Innodb_buffer_pool_read_requests&lt;/code&gt;: Total request sent to Buffer Pool. Command: &lt;code&gt;show global status like '%Innodb_buffer_pool_read_requests%&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Innodb_buffer_pool_reads&lt;/code&gt;: Total requests that are not found in the buffer pool in memory must be read from disk. Command: &lt;code&gt;show global status like '%Innodb_buffer_pool_reads%&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Calculation formula:
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(Innodb_buffer_pool_read_requestss - Innodb_buffer_pool_reads) x100 / Innodb_buffer_pool_read_requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Table Cache Hit
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;MySQL will cache the table to query it fast in memory.&lt;/li&gt;
&lt;li&gt;If the percentage is lower than 80%, then you must optimize it.&lt;/li&gt;
&lt;li&gt;Depend on 2 parameters:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Open_tables&lt;/code&gt;: Total tables are opened in cache. Command: &lt;code&gt;show global status like 'Open_tables&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Opened_tables&lt;/code&gt;: Total tables are opened. Command: &lt;code&gt;show global status like 'Opened_tables&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Calculation formula: &lt;code&gt;Open_tables/Opened_tables&lt;/code&gt;
&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Table Definition Cache Hit
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When executing a query, MySQL requires information about the table, such as its name, number of columns, and number of rows. This information is referred to as the table definition.&lt;/li&gt;
&lt;li&gt;If the percentage is lower than 80%, then you must optimize it.&lt;/li&gt;
&lt;li&gt;Depend on 2 parameters:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Open_table_definitions&lt;/code&gt;: Total definition tables are in cache . Command: &lt;code&gt;show global status like 'Open_table_definitions'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Opened_tables&lt;/code&gt;: Total definition tables are requested. Command: &lt;code&gt;show global status like 'Opened_tables'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Calculation formula: &lt;code&gt;Open_table_definitions/Opened_table_definitions&lt;/code&gt;
&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  Temporary Table in memory
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Many SQL statements like &lt;code&gt;Order, Group By, ...&lt;/code&gt; must use a temporary table.&lt;/li&gt;
&lt;li&gt;If the percentage is lower than 80%, then you must optimize it.&lt;/li&gt;
&lt;li&gt;Depend on 2 parameters:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Created_tmp_disk_tables&lt;/code&gt;: Total temp tables are created on disk. Command: &lt;code&gt;show global status like '%Created_tmp_disk_tables%&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Created_tmp_tables&lt;/code&gt;: Total temp tables are created. Command: &lt;code&gt;show global status like '%Created_tmp_tables%&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Calculation formula: &lt;code&gt;(Created_tmp_tables - Created_tmp_disk_tables)/ Created_tmp_tables&lt;/code&gt;
&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This blog offers research-based knowledge about MySQL. MySQL is a relational database management system known for its low cost, extensive utilities, and high performance, making it suitable for a wide range of projects. While this blog covers many features and aspects of MySQL, there is more to explore. I will delve deeper into additional features and knowledge in future blogs. &lt;/p&gt;

&lt;p&gt;Thank you for reading, and see you in my next blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference document
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.mysql.com/doc/" rel="noopener noreferrer"&gt;MySQL Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=TslBGnENTFw" rel="noopener noreferrer"&gt;MySQL Course - Tran Quoc Huy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/MySQL" rel="noopener noreferrer"&gt;Wikipedia - MySQL&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>mysql</category>
      <category>performance</category>
      <category>database</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
