1. 索引的概念
數(shù)據(jù)庫索引是一種提高數(shù)據(jù)庫查詢效率的數(shù)據(jù)結(jié)構(gòu)。它可以快速地定位和訪問數(shù)據(jù)庫中的數(shù)據(jù),從而大大提高數(shù)據(jù)庫查詢的速度和效率。數(shù)據(jù)庫索引可以根據(jù)不同的查詢需求構(gòu)造多個索引,以最大化提高查詢效率。
數(shù)據(jù)庫索引基于各種字段來創(chuàng)建,在查詢時可以通過索引直接找到滿足條件的數(shù)據(jù),而不需要掃描整個數(shù)據(jù)表。數(shù)據(jù)庫索引可以分為主鍵索引、唯一索引、普通索引和全文索引等類型,每種類型的索引具有不同的特點和應用場景。
2.索引的創(chuàng)建與使用
數(shù)據(jù)庫索引可以基于各種字段創(chuàng)建,但通?;诓樵冾l率高、查詢條件復雜或者需要排序的字段來創(chuàng)建索引。
常用的索引字段包括主鍵、唯一鍵、外鍵、常用的查詢字段等。
2.1 自動創(chuàng)建對應列的索引
創(chuàng)建主鍵約束(PRIMARY KEY)、唯一約束(UNIQUE)、外鍵約束(FOREIGN KEY)時,會自動創(chuàng)建對應列的索引。
-- 查看索引
show index from 表名;
mysql> create table book(id int primary key, name varchar(10), price int);
Query OK, 0 rows affected (0.02 sec)
mysql> show index from book;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> drop table book;
Query OK, 0 rows affected (0.01 sec)
mysql> create table book(id int unique, name varchar(10), price int);
Query OK, 0 rows affected (0.01 sec)
mysql> show index from book;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book | 0 | id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2.2 自己創(chuàng)建普通索引
--自己創(chuàng)建的索引
create index 索引名 on 表名(字段名);
mysql> create table book(id int, name varchar(10), price int);
Query OK, 0 rows affected (0.02 sec)
mysql> show index from book;
Empty set (0.00 sec)
mysql> create index idx_book_id on book(id);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from book;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book | 1 | idx_book_id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
2.3 刪除索引
-- 刪除
drop index 索引名 on 表名;
mysql> show index from book;
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book | 1 | idx_book_id | 1 | id | A | 0 | NULL | NULL | YES | BTREE | | |
+-------+------------+-------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)
mysql> drop index idx_book_id on book;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from book;
Empty set (0.00 sec)
2.4 索引查詢
使用索引查詢數(shù)據(jù)和正常查詢一樣,但索引保存的數(shù)據(jù)結(jié)構(gòu)主要為B+樹,及hash的方式,所以當數(shù)據(jù)非常多的時候,查詢速度將會大大增加。
mysql> insert into book values(1,'西游'),
-> (2,'三國'),
-> (3,'水滸'),
-> (4,'紅樓'),
-> (5,'0'),
-> (6,'1'),
-> (7,'2');
Query OK, 7 rows affected (0.01 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> select * from book where id = 4;
+------+------+
| id | name |
+------+------+
| 4 | 紅樓 |
+------+------+
1 row in set (0.00 sec)
3. 索引的弊端
數(shù)據(jù)庫索引可以提高查詢效率,但同時也會增加數(shù)據(jù)表的存儲空間和更新數(shù)據(jù)的時間成本。因此在設(shè)計數(shù)據(jù)庫時,需要根據(jù)實際情況合理地創(chuàng)建索引,以保證查詢效率的同時不影響數(shù)據(jù)更新和插入的速度。
如果表中的數(shù)據(jù)經(jīng)常被修改,索引也需要隨之更新,這會帶來一定的性能開銷。因此,對于不經(jīng)常使用或值域比較小的字段,不建議創(chuàng)建索引。
同時,一個表的索引可以不只一個,但也應該避免過多的索引,否則會降低數(shù)據(jù)庫的性能。
4. 事務的概念
事務就是邏輯上的一組操作,要么全部成功,要么全部失敗,如果有一個操作失敗,整個事務就會回滾,恢復到執(zhí)行前的狀態(tài)。保證了數(shù)據(jù)的一致性和可靠性。同時,事務也可以提高數(shù)據(jù)庫的并發(fā)性能,并且保證數(shù)據(jù)的唯一性和隔離性。因此,在高并發(fā)、高可用的應用場景下,使用事務是非常重要的。
5. 事務的特性
事務分為四個重要的ACID屬性,即:文章來源:http://www.zghlxwxcb.cn/news/detail-490563.html
- 原子性(Atomicity):一個事務中的所有操作,要么全部執(zhí)行成功,要么全部失敗回滾,如同原子一樣不可分割。
- 一致性(Consistency):事務執(zhí)行前后,數(shù)據(jù)的完整性和一致性必須保持一致。
- 隔離性(Isolation):事務執(zhí)行的過程中,對數(shù)據(jù)的操作相互隔離,每個事務都像在獨立的環(huán)境中執(zhí)行一樣,以避免數(shù)據(jù)操作之間的相互干擾。
- 持久性(Durability):事務一旦提交,它對數(shù)據(jù)庫中的數(shù)據(jù)狀態(tài)的改變是永久性的,即使系統(tǒng)崩潰也不會對其有影響,數(shù)據(jù)將一直保持到下一個事務修改它為止。
6. 事務的使用
- 開啟事務:start transaction;
- 執(zhí)行多條SQL語句
- 回滾或提交:rollback/commit;
rollback即是全部失敗,commit即是全部成功。文章來源地址http://www.zghlxwxcb.cn/news/detail-490563.html
start transaction;
-- 阿里巴巴賬戶減少2000
update accout set money=money-2000 where name = '阿里巴巴';
-- 四十大盜賬戶增加2000
update accout set money=money+2000 where name = '四十大盜';
commit;
到了這里,關(guān)于【MySQL】一文帶你了解數(shù)據(jù)庫索引與事務的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!