码迷,mamicode.com
首页 > 其他好文 > 详细

7 事务

时间:2017-12-15 13:36:21      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:images   ati   name   一个数据库   img   upd   多个   修改   完成   

1.事务

begin
xxx操作
commit

begin
xxx操作
rollback

 

  • 当一个业务逻辑需要多个sql完成时,如果其中某条sql语句出错,则希望整个操作都退回
  • 使用事务可以完成退回的功能,保证业务逻辑的正确性
  • 事务四大特性(简称ACID)
    • 原子性(Atomicity):事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行
    • 一致性(Consistency):几个并行执行的事务,其执行结果必须与按某一顺序串行执行的结果相一致
    • 隔离性(Isolation):事务的执行不受其他事务的干扰,事务执行的中间结果对其他事务必须是透明的
    • 持久性(Durability):对于任意已提交事务,系统必须保证该事务对数据库的改变不被丢失,即使数据库出现故障
  • 要求:表的类型必须是innodb或bdb类型,才可以对此表使用事务

 

mysql> show create table areas;

) ENGINE=InnoDB AUTO_INCREMENT=991401 DEFAULT CHARSET=utf8 |

   

使用事务的情况:当数据被更改时,包括inert/update/delte

 

    技术分享图片

 

示例1

  • 步骤1:打开两个终端,连接mysql,使用同一个数据库,操作同一张表
终端1: 只是内存级别的操作
mysql> begin;

mysql> update students set name=小郭 where id=1
终端2:
mysql> select * from students;
  •  步骤2
终端1 : (临时表修改了)
mysql> select * from students;

+----+-----------+--------+---------------------+----------+
| id | name | gender | birthday | isDelete |
+----+-----------+--------+---------------------+----------+
| 1 | 小郭 | | 1999-09-09 00:00:00 | |

  • 步骤3
终端1:
mysql> commit;    #生效 
终端2:
mysql> select * from students;
+----+-----------+--------+---------------------+----------+
| id | name      | gender | birthday            | isDelete |
+----+-----------+--------+---------------------+----------+
|  1 | 小郭      |       | 1999-09-09 00:00:00 |          |

示例2

  • 步骤1:打开两个终端,连接mysql,使用同一个数据库,操作同一张表
终端1:
mysql> begin;

mysql> update students set name=阿道夫静安寺 where id=1;

 


终端2:
mysql> select * from students;
+----+-----------+--------+---------------------+----------+
| id | name      | gender | birthday            | isDelete |
+----+-----------+--------+---------------------+----------+
|  1 | 小郭      |       | 1999-09-09 00:00:00 |          |

 

  • 步骤2
终端1:  临时表修改了,实际的表没有修改
mysql> select * from students;
+----+--------------------+--------+---------------------+----------+
| id | name               | gender | birthday            | isDelete |
+----+--------------------+--------+---------------------+----------+
|  1 | 阿道夫静安寺       |       | 1999-09-09 00:00:00 |          |

 

  • 步骤3
终端1:
mysql> rollback;

mysql> select * from students;
+----+-----------+--------+---------------------+----------+
| id | name      | gender | birthday            | isDelete |
+----+-----------+--------+---------------------+----------+
|  1 | 小郭      |       | 1999-09-09 00:00:00 |          |

 

 


2.索引

创建索引

索引被创建于已有的表中,它可使对行的定位更快速更有效。可以在表格的一个或者多个列上创建索引,每个索引都会被起个名字。用户无法看到索引,它们只能被用来加速查询。

注释:更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常用于搜索的列上面创建索引。

 查看索引

mysql> show index from students;

 

 

示例:

  • 开启运行时间检测
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

 

  • 执行查询语句
mysql> select * from areas where title="北京市";
+--------+-----------+--------+
| id     | title     | pid    |
+--------+-----------+--------+
| 110000 | 北京市    |   NULL |
| 110100 | 北京市    | 110000 |
+--------+-----------+--------+

 

  • 查看执行时间
mysql> show profiles;
+----------+------------+---------------------------------------------+
| Query_ID | Duration   | Query                                       |
+----------+------------+---------------------------------------------+
|        1 | 0.00661175 | select * from areas where title="北京市"    |
+----------+------------+---------------------------------------------+

 

  • 查看areas表的索引
mysql> show index from areas;

e | Key_name | Seq_in| Null | Index_type |
--+----------+-------+------+------------+
0 | PRIMARY  |       |      | BTREE      |
1 | pid      |       | YES  | BTREE      |
--+----------+-------

 

  • 为表areas表创建索引
mysql> create index titleIndex on areas(title(20));

 

  • 再次执行查询
mysql> select * from areas where title="北京市";

 

 

  • 再次查看执行
mysql> show profiles;
+----------+------------+---------------------------------------------+
| Query_ID | Duration   | Query                                       |
+----------+------------+---------------------------------------------+
|        1 | 0.00661175 | select * from areas where title="北京市"    |
|        2 | 0.00049000 | show index from areas                       |
|        3 | 0.03270275 | create index titleIndex on areas(title(20)) |
|        4 | 0.00072175 | select * from areas where title="北京市"    |
+----------+------------+---------------------------------------------+

 

7 事务

标签:images   ati   name   一个数据库   img   upd   多个   修改   完成   

原文地址:http://www.cnblogs.com/venicid/p/8042384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!