MySQL--06
阿新 • • 發佈:2020-08-14
資料量很大時,查詢慢的問題;
資料量巨大時,索引無效
索引定義:是一個排好序的,便於快速查詢的,資料結構。
主鍵是一種特殊的索引,
index
pid : parent id ->pid
可以在任何欄位上建立索引,但不是每個欄位都適合做索引;
檢視索引
show index from 表名;
建立索引的命令
create index idx_索引名 on 表名(欄位名(索引長度))
索引型別
主鍵、唯一索引、普通索引、聯合(複合索引)、全文索引
新增唯一索引命令
alter table 表名 add unique index idx_索引名(要索引的欄位(長度))
ERROR 1062 (23000):** Duplicate** entry '湖南省永州市' for key 'idx_address'
刪除索引
drop index 索引名稱 on 表名;
MariaDB [books]> drop index idx_address on student; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 MariaDB [books]> show index from student;View Code+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | student | 0 | PRIMARY | 1 | id | A | 6 | NULL | NULL | | BTREE | | | | student | 1 | idx_name | 1 | name | A | 6 | 10 | NULL | | BTREE | | | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows inset (0.00 sec)
Key_name:索引名稱
MariaDB [books]> show index from student; +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | student | 0 | PRIMARY | 1 | id | A | 6 | NULL | NULL | | BTREE | | | | student | 1 | idx_name | 1 | name | A | 6 | 10 | NULL | | BTREE | | | | student | 1 | idx_fuhe | 1 | name | A | 6 | NULL | NULL | | BTREE | | | | student | 1 | idx_fuhe | 2 | address | A | 6 | NULL | NULL | YES | BTREE | | | +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 4 rows in set (0.00 sec)View Code
建立全文索引
MariaDB [books]> alter table student add FULLTEXT idx_full(address); Query OK, 0 rows affected, 1 warning (0.18 sec) Records: 0 Duplicates: 0 Warnings: 1View Code
MariaDB [books]> create table idx( -> id int primary key auto_increment, -> name varchar(20), -> email varchar(60), -> unique idx_email (email(20)) -> )engine=innodb default charset=utf8; Query OK, 0 rows affected (0.02 sec) MariaDB [books]> show index from idx; +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | idx | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | | | idx | 0 | idx_email | 1 | email | A | 0 | 20 | NULL | YES | BTREE | | | +-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.00 sec)View Code
事務:一系列的mysql操作,它的特性具有acid特性。
acid:原子性,一致性,隔離性,永續性。
start transaction;/begin;
commit;
提交事務的案例
-
開啟 視窗1
-
開啟視窗2
-
兩個視窗都使用同一資料庫
-
視窗一 start transaction;或者begin開啟事務
-
視窗一 執行插入或修改的操作,
-
視窗一檢視,這時可以看到操作的結果
-
視窗二檢視,這時看不到操作的結果
-
視窗一commit
-
視窗一檢視,可以看到操作結果,視窗二檢視,可以看到操作結果,完成事務操作。