引擎 索引 日志查询 权限管理

存储引擎:
定义:引擎就是驱动各种数据库的程序,它负责处理数据库相关工作的核心部份。
同样的,数据库应用项目的操作指令,均会通过数据库引擎的处理作用到数据库上。
引擎的选择:
大尺寸的数据集趋向于InnoDB引擎,因为它支持事务处理和故障恢复。
数据库的大小决定了故障恢复的时间长短,InnoDB可以利用事务日志进行
数据恢复,这会比较快。
引擎分类:(Innodb):
默认版本包含5.5 以上
支持事务
不支持全文索引
索引和数据都在同一个文件中, .ibd 表的结构是在.frm文件中


(MyIsam):
默认版本5.5以下 5.3
不支持事务
支持全文索引
.frm 表结构 .MYD 表数据 . MYI 表索引

索引
作用:加快查询速度
缺点:5.3以下 删除与修改的速度变慢了,5.5以上 速度不是特别慢
类比:兴华字典的目录,可以将索引理解成一个特殊文件,如果没这个文件,查找顺序将是从前往后找 慢,有的话会按照特殊数据结构(二叉树)查找数据 快

索引分类:
主键索引: 加快查询 + 不能重复 + 不能为空 primary key
唯一索引: 加快查询 + 不能重复 unique(列名)
联合唯一索引: 加快查询 + 不能重复 unique(列名1,列名2)
普通索引: 加快查询 index('列名')
索引创建:
主键索引:
第一种:
create table t1(
id int auto_increment primary key,
name varchar(32) not null default '',
index ix_name ('name')
)engine=Innodb charset=utf8;
第二种:
alter table t1 change id id int auto_increment primary key;
唯一索引:
第一种:
create table t1(
id int auto_increment primary key,
name varchar(32) not null default '',
unique ix_name ('name')
)engine=Innodb charset=utf8;
第二种:
create unique index 索引名称(ix_name) on 表名(t1) (name);
create unique index 索引名称(ix_name_age) on 表名(t1) (name,age);
普通索引:
第一种:
create table t1(
id int auto_increment primary key,
name varchar(32) not null default'',
index ix_name ("name")
)engine=Innodb charset=utf8;
第二种:
create index (ix_naem) on 表名(t1)(name)
删除:
drop 索引名称(ix_name) on 表名(t1);
使用场景:根据需求在使用频繁的列上加索引;
缺点:5.3 版本 加上索引 查询 速度会很慢, 5.6 不是很慢

索引的使用:explain 工具 是查看sql语句(执行效率)是否用得上索引的工具,
给出一个报告,通过报告来判断sql的执行效率与效果
ES(elasticsearch)
规则:不建议使用like进行搜询
组合索引最左前缀
如果组合索引为(name,email)
where name and email 使用索引
where name 使用索引
where email 不适用索引
慢日志查询(show log):
日志文件:记录了执行速度特别慢的sql语句
步骤:show varibles like '%query%'
set global long_query_time=1; 设置慢查询时间
show_query_log=ON
show_query_log_file = e: mysql-5.6.44-winx64dataoldboy-slow.log
普通日志查询(genneral):
sql审计:记录sql的操作语句
步骤:show varibles like '%genneral%';
set global general_log=ON;
权限管理
创建权限
create user '用户名'@'ip地址’ identified by '密码'
eg: create user 'wyf'@'192.168.1.123' identified by '123456'c
create user 'wyf'@'192.168.1.%' identified by '123456'
create user 'wyf'@'%' identified by '123456'
删除权限
drop user '用户名’@'ip地址';
修改用户
rename user '用户名 ’@'ip地址'to '新用户名 ’@'ip地址'
修改密码
set password for '用户名 ’@'ip地址'= password("新密码")
授权:
grant 权限 on数据库.表 to '用户'@'IP地址'
grant select on db1.* to 'zekai'@'%';
grant select on *.* to 'zekai'@'%';
grant select,insert,delete on *.* to 'zekai'@'%';
记住:
flush privileges;
原文地址:https://www.cnblogs.com/wyf20190411-/p/11040701.html