当前位置:网站首页>【MySQL】索引与事务
【MySQL】索引与事务
2022-06-13 03:35:00 【敲代码的布莱恩特】
博客主页:敲代码的布莱恩特
欢迎点赞收藏留言欢迎讨论!
本文由 【敲代码的布莱恩特】 原创,首发于 CSDN
由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!
精品专栏(不定时更新)【JavaSE】 【Java数据结构】【LeetCode】
【MySQL】索引与事务
1.索引
什么是索引
索引是一种特殊的文件,包含着对数据表里所有记录的引用指针
。可以对表中的一列或多列创建索引,并指定索引的类型,各类索引有各自的数据结构实现。
索引的作用
- 数据库中的表、数据、索引之间的关系,
类似于书架上的图书、书籍内容和书籍目录的关系。
- 索引所起的作用类似书籍目录,可用于
快速定位、检索数据
。 - 索引对于
提高数据库的性能
有很大的帮助。
索引的使用场景
要考虑对数据库表的某列或某几列创建索引,需要考虑以下几点:
- 数据量较大,且经常对这些列进行条件查询。
- 该数据库表的插入操作,及对这些列的修改操作频率较低。
- 索引会占用额外的磁盘空间。
- 根据where条件查询来创建合适的索引。
- 索引也不是创建的越多越好,数据量大的时候更新索引也消耗时间
满足以上条件时,考虑对表中的这些字段创建索引,以提高查询效率。反之,如果非条件查询列,或经常做插入、修改操作,或磁盘空间不足时,不考虑创建索引
索引的简单使用
创建主键约束(PRIMARY KEY)、唯一约束(UNIQUE)、外键约束(FOREIGN KEY)时,会 自动创建对应列的索引
。
查看索引
show index from 表名;
创建索引
create index 索引名 on 表名(字段名);
删除索引
drop index 索引名 on 表名;
2.事务
什么是事务以及为什么使用事务
事务的概念
事务(具有原子性)指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部失败
。在不同的环境中,都可以有事务。对应在数据库中,就是数据库事务。
使用事务的原因
在某些场景下,如果没有事务就会出现很大的问题。例如银行的转账问题。
准备测试表:
drop table if exists accout;
create table accout(
id int primary key auto_increment,
name varchar(20) comment '账户名称',
money decimal(11,2) comment '金额'
);
insert into accout(name, money) values
('阿里巴巴', 5000),
('四十大盗', 1000);
比如说,四十大盗把从阿里巴巴的账户上偷盗了2000元
-- 阿里巴巴账户减少2000
update accout set money=money-2000 where name = '阿里巴巴';
-- 四十大盗账户增加2000
update accout set money=money+2000 where name = '四十大盗';
假如在执行以上第一句SQL时,出现网络错误,或是数据库挂掉了,阿里巴巴的账户会减少2000,但是四十大盗的账户上就没有了增加的金额。
解决方案: 使用事务来控制,保证以上两句SQL要么全部执行成功,要么全部执行失败
如何开启事务
开启事务的三部曲
- 开启事务:start transaction;
- 执行多条SQL语句
- 回滚或提交 rollback(全部失败)/commit(全部成功)。
说明:rollback即是全部失败,commit即是全部成功。
开启事务的例子
start transaction;
-- 阿里巴巴账户减少2000
update accout set money=money-2000 where name = '阿里巴巴';
-- 四十大盗账户增加2000
update accout set money=money+2000 where name = '四十大盗';
commit;
开启事务后,如果没有执行到commit,那么数据库中的数据就没有得到修改,虽然在当前数据库中显示钱已经发生了变化,但是再打开另一个数据库端口重新进入就会发现没有变化,这就是开启事务的好处,没有执行成功就不会修改数据库中的数据。
事务的四大特性
- 原子性:对于一组操作(主要为更新),要么全部成功,要么全部失败。
- 一致性:一个事务里面,多次查询到的结果都是一致的。
- 隔离性:不同事务,查询/修改的数据是互相隔离的。一个事务没有提交/回滚之前,修改数据只有自己可以看到。
- 持久性:事务提交后,数据会持久化到硬盘中。
总结
索引:
(1)对于插入、删除数据频率高的表,不适用索引
(2)对于某列修改频率高的,该列不适用索引
(3)通过某列或某几列的条件查询频率高的,可以对这些列创建索引
事务:
start transaction;
...
rollback/commit;
边栏推荐
- Druid query
- Review the first three IO streams
- Complex network analysis capability based on graph database
- C语言程序设计——从键盘任意输入一个字符串,计算其实际字符个数并打印输出,要求不能使用字符串处理函数strlen(),使用自定义子函数Mystrlen()实现计算字符个数的功能。
- Simulink code generation: simple state machine and its code
- DTCC | 2021 China map database technology conference link sharing
- MySQL group commit
- Oracle built-in functions
- 300W pieces of MySQL data were written in the test, and they were broken between 1.6 and 2W each time. Then the following problems occurred:
- MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string
猜你喜欢
Use cypher to get the tree of the specified structure
Microservice practice based on rustlang
Simulink code generation: simple state machine and its code
2000-2019 enterprise registration data of provinces, cities and counties in China (including longitude and latitude, number of registrations and other multi indicator information)
Video playback has repeatedly broken 1000w+, how to use the second dimension to create a popular model in Kwai
MySQL learning summary 6: data type, integer, floating point number, fixed-point number, text string, binary string
The latest collation of the number of years of education per capita in the country and provinces -1989-2020- includes the annual original data, calculation process and result summary
Window and various windowfunctions in Flink
Get to know druid IO real time OLAP data analysis storage system
Feign based remote service invocation
随机推荐
How to Draw Useful Technical Architecture Diagrams
19 MySQL database optimization methods
Prefecture level city - air flow coefficient data - updated to 2019 (including 10m wind speed, boundary height, etc.)
Brief introduction: distributed cap theory and base theory
China Civil Aviation Statistical Yearbook (1996-2020)
Patrick Pichette, partner of inovia, former chief financial officer of Google and current chairman of twitter, joined the board of directors of neo4j
年金险产品保险期满之后能领多少钱?
测试写入mysql数据300W条,每次1.6-2W之间就断掉然后出现以下问题:
Figure data * reconstruction subgraph
Onnx+tensorrt+yolov5: yolov5 deployment based on trt+onnx 1
Technical documentbookmark
Getting started with Oracle
Panel data set of rural cities and towns: per capita consumption and expenditure of prefecture level cities 2012-2019 & rural data of provinces 2013-2019
Complete set of Stata code commands: follow and verify do files, common Stata commands, code collection, panel entropy method
Masa auth - overall design from the user's perspective
LVS four - tier Load Balancing Cluster (5) LVS Overview
Window and various windowfunctions in Flink
Review the first three IO streams
MySQL learning summary 9: non empty constraints, uniqueness constraints, primary key, auto_ Increment, foreign key, default, etc
Coal industry database - coal price, consumption, power generation & Provincial Civil and industrial power consumption data