數(shù)據(jù)庫(kù)引擎InnoDB
InnoDB是事務(wù)型存儲(chǔ)引擎,適合對(duì)事務(wù)要求較高的場(chǎng)景中;但較適用于處理大量短期事務(wù);
基于MVCC(Mutli Version Concurrency Control)支持高并發(fā);支持四個(gè)隔離級(jí)別,默認(rèn)級(jí)別為REPEATABLE-READ;間隙鎖以防止幻讀;
使用聚集索引(主鍵索引);索引和數(shù)據(jù)在一起,一個(gè)索引對(duì)應(yīng)一個(gè)數(shù)據(jù)。
支持”自適應(yīng)Hash索引“;
鎖粒度:行級(jí)鎖;間隙鎖;
InnoDB改表改一行鎖一行,MyISAM改一行也要鎖定整個(gè)表
事務(wù)就是把多個(gè)sql語(yǔ)句當(dāng)一個(gè)正題來(lái)使用,要么同時(shí)都執(zhí)行,要么走到半道失敗以后回滾都不執(zhí)行,事務(wù)性存儲(chǔ)引擎表示這個(gè)事務(wù)能滿足ACID測(cè)試
事務(wù)示例,銀行卡轉(zhuǎn)賬,扣除和增加一前一后進(jìn)行,操作不能完成的時(shí)候必須回滾,事務(wù)可以交叉進(jìn)行,避免事務(wù)處理熟練過多時(shí)串行執(zhí)行。
事務(wù)具有隔離性,在交叉執(zhí)行是,隔離不能太嚴(yán)格,保證高并發(fā)執(zhí)行
事務(wù)的四個(gè)特性:一致性;原子性;隔離性;持久性;
創(chuàng)建表
配置mariadb并啟動(dòng)
[root@C7m ~]#vim /etc/my.cnf.d/server.cnf
# this is read by the standalone daemon and embedded servers
[server]
skip_name_resolve = ON ------------ 跳過名稱解析
innodb_file_per_table = ON ------- 每表使用單獨(dú)表空間
max_connections = 20000 ---------- 最大并發(fā)連接數(shù)
rpm安裝下的數(shù)據(jù)庫(kù)文件位置,在此目錄下創(chuàng)建文件相當(dāng)于數(shù)據(jù)庫(kù)。
[root@C7m ~]#ls /var/lib/mysql/
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock test
aria_log_control ib_logfile0 mysql performance_schema
顯示支持的存儲(chǔ)引擎
[root@C7m ~]#mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SHOW ENGINES;
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment 注釋 | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
| CSV | YES | CSV storage engine | NO | NO | NO |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Percona-XtraDB, Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| FEDERATED | YES | FederatedX pluggable storage engine | YES | NO | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| Aria | YES | Crash-safe tables with MyISAM heritage | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------------------+--------------+------+------------+
10 rows in set (0.00 sec)
MariaDB [(none)]>
創(chuàng)建數(shù)據(jù)庫(kù)hidb,定義字符集為utf8,查看數(shù)據(jù)庫(kù)目錄,目錄內(nèi)生成了hidb文件夾
MariaDB [(none)]> CREATE DATABASE hidb CHARACTER SET 'utf8';
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]>
[root@C7m ~]#ls /var/lib/mysql/
aria_log.00000001 hidb ib_logfile0 mysql performance_schema
aria_log_control ibdata1 ib_logfile1 mysql.sock test
選擇hidb數(shù)據(jù)庫(kù),創(chuàng)建表mytbl1,定義id為整數(shù),名稱長(zhǎng)度為30字符,數(shù)據(jù)庫(kù)引擎為InnoDB。查看hidb目錄,生成了以mytbl1開頭的frm和ibd后綴的數(shù)據(jù)表文件,其中以frm為后綴的文件定義數(shù)據(jù)表的表格式,以ibd為后綴的文件定義數(shù)據(jù)表的表空間。里面存放表的數(shù)據(jù)和索引,每張表使用單獨(dú)的表空間文件。
MariaDB [(none)]> USE hidb;
Database changed
MariaDB [hidb]> CREATE TABLE mytbl1(id INT, name CHAR(30)) ENGINE InnoDB;
Query OK, 0 rows affected (0.01 sec)
MariaDB [hidb]>
[root@C7m ~]#ls /var/lib/mysql/hidb/
db.opt mytbl1.frm mytbl1.ibd
總結(jié):
數(shù)據(jù)存儲(chǔ):表空間;
并發(fā):MVCC,間隙鎖,行級(jí)鎖;
索引:聚集索引、輔助索引;
性能:預(yù)讀操作、內(nèi)存數(shù)據(jù)緩沖、內(nèi)存索引緩存、自適應(yīng)Hash索引、插入操作緩存區(qū);
備份:支持熱備;
查詢表
查看所有表的信息和狀態(tài)狀態(tài),\G 豎狀排列
MariaDB [hidb]> SHOW TABLE STATUS\G;
*************************** 1. row ***************************
Name: mytbl1 -------------------- 表名稱
Engine: InnoDB -------------------- 數(shù)據(jù)引擎
Version: 10 ------------------------ 版本
Row_format: Compact ------------------- 行格式為緊密
Rows: 0 ------------------------- 行
Avg_row_length: 0 ------------------------- 平均行長(zhǎng)度
Data_length: 16384 --------------------- 數(shù)據(jù)長(zhǎng)度
Max_data_length: 0 ------------------------- 最大數(shù)據(jù)長(zhǎng)度
Index_length: 0 ------------------------- 索引長(zhǎng)度
Data_free: 0 ------------------------- 數(shù)據(jù)幀
Auto_increment: NULL ---------------------- 自動(dòng)遞增
Create_time: 2017-11-14 15:08:15 ------- 創(chuàng)建時(shí)間
Update_time: NULL ---------------------- 更新時(shí)間
Check_time: NULL ---------------------- 檢驗(yàn)完整性的時(shí)間
Collation: utf8_general_ci ----------- 字符集
Checksum: NULL ---------------------- 檢驗(yàn)碼
Create_options: -------------------------- 創(chuàng)建時(shí)的選項(xiàng)
Comment: -------------------------- 表注釋
1 row in set (0.00 sec)
查看以my開頭的數(shù)據(jù)表狀態(tài),支持使用通配符查詢
MariaDB [hidb]> SHOW TABLE STATUS LIKE 'my%'\G;
*************************** 1. row ***************************
Name: mytbl1
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-11-14 15:08:15
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
查詢數(shù)據(jù)引擎為InnoDB的所有表
MariaDB [hidb]> SHOW TABLE STATUS WHERE Engine='InnoDB'\G;
*************************** 1. row ***************************
Name: mytbl1
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-11-14 15:08:15
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
數(shù)據(jù)庫(kù)引擎MyISAM
MyISAM:
支持全文索引(FULLTEXT index)、壓縮、空間函數(shù)(GIS);
不支持事務(wù)
鎖粒度:表級(jí)鎖
崩潰無(wú)法保證表安全恢復(fù)
特性:
加鎖和并發(fā):表級(jí)鎖;
修復(fù):手動(dòng)或自動(dòng)修復(fù)、但可能會(huì)丟失數(shù)據(jù);
索引:非聚集索引;
延遲索引更新;
表壓縮;
適用場(chǎng)景:只讀或讀多寫少的場(chǎng)景、較小的表(以保證崩潰后恢復(fù)的時(shí)間較短);
創(chuàng)建數(shù)據(jù)庫(kù)HELLODB,選擇數(shù)據(jù)庫(kù)HELLODB;創(chuàng)建數(shù)據(jù)表tbl1,定義Id為整數(shù),Name是長(zhǎng)度為的字符集,數(shù)據(jù)庫(kù)引擎為MyISAM。
MariaDB [hidb]> CREATE DATABASE HELLODB;
Query OK, 1 row affected (0.00 sec)
MariaDB [hidb]> USE HELLODB;
Database changed
MariaDB [HELLODB]> CREATE TABLE tbl1 (Id INT, Name CHAR(4)) ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)
MariaDB [HELLODB]>
查看數(shù)據(jù)庫(kù)文件生成和數(shù)據(jù)表文件生成。每個(gè)表有三個(gè)文件,存儲(chǔ)于數(shù)據(jù)庫(kù)目錄中。
tbl_name.frm:表格式定義;
tbl_name.MYD:數(shù)據(jù)文件;
tbl_name.MYI:索引文件;
[root@C7m ~]#ls /var/lib/mysql/
aria_log.00000001 HELLODB ibdata1 ib_logfile1 mysql.sock test
aria_log_control hidb ib_logfile0 mysql performance_schema
[root@C7m ~]#ls /var/lib/mysql/HELLODB/
db.opt tbl1.frm tbl1.MYD tbl1.MYI
[root@C7m ~]#
查看數(shù)據(jù)表狀態(tài)
MariaDB [HELLODB]> SHOW TABLE STATUS\G;
*************************** 1. row ***************************
Name: tbl1
Engine: MyISAM
Version: 10
Row_format: Fixed
Rows: 0
Avg_row_length: 0
Data_length: 0
Max_data_length: 2533274790395903
Index_length: 1024
Data_free: 0
Auto_increment: NULL
Create_time: 2017-11-14 16:47:19
Update_time: 2017-11-14 16:47:19
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.01 sec)
其它的存儲(chǔ)引擎:
CSV:將CSV文件(以逗號(hào)分隔字段的文本文件)作為MySQL表文件;
MRG_MYISAM:將多個(gè)MyISAM表合并成的虛擬表;
BLACKHOLE:類似于/dev/null,不真正存儲(chǔ)數(shù)據(jù);
MEMORY:內(nèi)存存儲(chǔ)引擎,支持hash索引,表級(jí)鎖,常用于臨時(shí)表;
FEDERATED: 用于訪問其它遠(yuǎn)程MySQL服務(wù)器上表的存儲(chǔ)引擎接口;
MariaDB額外支持很多種存儲(chǔ)引擎:
OQGraph、SphinxSE、TokuDB、Cassandra、CONNECT、SQUENCE、...
搜索引擎:
lucene, sphinx
lucene:Solr, ElasticSearch