【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
1、查询sql执行时间和效率
set profiling = 1;
执行下面命令查看系统字段profiling的状态是否为ON,为ON表示开启
show variables;
如图:

然后我们执行sql,查看sql的执行时间以及效率
explain select * from t_user where user_id = 1509; show profiles;
如图:

2、索引
创建索引
ALTER TABLE projectfile ADD UNIQUE INDEX (fileuploadercode); // 唯一索引,不能重复 ALTER TABLE projectfile ADD INDEX (fileuploadercode, projectid); // 普通索引,复合索引
删除索引
drop index fileuploadercode1 on projectfile; 另外一种方式 alter table projectfile drop index s2123;
查看索引
show index from 表名;
3、Sql优化策略
当只需要一条数据的时候,使用limit 1。

3、explain解析字段含义
| 列名 | 描述 |
|---|---|
| id | 在一个大的查询中每一个查询语句都对应一个id |
| select type | select关键字对应的那个查询类型 |
| table | 表名 |
| partitions(*) | 分配的分区信息 |
| type | 针对单表的访问方法,按照性能排序,如下 system > const > eq_ref > ref > ref_or_null > index_merge > unique_subquery > index_subquery > range >index > ALL |
| possible_keys | 可能用到的索引 |
| key | 实际上使用的索引 |
| key len | 实际用到的索引长度 |
| ref | 当索引列等值查询时,与索引列进行等值匹配的对象信息 |
| rows | 预估的需要读取的记录条数 |
| filtered | 某个表经过搜索条件过滤后剩余记录条数的百分比 |
| extra | 一些额外的信息 |
4、其他博客
来源:oschina
链接:https://my.oschina.net/mdxlcj/blog/3150923