數據庫引擎InnoDB
InnoDB是事務型存儲引擎,適合對事務要求較高的場景中;但較適用于處理大量短期事務;
基于MVCC(Mutli Version Concurrency Control)支持高并發;支持四個隔離級別,默認級別為REPEATABLE-READ;間隙鎖以防止幻讀;
使用聚集索引(主鍵索引);索引和數據在一起,一個索引對應一個數據。
支持”自適應Hash索引“;
鎖粒度:行級鎖;間隙鎖;
InnoDB改表改一行鎖一行,MyISAM改一行也要鎖定整個表
事務就是把多個sql語句當一個正題來使用,要么同時都執行,要么走到半道失敗以后回滾都不執行,事務性存儲引擎表示這個事務能滿足ACID測試
事務示例,銀行卡轉賬,扣除和增加一前一后進行,操作不能完成的時候必須回滾,事務可以交叉進行,避免事務處理熟練過多時串行執行。
事務具有隔離性,在交叉執行是,隔離不能太嚴格,保證高并發執行
事務的四個特性:一致性;原子性;隔離性;持久性;
創建表
配置mariadb并啟動
[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 ------- 每表使用單獨表空間
max_connections = 20000 ---------- 最大并發連接數
rpm安裝下的數據庫文件位置,在此目錄下創建文件相當于數據庫。
[root@C7m ~]#ls /var/lib/mysql/
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock test
aria_log_control ib_logfile0 mysql performance_schema
顯示支持的存儲引擎
[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)]>
創建數據庫hidb,定義字符集為utf8,查看數據庫目錄,目錄內生成了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數據庫,創建表mytbl1,定義id為整數,名稱長度為30字符,數據庫引擎為InnoDB。查看hidb目錄,生成了以mytbl1開頭的frm和ibd后綴的數據表文件,其中以frm為后綴的文件定義數據表的表格式,以ibd為后綴的文件定義數據表的表空間。里面存放表的數據和索引,每張表使用單獨的表空間文件。
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
總結:
數據存儲:表空間;
并發:MVCC,間隙鎖,行級鎖;
索引:聚集索引、輔助索引;
性能:預讀操作、內存數據緩沖、內存索引緩存、自適應Hash索引、插入操作緩存區;
備份:支持熱備;
查詢表
查看所有表的信息和狀態狀態,\G 豎狀排列
MariaDB [hidb]> SHOW TABLE STATUS\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)
查看以my開頭的數據表狀態,支持使用通配符查詢
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)
查詢數據引擎為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)
數據庫引擎MyISAM
MyISAM:
支持全文索引(FULLTEXT index)、壓縮、空間函數(GIS);
不支持事務
鎖粒度:表級鎖
崩潰無法保證表安全恢復
特性:
加鎖和并發:表級鎖;
修復:手動或自動修復、但可能會丟失數據;
索引:非聚集索引;
延遲索引更新;
表壓縮;
適用場景:只讀或讀多寫少的場景、較小的表(以保證崩潰后恢復的時間較短);
創建數據庫HELLODB,選擇數據庫HELLODB;創建數據表tbl1,定義Id為整數,Name是長度為的字符集,數據庫引擎為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]>
查看數據庫文件生成和數據表文件生成。每個表有三個文件,存儲于數據庫目錄中。
tbl_name.frm:表格式定義;
tbl_name.MYD:數據文件;
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 ~]#
查看數據表狀態
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)
其它的存儲引擎:
CSV:將CSV文件(以逗號分隔字段的文本文件)作為MySQL表文件;
MRG_MYISAM:將多個MyISAM表合并成的虛擬表;
BLACKHOLE:類似于/dev/null,不真正存儲數據;
MEMORY:內存存儲引擎,支持hash索引,表級鎖,常用于臨時表;
FEDERATED: 用于訪問其它遠程MySQL服務器上表的存儲引擎接口;
MariaDB額外支持很多種存儲引擎:
OQGraph、SphinxSE、TokuDB、Cassandra、CONNECT、SQUENCE、...
搜索引擎:
lucene, sphinx
lucene:Solr, ElasticSearch