存储引擎 , 索引 ,慢日志查询 , explain查询优化, 权限管理

存储引擎

create table t1(
    id int auto_increment primary key,
    name varchar(32) not null default ''
)engine=Innodb charset=utf8;

分类: (****************)

Innodb : 1.(默认版本包含5.5) 2.支持事务 3.不支持全文索引 4.索引和数据都是在同一个文件中, .ibd 表的结构实在.frm文件中
    
MyIsam : 1.(默认版本5.5以下 5.3) 2.不支持事务 3.支持全文索引 4..frm: 表结构 .MYD: 表数据 .MYI: 表索引 memory :

存储引擎的选择:

1).文章,新闻等安全性要求不高的,选myisam
2). 订单,资金,账单,火车票等对安全性要求高的,选用innodb
3). 对于临时中转表,可以用memory型 ,速度最快   MySQL在5.6版本以上,默认的存储引擎就是innodb

 索引

 全文索引 (myisam):
  sphinx

 索引:

  作用: 加快查询的速度

  类比: 新华字典的目录, 可以将索引理解成一个特殊的文件, 然后如果没有这个文件的话, 查询是从前到后查找数据的,
         如果有这个文件的话, 会按照一种特殊的数据结构(二叉树)查找数据

  分类:
       主键索引: 加快查询 + 不能重复 + 不能为空  primary key
       唯一索引: 加快查询 + 不能重复   unique(列名)
           联合唯一索引: 加快查询 + 不能重复 unique(列名1,列名2)
        普通索引: 加快查询    index('列名')


    
主键索引:

第一种: create table t1( id int auto_increment primary key, name varchar(
32) not null default '' )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_name) on 表名(t1)(name);    删除:    drop 索引名称(ix_name) on 表名(t1); 应用场景: 使用频繁的列上加一个索引
索引的优缺点:
缺点:   版本5.3以下:   删除和修改的速度就变慢了    版本5.5以上:    删除和修改的速度不是特别的慢 索引的使用:
    SQL语句的规则:
- 不建议使用 like 进行搜索 - 组合索引最左前缀 如果组合索引为:(name,email) where name and email -- 使用索引 where name -- 使用索引 where email -- 不使用索引

explain 查询优化神器:

    
    查看sql语句是否用的上索引, 或者查看sql执行效率的工具
    
    给执行的SQL语句出一个报告, 通过此报告来判断sql语句的执行效率和效果

    执行计划:让mysql预估执行操作(一般正确)
all
< index < range < index_merge < ref_or_null < ref < eq_ref < system/const id,email 慢: select * from userinfo3 where name='alex' explain select * from userinfo3 where name='alex' type: ALL(全表扫描) select * from userinfo3 limit 1; 快: select * from userinfo3 where email='alex' type: const(走索引)
  参考官网 : explain-output
ES (elasticsearch ) : ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。(搜索框)

 

慢日志查询:

慢日志查询(slow log):
    
    日志文件: 记录了执行速度特别慢的SQL语句 
    
    开启的步骤:
        1. show variables like '%query%';
        
        2. set global long_query_time = 1; 设置慢查询的时间
        3.  slow_query_log = ON                                                  
        4.  slow_query_log_file  = E:programmysql-5.6.44-winx64dataoldboy-slow.log 
普通日志记录(general log): 不适合在线上使用,会把占用磁盘空间 SQL审计 (记录sql的操作语句,不会记录输出结果) show variables like
'%general%'; +------------------+------------------------------------------------+ | Variable_name | Value | +------------------+------------------------------------------------+ | general_log | ON | | general_log_file | E:programmysql-5.6.44-winx64dataoldboy.log | +------------------+------------------------------------------------+ set global general_log = ON;

tee (cmd):
    tee 指定路径(会自动创建文件) 
    记录到指定文件中,会记录输入输出结果,如下:

mysql> tee D:a.txt
Logging to file 'D:a.txt'
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| py8 |
| py9 |
| test |
+--------------------+
7 rows in set (0.01 sec)

 

权限管理 :

创建用户
    create user '用户名'@'IP地址' identified by '密码';
    creaee user 'zekai'@'192.168.1.123' identified by '123qwe';
    creaee user 'zekai'@'192.168.1.%' identified by '123qwe';
    create user 'zekai'@'%' identified by '123qwe';
    
删除用户
    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 db1.* to 'zekai'@'%'; 记住: flush privileges;

原文地址:https://www.cnblogs.com/HZLS/p/11046258.html