存储引擎(处理表的处理器)
1、基本操作
1、基本操作
1、查看所有存储引擎
mysql> show engines;
2、查看已有表的存储引擎
mysql> show create table 表名;
3、创建表指定
create table 表名(...) engine = myisam;
4、已有表(表已建立不建议再更改!!)
alter table 表名 engine = innodb;
2、锁
1、目的:解决客户端并发访问的冲突问题
2、锁分类
1、锁类型
1、读锁(共享锁)
select:加读锁之后别人不可更改表记录,但可
以进行查询
2、写锁(互斥锁、排他锁)
insert、delete、update
加写锁之后别人不能查、不能改
2、锁粒度
1、表级锁:myisam
2、行级锁:innodb
3、常用存储引擎特点
1、InnoDB特点
1、共享表空间(/var/lib/mysql/库名)
表名.frm:表结构和索引文件
表名.ibd:表记录
2、支持行级锁
3、支持外键、事务操作
2、MyISAM特点
1、独享表空间
表名.frm:表结构
表名.myd:表记录 (mydata)
表名.myi:索引文件(myindex)
2、支持表级锁
4、如何决定使用哪个存储引擎
1、执行查询操作多的表用MyISAM(使用InnoDB浪费资源)
2、执行写操作多的表用InnoDB
MySQL简单调优
1、选择合适的存储引擎
1、读操作多:MyISAM
2、写操作多:InnoDB
2、创建索引
在select、where、order by常涉及到的字段建立索引
3、SQL语句的优化
1、where子句中尽量不使用 !=,否则放弃索引全表扫描
2、尽量避免NULL值判断,否则放弃索引全表扫描
优化前:
select number from t1 where number is null;
优化后:
在number列上设置默认值为0,确保number列无NULL值
select number from t1 where number = 0;
3、尽量避免or连接条件,否则放弃索引全表扫描
优化前:
select id rom t1 where id = 10 or id = 20;
优化后:
select id from t1 where id = 10
union all #把多SQL查询命令结果拼接在一起
select id from t1 where id = 20
union all
select id from t1 where id = 30;
4、模糊查询尽量避免使用前置 %,否则全表扫描
优化前:
select name from t1 where name like "%c%";
5、尽量避免使用 in 和 not in,否则全表扫描
连续值可优化:
select id from t1 where id in (1,2,3,4);
select id from t1 where id between 1 and 4;
6、尽量避免使用 *,用具体的字段代替 * ,不要返回用不到的
任何字段