mysql crud基础知识

MyISAM

sql语句是串行的

InnoDB

单表超过两千万数据会非常影响效率。

TokuDB

1、批量插入数据,因为一条数据有问题,全部是数据写入失败

INSERT INTO t_dept(deptno,dname,loc) values (40,'软件部门',北京),(30,'软件部门',北京),(30,'软件部门',北京);

加入IGNORE 关键字

INSERT IGNORE  INTO t_dept(deptno,dname,loc) values (40,'软件部门',北京),(30,'软件部门',北京),(30,'软件部门',北京);

2、不存在插入,存在更新

INSERT INTO t_temp_ip (id,demo,ip) values (5,8004,"192.168.0.1"),
(6,8004,"192.168.0.6"),(7,8004,"192.168.0.3") on DUPLICATE key update ip = values(ip);

3、mysql是默认关闭缓存的,所以每个子查询都是相关子查询,相关子查询就是要循环多次执行的子查询。

select empno,ename from t_emp where sal > (select sal from t_emp where empno =74999) and empno != 74999。

上面子查询表里面有十万条数据子查询就要执行十万次。

mybatis开启了一级缓存,可以使用子查询,控制台不建议使用子查询。

如何替代子查询?

采用from子查询替代where子查询,from 子句只执行一次,只确定数据来源不是数据条件。

select empno,ename from t_emp e join (select sal from t_emp where empno ==74999) t on e.sal > t.saland empno != 74999

4、内连接,查询条件写在on里面和where里面效果一样。

select e.empno,d.dname from t_emp e join t_dept d on t.deptno = e.deptno and d.depton =10

select e.empno,d.dname from t_emp e join t_dept d on t.deptno = e.deptno where d.depton =10

5、外连接,查询条件写在on里面和where里面效果不一样。

select e.empno,d.dname from t_emp e left join t_dept d on t.deptno = e.deptno and d.depton =10

select e.empno,d.dname from t_emp e eft join t_dept d on t.deptno = e.deptno where d.depton =10

6、表连接修改

update t_emp set sal=10000 where deptno = (select deptno from t_dept where dname = 'sales')

update t_emp e join t_dept d on e.deptno = d.deptno
and d.dname = 'sales' set e.sal=10000,d.dname='销售部';

7、表连接删除

DELETE e,d from t_emp e join t_dept d on e.deptno
= d.deptno and d.dname = '销售部';

猜你喜欢

转载自www.cnblogs.com/xiaofeiyang/p/12446724.html