zoukankan      html  css  js  c++  java
  • 7 事务

    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="北京市"    |
    +----------+------------+---------------------------------------------+
  • 相关阅读:
    算法的时间复杂度
    二叉树递归建立和遍历
    数据挖掘之分类算法---knn算法(有matlab例子)
    C链表之创建简单静态链表
    ID3决策树算法原理及C++实现(其中代码转自别人的博客)
    adobe reader安装完成之前被中断,错误代码150210解决方法
    Oracle性能诊断艺术-读书笔记
    先对结果集排序然后做update、delete操作
    索引聚簇因子相关
    直方图及low_value、high_value
  • 原文地址:https://www.cnblogs.com/venicid/p/8042384.html
Copyright © 2011-2022 走看看