官网地址:https://dev.mysql.com/doc/refman/8.0/en/loading-tables.html
1. mysql相关命令
//1. 登陆
msql -h localhost -u root -p
输入密码:
// 如果是本地 登陆
mysql -u root -p
输入密码:
// 2. 退出
quit;
// 3. 查看当前用户
select USER();
// 4. 查看版本和时间
select version();show now();
// 5. 查询数据库
show databases;
// 6. 选择使用的数据库
use 数据库名称;
// 7. 创建数据库myTest
create database myTest;
// 8. 显示数据库表
show tables;
// 9. 登陆时指定数据
mysql -u root -p myTest;
//10. 建表
create table t_user (user_id varchar(10) , user_name varchar(20));
// 11. 查看表结构
describe t_user;
// 12. 从文件中倒入数据到数据库表中,文件格式一定要注意
// 一行就是一条记录,字段之间用\n 风格
load data local infile '/path/user.txt' into table t_user;
load data local infile '/Users/tianzhiwei/test/user.txt' into table t_user;
// 13. 增删改查
....
// 14. 查看当前连接
show processlist;
\r:回车符,返回到这一行的开头,return的意思。
\n:换行符,到下一行的同一位置,纵坐标相同,new line的意思。
\t:制表符,为了在不使用表格的情况下,上下对齐,table的意思。
Enter 相当于\n\r,所以Enter的标识为 往下,再往前。当然,\n\r等价于\r\n。
2. 执行计划
//执行sql计划
explain sql语句;
eg: explain select * from t_user;
这是执行的sql语句的计划。我们需要看懂这个计划
2.1 explain表的列解释
Columns | JSON Name | Meaning | 中文解释 |
---|---|---|---|
id | select_id | The SELECT identifier | 查询表示符 |
select_type | None | The SELECT type | 查询类型 |
table | table_name | The table for the output row | 输出行的表 |
partitions | partitions | The matching partitions | 匹配的分区 |
type | access_type | The join type | 连接类型 |
possible_keys | possible_keys | The possible indexes to choose | 要选择的可能索引 |
key | key | The index actually chosen | 实际选择的索引 |
key_len | key_length | The length of the chosen key | 所选索引长度 |
ref | ref | The columns compared to the index | 与索引比较的列 |
rows | rows | Estimate of rows to be examined | 估计的查询结果条数 |
filtered | filtered | Percentage of rows filtered by table condition | 按表条件过滤行的百分比 |
Extra | None | Additional information | 其他 |
2.1.1 id
id是select查询的序列号,包含一组数字,表示查询中执行select子句或者操作表的顺序。
id相同,按照同上往下顺序执行
id不同,按优先级执行,谁大先执行谁。
总结:数字越大越先执行,数字相同的从上往下执行。
2.1.2 select_type
主要用来分辨查询的类型,是普通查询还是联合查询还是子查询
select_type Value | Meaning |
---|---|
SIMPLE | Simple SELECT (not using UNION or subqueries) |
PRIMARY | Outermost SELECT |
UNION | Second or later SELECT statement in a UNION |
DEPENDENT UNION | Second or later SELECT statement in a UNION, dependent on outer query |
UNION RESULT | Result of a UNION. |
SUBQUERY | First SELECT in subquery |
DEPENDENT SUBQUERY | First SELECT in subquery, dependent on outer query |
DERIVED | Derived table |
UNCACHEABLE SUBQUERY | A subquery for which the result cannot be cached and must be re-evaluated for each row of the outer query |
UNCACHEABLE UNION | The second or later select in a UNION that belongs to an uncacheable subquery (see UNCACHEABLE SUBQUERY) |
simple: 简单的查询,没有子查询没有union。eg:select * from emp;
primary:查询中若包含任何复杂的子查询,最外层查询则被标记为Primary。相当于主程序。
union:使用union关键子,unionh后边的语句
dependent unicon:依赖union结果集的。
union result : 返回union的结果集
subQuery: 子查询
dependent sunQuery:依赖子查询的
derived :派生表,查询结果作为一张新的临时表
uncacheable subQuery: 子查询的结果不被缓存
uncacheable union :union查询的结果不被缓存
2.1.3 table
表示访问的表,可能是表名称,表的别名,或者derived派生表的表明等。
2.1.4 partitions 分区
语句匹配的分区
2.1.5 type 连接类型
type显示的是访问类型,访问类型表示我是以何种方式去访问我们的数据,最容易想的是全表扫描,直接暴力的遍历一张表去寻找需要的数据,效率非常低下,访问的类型有很多,效率从最好到最坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
一般情况下,得保证查询至少达到range级别,最好能达到ref
- all: 全表扫描,非索引全表扫描
- index: 索引全表扫描。
- range: 范围查询,有范围控制的扫描。
- index_subquery: 使用普通索引关联子查询
- unque_subquery:使用唯一索引关联子查询
- index_merge:多个索引组合使用
- ref_or_null :和ref相似,如果也查询or null,就是ref_or_null
- fulltext :文本索引
- ref:使用普通索引
- eq_ref:使用唯一性索引进行数据查找
- const: 常量:select * from t_ecif_user where user_id = ‘1’; 精确查找, 主键查找
- system: 系统级别
2.1.6 其他
possible_keys: 显示可能应用在这张表中的索引,一个或多个,查询涉及到的字段上若存在索引,则该索引将被列出,但不一定被查询实际使用
key: 实际使用的索引,如果为null,则没有使用索引,查询中若使用了覆盖索引,则该索引和查询的select字段重叠
key_len: 表示索引中使用的字节数,可以通过key_len计算查询中使用的索引长度,在不损失精度的情况下长度越短越好。
ref:显示索引的哪一列被使用了,如果可能的话,是一个常数
rows:根据表的统计信息及索引使用情况,大致估算出找出所需记录需要读取的行数,此参数很重要,直接反应的sql找了多少数据,在完成目的的情况下越少越好
filtered:
2.1.7 extra
Extra:包含额外的信息。
–using filesort:说明mysql无法利用索引进行排序,只能利用排序算法进行排序,会消耗额外的位置
explain select * from t_ecif_user order by user_tel; 使用非索引排序
–using temporary:建立临时表来保存中间结果,查询完成之后把临时表删除
explain select user_tel,count(*) from t_ecif_user where user_id = 10 group by user_tel;
–using index:这个表示当前的查询时覆盖索引的,直接从索引中读取数据,而不用访问数据表。表示不需要回表了
如果同时出现using where 表名索引被用来执行索引键值的查找,如果没有,表面索引被用来读取数据,而不是真的查找
explain select deptno,count(*) from emp group by deptno limit 10;
–using where:使用where进行条件过滤
explain select * from t_ecif_user where user_id = 1;
–using join buffer:使用连接缓存,情况没有模拟出来
–impossible where:where语句的结果总是false
explain select * from t_ecif_user where user_id = 7469;