Mysql performance tuning (4)

Preface

  Recent articles are all about the related content of mysql performance tuning. The previous three articles respectively introduced the related knowledge of indexes , the implementation of views and triggers in mysql, and the related knowledge of stored procedures and stored functions in mysql . The following article will first introduce the architecture of mysql, so that we can further understand the internal structure and working principle of mysql, and introduce the difference and specific use of several typical storage engines in mysql.
  First of all, I will introduce the architecture of mysql.

One, Mysql architecture

  The mysql architecture is the main part of mysql operation. We should understand its architecture to better understand the principles of mysql and have a good foundation for us when optimizing mysql. The following figure is a schematic diagram of mysql's architecture.

  From the figure, we clearly know that the entire MySQL Server is composed of the following parts, as follows:

  • Connection Pool : connection pool component
  • Management Services & Utilities : Management Services and Utilities
  • SQL Interface : SQL interface components
  • Parser : Query analyzer component
  • Optimizer : Optimizer component
  • Caches & Buffers : Cache pool component
  • Pluggable Storage Engines : storage engines
  • File System : File system

  Through the above introduction to the various parts of MySQL Server, I believe that everyone has a general understanding of its architecture. Next, I will introduce you to the specific details of each part.

  • Connection layer The
      connection layer is the uppermost layer of the architecture. It mainly provides some client and link services , including local sock communication and most TCP/IP-like communication based on client/server (C/S) tools. It mainly completes some similar connection processing , authorization authentication and related security schemes . In this layer, the concept of thread pool is introduced to provide threads for clients that pass authentication and secure access. The SSL-based secure link is also implemented on this layer. The server will also verify the operation authority it has for each client that securely accesses.

  •   The architecture of the second layer of the service layer mainly completes most of the core service functions , such as SQL interface, and completes cache query, SQL analysis and optimization, and the execution of some built-in functions. All cross-storage engine functions are also implemented at this layer, such as procedures, functions, and so on. At this layer, the server will parse the query and create the corresponding internal parse tree, and complete the corresponding optimizations, such as determining the query order of the table, whether to use the index, etc., and finally generate the corresponding execution operation. If it is a select statement, the server will also query the internal query. If the cache space is large enough, the performance of the system can be well improved in an environment that solves a large number of read operations.
  • Engine layer
      Storage engine layer, the storage engine is really responsible for the storage and extraction of data in MySQL, and the server communicates with the storage engine through the API. Different storage engines have different storage functions, so we can choose the appropriate storage engine according to our needs.
  • Storage layer The
      data storage layer mainly stores data on the file system and completes the interaction with the storage engine.
      The above are the main functions of each layer of the MySQL database architecture. Compared with other databases, Mysql has its own uniqueness. Its architecture can be applied in a variety of different scenarios and play a good role. Mainly reflected in the storage engine, the plug-in storage engine architecture separates query processing from other system tasks and data storage and extraction. This architecture can select a suitable storage engine according to business needs and actual needs. Next, I will introduce you to the related content of the storage engine.

Second, the storage engine

1. Overview of storage engine

  Unlike most databases, MySQL has a concept of a storage engine, and the optimal storage engine can be selected for different storage requirements. In fact, the storage engine is a way to store data, build indexes, update and query data and other technologies. The storage engine is table-based, not library-based . So the storage engine can also be called a table type. Databases such as Oracle and SqlServer have only one storage engine. MySQL provides a plug-in storage engine architecture. Therefore, MySQL has a variety of storage engines, and you can write storage engines based on the corresponding engines.
  The storage engines supported from MySQL 5.0 are: InnoDB, MyIAM, BDB, MEMORY, MERGE, EXAMPLE, NDB Cluster, ARCHIVE, CSV, BLACKHOLE, FEDERATED, etc. Among them, InnoDB and BDB provide transaction-safe tables, and other storage engines are non-transaction-safe. table.
  Next, we can query the storage engines supported by the current database by specifying the engine. The specific commands are as follows:

show engines;

  The storage engines supported by MySQL are as follows:

  What we need to pay attention to here is: when we create a new table, if we do not specify a storage engine, the system will use the default storage engine. Before mysql5.5, the default storage engine is MyISAM. After 5.5, the default storage engine became InnoDB. Next, I will introduce you to various storage engine features.

2. Various storage engine features

  Earlier we introduced the major storage engines in Mysql, and then we will introduce the characteristics of these storage engines. First, let us understand the differences between the major storage engines as a whole.

2.1 InnoDB

  InnoDB storage engine is the default storage engine of Mysql. The InnoDB storage engine provides transaction security with commit, rollback, and crash recovery capabilities. However, compared with the storage engine of MyISAM, InnoDB writes are less efficient and will take up more disk space to retain data and indexes.
  The InnoDB storage engine is different from other storage engines in features:

  • For transaction control,
      we first create a goods_innodb table:
create table goods_innodb(
	id int not NULL AUTO_INCREMENT,
	name varchar(20) NOT NULL,
	primary key(id)
)ENGINE=innodb DEFAULT CHARSET=utf8;

  Second, we use the goods_innodb table above for transaction control:

start transaction;
insert into goods_innodb(id, name) values(null, 'Meta40');
commit;

  The achieved effect is as follows:

  • Foreign key constraints
      MySQL only supports InnoDB as the storage engine that supports foreign keys. When creating a foreign key, the parent table must have a corresponding index. When creating a foreign key, the corresponding index will also be automatically created for the word table. We will create two tables next, where country_innodb is the parent table, city_innodb is the child table, and the country_id field is the foreign key, corresponding to the primary key country_id of the country_innodb table. The corresponding information of the two tables is as follows:
create table country_innodb(
	country_id int NOT NULL AUTO_INCREMENT,
	country_name varchar(100) NOT NULL,
	primary key(country_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

create table city_innodb(
	city_id int NOT NULL AUTO_INCREMENT,
	city_name varchar(50) not NULL,
	country_id int NOT NULL,
	primary key(city_id),
	key idx_fk_country_id(country_id),
	CONSTRAINT `fk_city_country` FOREIGN KEY(country_id) REFERENCES country_innodb(country_id) ON DELETE
	RESTRICT ON UPDATE CASCADE
)ENGINE= InnoDB DEFAULT CHARSET=utf8;

  The next creation process is similar to the above table. Therefore, in order to save space, we can directly replace it with graphics:

  Next, insert data for these two tables:

insert into country_innodb values(null, 'China'),(null,'America'),(null,'Japan');
insert into city_innodb values(null, 'xian', 1),(null,'NewYork', 2),(null,'BeiJing', 1);

  When creating an index, you can specify corresponding operations on the child table when deleting or updating the parent table, including RESTRICT, CASCADE, SET NULL, and NO ACTION.

  • RESTRICT and NO ACTION are the same, which means that the parent table cannot be updated when the child table has related records;
  • CASCADE means that when the parent table is updated or deleted, the record corresponding to the child table is updated or deleted;
  • SET NULL means that when the parent table is updated or deleted, the column corresponding to the child table is SET NULL.

  For the two tables created above, the foreign key of the sub-table is specified as ON DELETE RESTRICT ON UPDATE CASCADE, then when the main table deletes records, if the sub-table has corresponding records, the deletion is not allowed, and the main table is updating When recording, if the sub-table has a corresponding record, the sub-table is updated accordingly. We can look at the specific results by querying the data:

select * from city_innodb;
select * from country_innodb;

  The information in the specific table is as follows:

  • Storage method

  InnoDB stores tables and indexes in the following two ways:
  ① Use shared table space storage , the structure of the table created in this way is stored in the .frm file, data and indexes are stored in innodb_data_home_dir and innodb_data_file_path are defined in the table space, which can be Multiple files.
  ② Using multi-table space storage , the table structure created in this way still exists in the .frm file, but the data and indexes of each table are stored separately in .idb.

2.2 MyISAM

  Earlier we introduced InnoDB, and then we introduced MyISAM. MyISAM does not support transactions, and of course it does not support foreign keys. Its advantage is that it has fast access speed, does not require the integrity of transactions, or applications based on select and insert can basically use this engine to create tables. Among them are the following two more important features:

create table goods_myisam(
	id int NOT NULL AUTO_INCREMENT,
	name varchar(20) NOT NULL,
	primary key(id)
)Engine=MyISAM DEFAULT CHARSET=utf8;

start transaction;
insert into goods values(null, '电脑3');
rollback;

  The specific content is as follows:

  Through the above experiments, we can see that there is no transaction control in the myisam storage engine.

  • File storage method
      Each MyISAM is stored as 3 files on the disk, the file names are the same as the table names, but the extensions are:
  • .frm: storage table definition
  • .MYD: MyData, store data
  • .MYI: MyIndex, storage index

2.3 MEMORY

  The Memory storage engine stores table data in memory. Each Memory table actually corresponds to a disk file, the format is .frm, in which only the structure of the table is stored, and its data files are stored in the memory, which is conducive to the rapid processing of data and improves the efficiency of the entire table. MEMORY type table access is very large, because its data is stored in memory, and the Hash index is used by default, but once the service is closed, the data in the table will be lost.

2.4 MERGE

  The MERGE storage engine is a combination of a set of MyISAM tables. These MyISAM tables must have exactly the same structure. The MERGE table itself does not store data. You can perform query, update, and delete operations on MERGE tables. These operations are actually on the internal MyISAM Table proceed. For the insert operation of the MERGE type table, the inserted table is defined by the INSERT_METHOD clause. There can be 3 different values. The FIRST or LAST value is used, so that the insert operation is applied to the first or last table accordingly. It is not defined This clause may be defined as NO, which means that the insert operation cannot be performed on the MERGE table. We can perform a DROP operation on the MERGE table, but this operation just deletes the definition of the MERGE table and has no effect on the internal tables. The relationship between the tables is as follows:

  We use the following cases to create and use the MERGE table:
1. First, let us create three test tables payment_2006, payment_2007, payment_all, where payment_all is the MERGE table of the first two tables; because we created before The case of the table. Next, due to the length of the article, the process of creating and inserting the table is replaced by pictures. The specific code for creating the table is as follows:

2. The code for inserting data is as follows:

3. The creation and insertion of the data table are both Finished, next, we look at the data in these three tables respectively;

select * from order_1990;
select * from order_1991;
select * from order_all;

  The data in the table is shown in the figure:

3. The choice of storage engine

  When choosing a storage engine, you should choose a suitable storage engine according to the characteristics of the application system. For complex application systems, a variety of storage engines can also be selected for combination according to actual conditions. The following are the usage environments of several commonly used storage engines.

  • InnoDB : is the default storage engine of mysql, used for transaction processing applications, and supports foreign keys. If the application has relatively high requirements for transaction integrity, requires data consistency under concurrent conditions, and data operations include many update and delete operations in addition to insertion and query accidents, then the InnoDB storage engine is a more suitable choice. InnoDB storage engine not only effectively reduces locks caused by deletions and updates, but also ensures the complete submission and rollback of transactions. For systems that require high data accuracy such as billing systems or financial systems, InnoDB is the most suitable select.
  • MyIsam : If the application is based on read operations and insert operations, there are only a few update and delete operations, and the integrity and concurrency requirements of the transaction are not very high, then this storage engine is very suitable.
  • MEMORY : Store all data in RAM, and can provide access to several blocks in the environment where fast positioning records and other similar data are required. The defect of MEMORY is that there is a limit to the size of the table. Tables that are too large cannot be cached in memory. Secondly, the data to be ensured can be restored. The data in the table can be restored after the abnormal termination of the database. The MEMORY table is usually used to update small tables that are not frequently updated to quickly obtain access results.
  • MERGE : Used to logically combine a series of equivalent MyISAM tables and refer to them as an object. The advantage of the MERGE table is that it can break through the size limit of a single MyISAM table, and by distributing unreasonable tables on multiple disks, the access efficiency of the MERGE table can be effectively improved. This is very suitable for storing VLDB environments such as data warehousing.

to sum up

  Our recent article will tell you all mysql performance of the previous two articles we introduced the relevant knowledge indexes and views, and triggers and stored procedures and stored functions , this article to introduce the architecture mysql, so let us Learn more about the internal structure and working principle of mysql, and also introduce the difference and specific use of several typical storage engines in mysql. Therefore, mysql is a very important skill. Almost every job in the computer needs a mysq skill. Therefore, we need special mastery. Life is endless and struggle is endless. We work hard every day, study hard, constantly improve our abilities, and believe that we will learn something. Come on! ! !

Guess you like

Origin blog.csdn.net/Oliverfly1/article/details/109546817