I discovered a magical parameter today: -site:xxxx.net
1. Selection of storage engine (table type)
1. Introduction to storage engine
The difference from most relational databases is that MySQL has the concept of a storage engine, which can be used for different purposes. You can choose the most appropriate storage engine according to your storage needs. The plug-in storage engine in MySQL is a major feature. Users can choose how to store, whether to index, and whether to use transactions according to the needs of the application. Hehe, you can also adapt the storage engine that is most suitable for your business according to the business environment.
Oracle sensed business opportunities, acquired MySQL, and has since then had an enterprise version (commercial support). The community version is still available for free download. Another great charm is also because of open source, the community is highly active and everyone can contribute. Next, we will introduce several commonly used storage engines. There is no distinction between good and bad storage engines. There is only one that is more suitable for the corresponding production business environment.
The storage engines supported in MySQL5.0 are FEDERATED, MRG_MYISAM, MyISAM, BLACKHOLE, CSV, MEMORY, ARCHIVE, NDB Cluster, BDB, EXAMPLE, InnoDB (the default storage engine after MySQL5.5 and MariaDB10.2), PERFORMANCE_SCHEMA (unconventional storage data engine). The following is a comparison of the storage engines supported by MySQL and MariaDB. It can be seen that MariaDB has added the Aria engine:
View storage engine
Enter show engines\G; through the character interface that comes with MySQL login, or use tools that support MySQL query, such as SQLyog, phpMyAdmin, MySQL workbench, etc. to query supported engines. Only some of them are shown here:
[test@cnwangk ~]$ mysql -uroot -p
Enter password:
mysql> show engines\G;*************************** 2. row ***************************
Engine: MRG_MYISAM
Support: YES Comment: Collection of identical MyISAM tablesTransactions: NO
XA: NO
Savepoints: NO*************************** 3. row ***************************
Engine: MyISAM
Support: YES Comment: MyISAM storage engineTransactions: NO
XA: NO
Savepoints: NO*************************** 6. row ***************************
Engine: MEMORY
Support: YES Comment: Hash based, stored in memory, useful for temporary tablesTransactions: NO
XA: NO
Savepoints: NO*************************** 8. row ***************************
Engine: InnoDB
Support: DEFAULT
Comment: Supports transactions, row-level locking, and foreign keysTransactions: YES
XA: YES
Savepoints: YES9 rows in set (0.00 sec)
Copy after login
Function description:
Engine: engine name (description);
Support: current version Whether the database supports this storage engine, YES: supported, NO: not supported; Supports transactions, row-level locking, and foreign keys, I personally translate this sentence: support transactions, row-level Locks and foreign keys;
Comment: A detailed description of the storage engine, such as whether the engine supports transactions and foreign keys;
Transactions: A description of whether the storage engine supports transactions, YES: supported, NO: not supported;
XA: whether it meets the XA specification. The XA specification is an open group specification for distributed transaction processing (DTP). YES: Support, NO: Not supported;
Savepoints: Literally means save points, whether the control of things is supported, YES: Supported, NO: Not supported.
Beep quietly, if you can read and understand some official English documents, this will help you further understand the MySQL storage engine and develop the ability to read source code or documents.
By the way, I would like to mention MariaDB, the sister of MySQL. In MariaDB, the forked version of MySQL, the new engine Aria was used before 10.2. The default storage engine used after MariaDB 10.2 is also InnoDB, which is enough to show the excellence of the InnoDB storage engine. MariaDB's API and protocol are compatible with MySQL, and some additional features have been added to support local non-blocking operations and progress reporting. This means that all connectors, libraries and applications that use MySQL will also work with MariaDB. On this basis, due to concerns about a more closed software project of Oracle MySQL, Linux distributions such as Fedora have replaced MySQL with MariaDB in the latest version, and the servers of the Wikimedia Foundation have also used MariaDB instead of MySQL.
MainSeveral storage engines that need to be understood:
MyISAM
InnoDB
MEMORY
MERGE
The following will focus on introducing several commonly used storage engines that I have recently learned from reading books, comparing the differences between each storage engine, and helping us understand how to use different storage engines. For more details, please refer to MySQL's official documentation.
CREATE TABLE GIRLS (
ID int NOT NULL,GIRE_NAME varchar(64) NOT NULL,GIRL_AGE varchar(10) NOT NULL,
CUP_SIZE varchar(2) NOT NULL,PRIMARY KEY (ID)) ENGINE=MEMORY DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-- 创建索引指定索引类型为hash。create index mem_hash USING HASH on GIRLS(ID);-- 查询索引类型,简化了一下,只展示了部分参数。mysql> SHOW TABLE STATUS LIKE 'GIRLS'\G*************************** 1. row ***************************
Name: GIRLS Engine: MEMORY
Version: 10
Row_format: Fixed1 row in set (0.00 sec)
Copy after login
虽然MEMORY容易丢失数据,但是在启动MySQL服务的时候,我们可以使用**–init-file选项,将insert into … select或者load data infile**这样的语句存放在这个指定的文件中,就可以在服务启动时从持久稳固的数据源装载表。
服务器需要提供足够的内存来维持所有在同一时间使用的MEMORY表,当不在需要MEMORY表内容之时,释放被MEMORY表使用的内存。仔细思考一下,如果内存用了不释放那将有多可怕。此时可以执行delete form 或truncate table亦或完整地删除整个表,使用drop table。这里提一点,在Oracle中也同样支持truncate,使用truncate的好处在于不用再去考虑回滚(rollback),效率更高。使用truncate需要在命令模式下使用,其它客户端工具可能不支持。
INSERT INTO study.`merge_demo01` VALUES(1,'demo01');
INSERT INTO study.`merge_demo02` VALUES(1,'demo02');
mysql [study]> select * from merge_demo;
+----+--------+
| ID | NAME |
+----+--------+
| 1 | demo01 |
| 1 | demo02 |
+----+--------+
2 rows in set (0.000 sec)
/** 创建函数 生成学号 **/DELIMITER $CREATE FUNCTION rand_number() RETURNS INTBEGIN
DECLARE i INT DEFAULT 0;
SET i= FLOOR(1+RAND()*100);
RETURN i;END $DELIMITER $
Copy after login
创建函数:用于生成姓名随机字符串
/** 创建函数 生成姓名随机字符串 **/DELIMITER $CREATE FUNCTION rand_name(n INT) RETURNS VARCHAR(255)BEGIN
DECLARE chars_str VARCHAR(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
DECLARE return_str VARCHAR(255) DEFAULT '';
DECLARE i INT DEFAULT 0;
WHILE i < n DO
SET return_str = CONCAT(return_str,SUBSTRING(chars_str,FLOOR(1+RAND()*52),1));
SET i = i+1;
END WHILE;
RETURN return_str; END $DELIMITER $
Copy after login
2、存储过程
创建存储过程,使用CREATE PROCEDURE创建:
/** 创建存储过程 **/DELIMITER $CREATE PROCEDURE insert_tolove(IN max_num INT(10))BEGIN
DECLARE i INT DEFAULT 0;
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
START TRANSACTION;
WHILE i< max_num DO
INSERT INTO test.`tolove`(ID,GIRL_NAME,GIRL_AGE,CUP_SIZE) VALUES(NULL,rand_name(5),rand_number(),NULL);
SET i = i + 1;
END WHILE;COMMIT;END $DELIMITER $
Copy after login
使用CALL调用存储过程,随机生成百万数据:
/** 调用存储过程 **/CALL insert_tolove(100*10000);
Copy after login
删除函数或者存储过程,使用DROP关键字
-- 删除函数rand_nameDROP FUNCTION rand_name;
-- 删除存储过程insert_toloveDROP PROCEDURE insert_tolove;
Copy after login
3、触发器
创建触发器使用CREATE TRIGGER,这里就引用sakila数据库实例。如果存在,使用了判断语句 IF EXISTS,然后删除DROP TRIGGER已经存在的触发器。
DELIMITER $$USE `sakila`$$DROP TRIGGER /*!50032 IF EXISTS */ `customer_create_date`$$CREATE
/*!50017 DEFINER = 'root'@'%' */
TRIGGER `customer_create_date` BEFORE INSERT ON `customer`
FOR EACH ROW SET NEW.create_date = NOW();$$DELIMITER ;
The above is the detailed content of How to understand MySQL storage engine. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn