精简版SQL代码部分:
insert into t_user(username,age) values('hyf',24);
delete from t_user where id=1;
update t_user set age=18 where id=2;
select count(1) from t_user where status=1;
select u.username,u.nickname from t_user u
left join t_blog b on b.user_id=u.id
where u.status=1 order by u.create_time desc
limit 1,10;
explain select id from t_user where username ='a';
create index indexOftuser on t_user(username);
explain select username from t_user where username='a'
查询语句:
能具体则具体,条件从具体,等值,到范围
索引时机:
where 和 order by 的位置加索引
Dao:
四个注解标签:
@Insert(""),@Update(""),@Delete("")@Select("")
Service:业务就不说了,根据具体业务
①count=
count(1)
②select a,b,c from a limit pageNo,pageSize
Controller:
参数接收:
Post
实体:直接from表单接收
实体加requestbody注解:json格式接收
Get
@Requestparam("") Integer age :单个属性接收
Restful
前端("/restful/1")
Getmappingw("/restful/{id}")
@Pathval(“id”) Integer Id
Dao:
insert
update
select
所有的属性都通过if条件标签判断去写
传入的时候要么是entity(没有的值初始为null),要么是map
开始概念面试题:
数据库引擎个数
INNODB (IDB文件存放索引和数据)
MYISAM
两种引擎的区别:
INNODB支持事务,有序索引,有外键
索引结构:B+树
索引的种类:
聚集索引(键值作为一个节点的内容,原有的主键索引(默认创建))
非聚集索引(值为ID,产生回表,到聚集索引处再查一次索引)
如何使用索引:
o.用具体值不用*
①.最左前缀:等值查询放左边,常用的查询条件放左边
②where子句和order by 中建立索引,通过合理的使用索引减少全表扫描
③使用覆盖索引,减少回表操作(只需要回表一次就好,如果存储太多就会多临时表,要回表多次才能到主键索引,取出对应的值)
查看执行计划命令 explain
12个字段
table,extra,type,possible_keys,key,select_type
extra:可能会出现的信息
①using index 使用了索引查询
②using where 使用了where字句
③filesort 文件排序 性能极差
type:
system>const>eq_ref>ref>range>index>all
range是使用了范围查询的最优化形式
all表示全表扫描
ref和range是我们能做到的比较优化的位置
possible_keys,key:
可能用到的索引名,使用了的索引名
select_type:给出使用的查询类型,是简单呢还是用了子查询
simple简单单表查询
SQL Query执行顺序
select distinct<select_list> from join <right_table> on
<join_condition> where <where_condition> group by having limit
<limit_number>
上述SQL查询执行顺序,按标号:
(6) SELECT (6.5)DISTINCT
——————
(1) FROM
(2) JOIN
(3) WHERE
(4) GROUP BY
(5) HAVING
——————
(7) ORDER BY
(8) LIMIT
————————————————
show profile:
//打开profile
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> help profile;
//使用show profile 监控语句执行情况
mysql> show profile;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000073 |
| Executing hook on transaction | 0.000006 |
| starting | 0.000189 |
| query end | 0.000008 |
| closing tables | 0.000004 |
| freeing items | 0.000012 |
| cleaning up | 0.000012 |
+--------------------------------+----------+
7 rows in set, 1 warning (0.00 sec)
mysql> show profiles;
+----------+------------+-----------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------+
| 1 | 0.06766775 | help 'profile' |
| 2 | 0.00006225 | query 1 |
| 3 | 0.00006350 | query 1 |
| 4 | 0.00006600 | show query 1 |
| 5 | 0.00013375 | set profiling=1 |
| 6 | 0.00030250 | help 'profile' |
+----------+------------+-----------------+
6 rows in set, 1 warning (0.00 sec)
//指定上述的一条ID语句;
mysql> show profile for query 5;
+----------------+----------+
| Status | Duration |
+----------------+----------+
| starting | 0.000073 |
| Opening tables | 0.000025 |
| query end | 0.000007 |
| closing tables | 0.000005 |
| freeing items | 0.000011 |
| cleaning up | 0.000015 |
+----------------+----------+
6 rows in set, 1 warning (0.00 sec)
mysql>
事务要素
①原子性
不可分割的,同一个事务,SQL要么都执行要么都不执行
②一致性
同一个事务的SQL要么都成功要么都回滚
③持久化
成功后都生效,更新到数据库中
④隔离性
事务之间互不影响
事务隔离级别:
事务的传播行为:
方法A调用方法B
①
看看A是否存在事务
若不存在
方法B创建事务
若存在
方法B加入方法A的事务
②当前方法有事务,就按事务执行,没有就不按事务执行
③方法必须存在事务,否则抛异常
④方法不能存在事务,否则抛异常
表锁
行锁
主从复制