您的位置:首页 > 数据库

基础的增删改查,数据库优化,索引

2016-07-02 08:46 288 查看

mysql的特点

关系型数据库,免费使用,

插入式存储引擎,

性能高,

基础的增删改查

ddl语句,数据定义语句

1
2
3
4
5
6
7
8
9
10
11
12

create database test1;
drop database test1;
use test1;
create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2));
drop table emp;
alter table emp modify ename varchar(20);
alter table emp add column age int(3);
alter table emp drop column age;
alter table emp change age age1 int(4);
alter table emp add birth date after ename;
alter table emp modify age int(3) first;
alter table emp rename emp1;

dml语句,数据操纵语句

1
2
3
4
5
6
7
8
9
10
11
12

insert into emp(ename,hiredate,sal,deptno) values('zzx1','2000-10-11',2000,1);
insert into emp values('lisa','2004-05-09',3000,2);
insert into dept values(5,'dept5'),(6,'dept6');
update emp set sal=4000 where ename='lisa';
update emp a,dept b set a.sal=a.sal*b.deptno,b.deptname=a.ename where a.deptno=b.deptno;
delete from emp where ename='dony';
delete a,b from emp a,dept b where a.deptno=b.deptno and a.deptno=3;
select * from emp where ename='lisa';
select distinct deptno from emp;
select * from emp order by sal(desc);
select * from emp order by sal limit 5;
select * from emp order by sal limit 1,5;ss

dcl语句,数据控制语句

sql优化

尽量使用 prepareStatement(java),利用预处理功能。

在进行多条记录的增加、修改、删除时,建议使用批处理功能,批处理的次数以整
个 SQL 语句不超过相应数据库的 SQL 语句大小的限制为准。

建议每条 SQL 语句中 in 中的元素个数在 200 以下,如果个数超过时,应拆分为多
条 SQL 语句。禁止使用 xx in(‘’,’’….) or xx in(‘’,’’,’’)。 ★

禁止使用 or 超过 200,如 xx =’123’ or xx=’456’。 ★

尽量不使用外连接。

禁止使用 not in 语句,建议用 not exist。 ★

禁止使用 Union, 如果有业务需要,请拆分为两个查询。 ★

禁止在一条 SQL 语句中使用 3 层以上的嵌套查询,如果有,请考虑使用临时表或
中间结果集。

尽量避免在一条 SQL 语句中从>= 4 个表中同时取数, 对于仅是作为过滤条件关联,
但不涉及取数的表,不参与表个数计算

查询条件里任何对列的操作都将导致表扫描,所以应尽量将数据库函数、计算表达
式写在逻辑操作符右边。

在对 char 类型比较时,建议不要使用 rtrim()函数,应该在程序中将不足的长度补
齐。

用多表连接代替 EXISTS 子句。

如果有多表连接时, 应该有主从之分, 并尽量从一个表取数, 如 select a.col1, a.col2
from a join b on a.col3=b.col4 where b.col5 = ‘a’。

在使用 Like 时,建议 Like 的一边是字符串,表列在一边出现。

不允许将 where 子句的条件放到 having 中。

将更新操作放到事务的最后执行。如

一个事务需更新多个对象时,需保证更新的顺序一致以避免死锁的发生。如总是先
更新子表再更新主表,根据存货档案批量更新现存量时,对传入的存货档案 PK 进
行排序,再做更新处理等。

禁止随意使用临时表,在临时数据不超过 200 行的情况下禁止使用临时表。

禁止随意使用 distinct,避免造成不必要的排序。

索引优化

创建索引,删除索引

1
2

create index cityname on city(city(10));
drop index cityname on city;

搜索的索引列最好在where的字句或者连接子句

使用唯一索引

使用短索引,对于较长的字段,使用其前缀做索引

不要过度使用索引,索引引起额外的性能开销和维护
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: