<!--[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区别是什么
- MyISAM 不支持外键, Innodb支持
- MyISAM 不支持事务,不支持外键.
- 对数据信息的存储处理方式不同.(如果存储引擎是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 架构.)=>效果,做一个项目后再说.
相关推荐
1.4 小结 第2章 MySQL架构组成 2.0 引言 2.1 MySQL物理文件组成 2.2 MySQL Server系统架构 2.3 MySQL 自带工具使用介绍 2.4 小结 第3章 MySQL存储引擎简介 3.0 引言 ...
《mysql管理之道:性能调优、高可用与监控》由资深mysql专家撰写,以最新的mysql版本为基础,以构建高性能mysql服务器为核心,从故障诊断、表设计、sql优化、性能参数调优、mydumper逻辑、xtrabackup热备份与恢复、...
1.4 小结 第2章 MySQL架构组成 2.0 引言 2.1 MySQL物理文件组成 2.2 MySQL Server系统架构 2.3 MySQL自带工具使用介绍 2.4 小结 第3章 MySQL存储引擎简介 3.0 引言 3.1 MySQL存储引擎概述 3.2 MyISAM存储...
本文作者总结梳理MySQL性能调优的15个重要变量,又不足需要补充的还望大佬指出。 1.DEFAULT_STORAGE_ENGINE 如果你已经在用MySQL 5.6或者5.7,并且你的数据表都是InnoDB,那么表示你已经设置好了。如果没有,确保把...
├─4、课程:聚合及进阶.20、综合应用实例及小结.mp4 ├─4、课程:聚合及进阶.2、Aggregate (GROUP BY) Functions(二).mp4 ├─4、课程:聚合及进阶.3、Aggregate (GROUP BY) Functions(三).mp4 ├─4、课程:...
08-MySQL增量恢复小结及核心恢复思想 09-课后自学能力作业-务必完成并分享讲解 10-内部学员技术大会分享课表说明 第九部-老男孩MySQL服务日志详细介绍及增量恢复命令实践(7节) 01-mysqlbinlog命令介绍及实战讲解 ...
mysql 的内存管理其实是比较复杂的,小结下,分为两类: 1 线程独享内存 2 全局共享内存 先说线程独享内存: 线程栈信息使用内存(thread_stack):主要用来存放每一个线程自身的标识信息,如线程id, 线程运行时...
由国内著名技术社区联合推荐的2012年IT技术力作:《高性能Linux服务器构建实战:运维监控、性能调优与集群应用》,即将上架发行,此书从Web应用、数据备份与恢复、网络存储应用、运维监控与性能优化、...14.6 本章小结
目录 一:为什么sparkSQL? 3 1.1:sparkSQL的发展历程 3 ...九:sparkSQL之调优 61 9.1:并行性 62 9.2: 高效的数据格式 62 9.3:内存的使用 63 9.4:合适的Task 64 9.5:其他的一些建议 64 十:总结 64