18.知识补充1

0. 文章内容

1. 事务隔离级别-锁
3. 索引(索引是什么,怎么创建(σ゚∀゚)σ..自个百度)

    索引创建的时候根据字段值进行排序 => 创建这颗树Btree

    中间件表没有主键

   3.1 索引的类型
       3.1.1 根据创建分类
             3.1.1.1 主键索引(聚集索引) 主键索引自带
             3.1.1.2 二级索引(非聚集索引& 辅助索引 & 普通索引)
       3.1.2 根据使用分类
             3.1.2.1 覆盖索引 :
              sql语句中所涉及的字段都在索引中
             3.1.2.2 (二级索引+回表)
              使用普通索引在操作的时候,获取的数据表中的字段有一些不包含在索引中

      ----------------------------

       3.1.3 根据字段分类
             3.1.3.1 单索引(唯一索引)
             3.1.3.2 联合索引(复合索引)
       3.1.4 本质类型
             3.1.4.1 hash
             3.1.4.2 btree
             3.1.4.3 全文索引
   3.2 索引结构(BTree)
       3.2.1 BTree
   3.3 最左匹配原则(联合索引)
       3.3.1 规则是什么?
       3.3.2 演示
       3.3.3 覆盖索引就是任选(・ω<)☆
   3.4 关于SQL语句优化的点思路
       3.4.1 过滤性SQL
             3.4.1.1 单SQL
             3.4.1.2 join
       3.4.2 本质上是全表扫描的SQL
             3.4.2.1 单SQL
             3.4.2.2 join
   3.5 索引的疑问与注意
       3.5.1 索引的复用性(=゜ω゜)ノ
       3.5.2 索引建立限制(数量,字段数量)
4. 优化器执行流程
### 1. 锁

看mysql优化的 第四节与第五节

排他锁:锁住的这一条数据不能被别的锁而操作(读和写)
共享锁:锁住的这条数据可以被 共享锁操作(读)

都不会影响普通的查询

update,insert,delete 默认就是加上排他锁 普通查询是不加锁

select * from table_name lock in share mode;

mysql 在实际中 innodb,myisam

行锁 => 表锁 锁住的数据where 上没有索引

myisam:不重要的可以存储 表级锁 : 直接锁住整个数据
innodb:事务            行级锁 : 锁住某一条数据,但是需要注意:innodb在使用锁的时候一定对where是索引的条件查询的数据去设置锁

```sql
select * from table for update innodb => 行 -> 表级锁
锁住的数据在where上没有使用索引就会变成表级锁(主键索引和普通索引)
```
innodb : 排他锁,和共享锁
Myisam

死锁:交替锁住数据
死锁:mysql会选择锁住的数据少的释放掉

锁的释放:主动->rollback commit 被动->超时 =>mysql释放

INNODB_TRX 记录数据库的锁情况

### 2. 事务隔离级别&事务级联回滚
注意!!! 事务不会自动提交 在代码中使用了事务就需要使用异常捕获并手动回滚try{}catch{}?

mysql对于数据操作的时候mysql是会进行提交的(写操作)

```sql
start transaction
```

事务隔离级别:隔离级别越高性能最差 安全会更高 默认隔离级别是RR

关于事务的使用:数据是否需要保证 安全性

事务级联回滚 正常操作


一般多表操作才会用到事务吧,单表操作也会用到?

innodb 支持 事务 核心因素行锁

mysql对于数据的读取: => 先放到缓存中(对于sql语句的相关数据)

myisam 伪事务(表锁)



### 3. 索引


### 4. 优化器执行流程

sql
explain  -- 回表 10% 使用索引 > 没有使用索引的情况
select equipmentMD5,type,age from  details where type =1 ;  0.026s
select equipmentMD5,type,age from  details ignore index(idx_type) where type =1   0.051s

explain 20%
select equipmentMD5,type,age from  details where type =2 ; 0.055s
select equipmentMD5,type,age from  details ignore index(idx_type) where type =2  0.053s

explain 30%
select equipmentMD5,type,age from  details where type =3 ; 0.086s
select equipmentMD5,type,age from  details ignore index(idx_type) where type =3  0.098s 0.079s

explain 40%
select equipmentMD5,type,age from  details where type =4 ; 0.184s
select equipmentMD5,type,age from  details ignore index(idx_type) where type =4  0.067s

经验法则,通用

select
	(select count(*) from details where type = 1)/count(*)  as c1, -- 10%
	(select count(*) from details where type = 2)/count(*)  as c2, -- 20%
	(select count(*) from details where type = 3)/count(*)  as c3, -- 30%
	(select count(*) from details where type = 4)/count(*)  as c4  -- 40%
from details
```

#### 4.1 了解即可optimizer trace
这个东东呀可以让你知道MySQL是如何解析并优化SQL语句的

```sql
select @@optimizer_trace;
+--------------------------+
| @@optimizer_trace        |
+--------------------------+
| enabled=off,one_line=off |
+--------------------------+
1 row in set (0.00 sec)

mysql> set optimizer_trace="enabled=on";
Query OK, 0 rows affected (0.00 sec)

mysql> explain select * from details where type = 2;
+----+-------------+---------+------------+------+---------------+----------+---------+-------+--------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key      | key_len | ref   | rows   | filtered | Extra |
+----+-------------+---------+------------+------+---------------+----------+---------+-------+--------+----------+-------+
|  1 | SIMPLE      | details | NULL       | ref  | idx_type      | idx_type | 1       | const | 259674 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+----------+---------+-------+--------+----------+-------+
1 row in set, 1 warning (0.00 sec)

mysql> select * from information_schema.OPTIMIZER_TRACE \G;
*************************** 1. row ***************************
-- 分析的查询语句是什么
QUERY: explain select * from details where type = 2
-- 优化的具体过程
TRACE: {
  "steps": [
    {
      "join_preparation": {
        "select#": 1,
        "steps": [
          {
            "expanded_query": "/* select#1 */ select `details`.`id` AS `id`,`details`.`equipmentMD5` AS `equipmentMD5`,`details`.`type` AS `type`,`details`.`age` AS `age` from `details` where (`details`.`type` = 2)"
          }
        ]
      }
    },
    {
      -- optimize阶段
      "join_optimization": {
        "select#": 1,
        "steps": [
          {
            -- 处理搜索条件
            "condition_processing": {
              "condition": "WHERE",
              "original_condition": "(`details`.`type` = 2)",
              "steps": [
                { -- 原始搜索条件
                  "transformation": "equality_propagation",
                  "resulting_condition": "multiple equal(2, `details`.`type`)"
                },
                { -- 常量传递转换
                  "transformation": "constant_propagation",
                  "resulting_condition": "multiple equal(2, `details`.`type`)"
                },
                { -- 去除没用的条件
                  "transformation": "trivial_condition_removal",
                  "resulting_condition": "multiple equal(2, `details`.`type`)"
                }
              ]
            }
          },
          { -- 替换虚拟生成列
            "substitute_generated_columns": {
            }
          },
          { -- 表的依赖信息
            "table_dependencies": [
              {
                "table": "`details`",
                "row_may_be_null": false,
                "map_bit": 0,
                "depends_on_map_bits": [
                ]
              }
            ]
          },
          {
            "ref_optimizer_key_uses": [
              {
                "table": "`details`",
                "field": "type",
                "equals": "2",
                "null_rejecting": false
              }
            ]
          },
          { -- 预估不同单表访问方法的访问成本
            "rows_estimation": [
              {
                "table": "`details`",
                "range_analysis": {
                  "table_scan": { -- 全表扫描的行数以及成本
                    "rows": 1014455,
                    "cost": 208308
                  },
                  "potential_range_indexes": [
                    {
                      "index": "PRIMARY",--  主键不可用
                      "usable": false,
                      "cause": "not_applicable"
                    },
                    {
                      "index": "idx_type", -- idx_type可能被使用
                      "usable": true,
                      "key_parts": [
                        "type",
                        "id"
                      ]
                    }
                  ],
                  "setup_range_conditions": [
                  ],
                  "group_index_range": {
                    "chosen": false,
                    "cause": "not_group_by_or_distinct"
                  },
                  -- 分析各种可能使用的索引的成本
                  "analyzing_range_alternatives": {
                    "range_scan_alternatives": [
                      { -- 使用idx_type的成本分析
                        "index": "idx_type",
                        "ranges": [
                          "2 <= type <= 2"
                        ],
                        "index_dives_for_eq_ranges": true,
                        "rowid_ordered": true,
                        "using_mrr": false,
                        "index_only": false,
                        "rows": 259674,
                        "cost": 311610,
                        "chosen": false,
                        "cause": "cost"
                      }
                    ],
                    -- 分析使用索引合并的成本
                    "analyzing_roworder_intersect": {
                      "usable": false,
                      "cause": "too_few_roworder_scans"
                    }
                  }
                }
              }
            ]
          },
          {
           -- 分析各种可能的执行计划
           --(对多表查询这可能有很多种不同的方案,单表查询的方案上边已经分析过了,直接选取idx_key
            "considered_execution_plans": [
              {
                "plan_prefix": [
                ],
                "table": "`details`",
                "best_access_path": {
                  "considered_access_paths": [
                    {
                      "access_type": "ref",
                      "index": "idx_type",
                      "rows": 259674,
                      "cost": 68180,
                      "chosen": true
                    },
                    {
                      "rows_to_scan": 1014455,
                      "access_type": "scan",
                      "resulting_rows": 259674,
                      "cost": 208306,
                      "chosen": false
                    }
                  ]
                },
                "condition_filtering_pct": 100,
                "rows_for_plan": 259674,
                "cost_for_plan": 68180,
                "chosen": true
              }
            ]
          },
          { -- 尝试给查询添加一些其他的查询条件
            "attaching_conditions_to_tables": {
              "original_condition": "(`details`.`type` = 2)",
              "attached_conditions_computation": [
              ],
              "attached_conditions_summary": [
                {
                  "table": "`details`",
                  "attached": null
                }
              ]
            }
          },
          { -- 再稍稍的改进一下执行计划
            "refine_plan": [
              {
                "table": "`details`"
              }
            ]
          }
        ]
      }
    },
    { -- execute阶段
      "join_explain": {
        "select#": 1,
        "steps": [
        ]
      }
    }
  ]
}
MISSING_BYTES_BEYOND_MAX_MEM_SIZE: 0
          INSUFFICIENT_PRIVILEGES: 0
1 row in set (0.00 sec)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值