mysql调优之小结

<!--[if !supportLists]-->u  <!--[endif]-->mysql的优化

<!--[if !supportLists]-->1.       <!--[endif]-->数据库()设计合理

我们的表设计要符合3NF   3范式(规范的模式) , 有时我们需要适当的逆范式

<!--[if !supportLists]-->2.       <!--[endif]-->sql语句的优化(索引,常用小技巧.)

<!--[if !supportLists]-->3.       <!--[endif]-->数据的配置(缓存设大)

<!--[if !supportLists]-->4.       <!--[endif]-->适当硬件配置和操作系统 (读写分离.)

 

 

 

 

<!--[if !supportLists]-->u  <!--[endif]-->数据的3NF

 

1NF :就是具有原子性,不可分割.(只要使用的是关系性数据库,就自动符合)

2NF: 在满足1NF 的基础上,我们考虑是否满足2NF: 只要表的记录满足唯一性,也是说,你的同一张表,不可能出现完全相同的记录, 一般说我们在 表中设计一个主键即可.

3NF: 在满足2NF 的基础上,我们考虑是否满足3NF:即我们的字段信息可以通过关联的关系,派生即可.(通常我们通过外键来处理)

 

 

逆范式: 为什么需呀逆范式:

(相册的功能对应数据库的设计)

适当的逆范式.

<!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:414.75pt; height:171pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image001.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

 

不适当的逆范式

<!--[if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:414.75pt;height:207.75pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image003.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

 

<!--[if !supportLists]-->u  <!--[endif]-->sql语句的优化

面试题 sql语句有几类

ddl (数据定义语言) [create alter drop]

dml(数据操作语言)[insert delete upate ]

select

dtl(数据事务语句) [commit rollback savepoint]

dcl(数据控制语句) [grant  revoke]

 

<!--[if !supportLists]-->u  <!--[endif]-->show status命令

该命令可以显示你的mysql数据库的当前状态.我们主要关心的是 “com”开头的指令

show status like ‘Com%’  <=> show session  status like ‘Com%’  //显示当前控制台的情况

show global  status like ‘Com%’ ; //显示数据库从启动到 查询的次数

 

显示连接数据库次数

show status like  'Connections';

 

 

这里我们优化的重点是在 慢查询. (在默认情况下是10 ) mysql5.5.19

 

显示查看慢查询的情况

show variables like ‘long_query_time’

 

为了教学,我们搞一个海量表(mysql存储过程)

 

 

目的,就是看看怎样处理,在海量表中,查询的速度很快!

 

 

select * from emp where empno=123456;

 

 

需求:如何在一个项目中,找到慢查询的select , mysql数据库支持把慢查询语句,记录到日志中,程序员分析. (但是注意,默认情况下不启动.)

步骤:

<!--[if !supportLists]-->1.       <!--[endif]-->要这样启动mysql

 

进入到 mysql安装目录

 

 

2.  启动 xx>bin\mysqld.exe –slow-query-log   这点注意

 

测试 ,比如我们把

select * from emp where empno=34678

用了1.5秒,我现在优化.

 

快速体验: emp表的 empno建立索引.

alter table emp add primary key(empno);

//删除主键索引

alter table emp drop primary key

 

然后,再查速度变快.

 

 

<!--[if !supportLists]-->l  <!--[endif]-->索引的原理

 

<!--[if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" style='width:415.5pt;height:240pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image005.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

 

介绍一款非常重要工具 explain, 这个分析工具可以对 sql语句进行分析,可以预测你的sql执行的效率.

他的基本用法是:

explain sql语句\G

//根据返回的信息,我们可知,sql语句是否使用索引,从多少记录中取出,可以看到排序的方式.

<!--[if gte vml 1]><v:shape id="_x0000_i1028" type="#_x0000_t75" style='width:384.75pt;height:165.75pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image007.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

 

 

 

<!--[if !supportLists]-->l  <!--[endif]-->在什么列上添加索引比较合适

 

<!--[if !supportLists]-->     <!--[endif]-->在经常查询的列上加索引.

<!--[if !supportLists]-->     <!--[endif]-->列的数据,内容就只有少数几个值,不太适合加索引.

<!--[if !supportLists]-->     <!--[endif]-->内容频繁变化,不合适加索引

 

 

<!--[if !supportLists]-->l  <!--[endif]-->索引的种类

<!--[if !supportLists]-->     <!--[endif]-->主键索引 (把某列设为主键,则就是主键索引)

<!--[if !supportLists]-->     <!--[endif]-->唯一索引(unique) (即该列具有唯一性,同时又是索引)

<!--[if !supportLists]-->     <!--[endif]-->index (普通索引)

<!--[if !supportLists]-->     <!--[endif]-->全文索引(FULLTEXT)

select * from article where content like ‘%李连杰%’;

hello, i am a boy

<!--[if !supportLists]-->l  <!--[endif]-->你好,我是一个男孩  =>中文 sphinx

 

<!--[if !supportLists]-->     <!--[endif]-->复合索引(多列和在一起)

create index myind on 表名 (1,2);

 

<!--[if !supportLists]-->l  <!--[endif]-->如何创建索引

 

如果创建unique / 普通/fulltext 索引

1. create [unique|FULLTEXT] index 索引名 on 表名 (列名...)

2. alter table 表名 add index 索引名 (列名...)

//如果要添加主键索引

alter table 表名 add primary key (...)

 

 

删除索引

<!--[if !supportLists]-->1.       <!--[endif]-->drop index 索引名 on 表名

<!--[if !supportLists]-->2.       <!--[endif]-->alter table 表名 drop index index_name;

<!--[if !supportLists]-->3.       <!--[endif]-->alter table 表名 drop primary key

 

显示索引

       show index(es) from 表名

       show keys from 表名

       desc 表名

 

 

 

 

如何查询某表的索引

show indexes from 表名

 

<!--[if !supportLists]-->l  <!--[endif]-->使用索引的注意事项

查询要使用索引最重要的条件是查询条件中需要使用索引。

下列几种情况下有可能使用到索引:
1
,对于创建的多列索引,只要查询条件使用了最左边的列,索引一般就会被使用。
2
,对于使用like的查询,查询如果是  %aaa 不会使用到索引

       aaa% 会使用到索引。

下列的表将不使用索引:
1
,如果条件中有or,即使其中有条件带索引也不会使用。
2
,对于多列索引,不是使用的第一部分,则不会使用索引。
3
like查询是以%开头
4
,如果列类型是字符串,那一定要在条件中将数据使用引号引用起来。否则不使用索引。
5
,如果mysql估计使用全表扫描要比使用索引快,则不使用索引。

 

<!--[if !supportLists]-->l  <!--[endif]-->如何检测你的索引是否有效

 

<!--[if gte vml 1]><v:shape id="_x0000_i1029" type="#_x0000_t75" style='width:246.75pt;height:147pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image009.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

 

结论: Handler_read_key 越大越少

Handler_read_rnd_next 越小越好

 

fdisk

find

 

<!--[if !supportLists]-->l  <!--[endif]-->MyISAM Innodb区别是什么

  1. MyISAM 不支持外键, Innodb支持
  2. MyISAM 不支持事务,不支持外键.
  3. 对数据信息的存储处理方式不同.(如果存储引擎是MyISAM的,则创建一张表,对于三个文件..,如果是Innodb则只有一张文件 *.frm,数据存放到ibdata1

 

对于 MyISAM 数据库,需要定时清理

optimize table 表名

 

全表复制:insert into tt select * from tt;

 

<!--[if !supportLists]-->l  <!--[endif]-->常见的sql优化手法

<!--[if !supportLists]-->1.       <!--[endif]-->使用order by null  禁用排序

比如 select * from dept group by ename order by null

 

<!--[if !supportLists]-->2.       <!--[endif]-->在精度要求高的应用中,建议使用定点数(decimal)来存储数值,以保证结果的准确性

 

1000000.32

create table sal(t1 float(10,2));

create table sal2(t1 decimal(10,2));

 

?php ,int 如果是一个有符号数,最大值. int- 4*8=32   2 31 -1

 

 

 

<!--[if !supportLists]-->l  <!--[endif]-->表的水平划分

<!--[if gte vml 1]><v:shape id="_x0000_i1030" type="#_x0000_t75" style='width:351pt;height:201pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image011.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]-->

 

<!--[if !supportLists]-->l  <!--[endif]-->垂直分割表

 

 

如果你的数据库的存储引擎是MyISAM的,则当创建一个表,后三个文件. *.frm 记录表结构. *.myd 数据  *.myi 这个是索引.

 

mysql5.5.19的版本,他的数据库文件,默认放在 (看 my.ini文件中的配置.

 

 

 

 

<!--[if !supportLists]-->l  <!--[endif]-->读写分离

<!--[if gte vml 1]><v:shape id="_x0000_i1031" type="#_x0000_t75" style='width:415.5pt;height:201pt'> <v:imagedata src="file:///C:\Users\fingers\AppData\Local\Temp\msohtmlclip1\01\clip_image013.png" o:title=""/> </v:shape><![endif]--><!--[if !vml]--><!--[endif]--> 

 

uml 课程.(uml 架构.)=>效果,做一个项目后再说.

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值