大量数据提交,上千,上万,批量性能非常快,mysql独有
多条提交:
INSERT INTO student (id,NAME) VALUES(4,'name1');
INSERT INTO student (id,NAME) VALUES(5,'name2');
批量提交:
INSERT INTO student (id,NAME) VALUES(4,'name1'),(5,'name2');
理由:
默认新增SQL有事务控制,导致每条都需要事务开启和事务提交;而批量处理是一次事务开启和提交。自然速度飞升
数据量小体现不出来
反例:
SELECT * FROM student WHERE age IS NOT NULL
正例:
SELECT * FROM student WHERE age>0
反例:
SELECT * FROM student WHERE id=1 OR salary=30000
正例:
SELECT * FROM student WHERE id=1
UNION ALL
SELECT * FROM student WHERE salary=30000
反例:
SELECT id,NAME FROM student WHERE NAME LIKE '%1'
SELECT id,NAME FROM student WHERE NAME LIKE '%1%'
正例:
SELECT id,NAME FROM student WHERE NAME LIKE '1%'
反例:先分组,再过滤
select job,avg(salary) from employee
group by job
having job ='president' or job = 'managent';
正例:先过滤,后分组
select job,avg(salary) from employee
where job ='president' or job = 'managent'
group by job;
三种连接如果结果相同,优先使用inner join,如果使用left join左边表尽量小
inner join 内连接,只保留两张表中完全匹配的结果集
left join会返回左表所有的行,即使在右表中没有匹配的记录
right join会返回右表所有的行,即使在左表中没有匹配的记录
理由:
如果inner join是等值连接,返回的行数比较少,所以性能相对会好一点
同理,使用了左连接,左边表数据结果尽量小,条件尽量放到左边处理,意味着返回的行数可能比较少。这是mysql优化原则,就是小表驱动大表,小的数据集驱动大的数据集,从而让性能更优