mysql架构和sql

新博客网站1

新博客网站2

一,MariaDB安装

官方yum源

vim /etc/yum.repo.d/MariaDB.repo
#以后安装软件,就去官网找安装文档
# MariaDB 10.2 CentOS repository list - created 2018-04-25 05:50 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

sudo yum install MariaDB-server MariaDB-client

二,mysql arch

mysql架构和sql_第1张图片
image.png

三,mysql数据文件类型

1.数据文件,索引文件
2.重做日志,撤销日志,二进制日志,错误日志,查询日志,慢查询日志

四,mysql 组织结构

1.建议同一种风格运行sql语句
2.查询缓存,key为sql语句的哈希值,区分大小写
3.如果sql风格不定,有时大写,有时小时,将降低缓存命中率,影响查询时间

mysql架构和sql_第2张图片
image.png

mysql架构和sql_第3张图片
image.png

五,索引管理

(1).概念

按特定数据结构存储的数据

(2).类型

1.聚集索引,非聚集索引;数据是否与索引存储在一起
2.主键索引,辅助索引
3.稠密索引,稀疏索引;是否索引了每一项
4.左前缀索引
5.覆盖索引

mysql架构和sql_第4张图片
image.png

六,管理索引的途径(不要轻易对线上数据操作索引)

#EXPLAIN 可以分享语句,是否使用索引,并不执行
MariaDB [hellodb]> EXPLAIN select * from students where StuID=3\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: students
         type: const     索引查询
possible_keys: PRIMARY
          key: PRIMARY
      key_len: 4
          ref: const
         rows: 1
        Extra: 
1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [hellodb]> EXPLAIN select * from students where Age=53\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: students
         type: ALL   所有的查询,不是索引
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 25
        Extra: Using where   使用where过滤
1 row in set (0.00 sec)
#不会用的使用help查看命令
#添加索引
MariaDB [hellodb]> alter table students add index(Age);
Query OK, 25 rows affected (0.00 sec)              
Records: 25  Duplicates: 0  Warnings: 0
#默认有主键索引
MariaDB [hellodb]> EXPLAIN select * from students where Age=53\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: students
         type: ref   没有直接标明conts说明在别的地方消耗了一些时间
possible_keys: Age
          key: Age
      key_len: 1
          ref: const
         rows: 1
        Extra: 
1 row in set (0.00 sec)
#第二种方式创建索引
MariaDB [hellodb]> create index name on students;
mysql架构和sql_第5张图片
image.png
MariaDB [hellodb]> explain select * from students where Name LIKE 'X%'\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: students
         type: range
possible_keys: name
          key: name
      key_len: 152
          ref: NULL
         rows: 6
        Extra: Using index condition
1 row in set (0.00 sec)

ERROR: No query specified

MariaDB [hellodb]> explain select * from students where Name LIKE '%X%'\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: students
         type: ALL
possible_keys: NULL
          key: NULL
      key_len: NULL
          ref: NULL
         rows: 25
        Extra: Using where
1 row in set (0.00 sec)

七,视图 view

虚表

#创建视图
MariaDB [hellodb]> create view test as select StuID,Name,Age from students;
Query OK, 0 rows affected (0.00 sec)

#show tables 可以查到,并不是实表

视图能否修改,插入数据,取决于基表的约束


mysql架构和sql_第6张图片
image.png

使用update,delete一定要带where或limit,否则清空表,更新所有的表数据

你可能感兴趣的:(mysql架构和sql)