MySQL优化总结
宁可集中批量操作,避免频繁读写
系统里包含了积分部分,学生和老师通过系统做了操作都可以获得积分,而且积分规则很复杂,限制每类操作获得积分不同,每人每天每类积分都有上限。比如登录,一次登录就可以获得1分,但是不管你登录多少次,一天只能累积一个登录积分。这个还是简单的,有的积分很变态,比如老师积分中有一类是看老师判作业的情况,规则是:老师判了作业,发现学生有错的,学生改过了,老师再判,如果这时候学生都对了,就给老师加分,如果学生还是错的,那就接着改,知道学生都改对了,老师都判完了,才能给老师加分。如果用程序来处理,很可能每个功能都会额外的写一堆代码来处理这个鸡肋似的积分。不仅编程的同事干活找不到重点,还平白给数据库带来了很大的压力。经过和需求人员的讨论,确定积分没有必要实时累积,于是我们采取后台脚本批量处理的方式。夜深人静的时候,让机器自己玩去吧。
这个变态的积分规则用批处理读出来是这样的:
1 select person_id, @semester_id, 301003, 0, @one_marks, assign_date, @one_marks
2 from hom_assignmentinfo ha, hom_assign_class hac
3 where ha.assignment_id = hac.assignment_id
4 and ha.assign_date between @time_begin and @time_end
5 and ha.assignment_id not in
6 (
7 select haa.assignment_id from hom_assignment_appraise haa, hom_check_assignment hca
8 where haa.appraise_id = hca.appraise_id and haa.if_submit=1
9 and (
10 (hca.recheck_state = 3004001 and hca.check_result in (3003002, 3003003) )
11 or
12 (hca.recheck_state = 3004002 and hca.recheck_result in (3003002, 3003003))
13 )
14 )
15 and ha.assignment_id not in
16 (
17 select assignment_id from hom_assignment_appraise where if_submit=0 and result_type = 0
18 )
19 and ha.assignment_id in
20 (
21 select haa.assignment_id from hom_assignment_appraise haa, hom_check_assignment hca
22 where haa.appraise_id = hca.appraise_id and haa.if_submit=1
23 and hca.check_result in (3003002, 3003003)
24 );
2 from hom_assignmentinfo ha, hom_assign_class hac
3 where ha.assignment_id = hac.assignment_id
4 and ha.assign_date between @time_begin and @time_end
5 and ha.assignment_id not in
6 (
7 select haa.assignment_id from hom_assignment_appraise haa, hom_check_assignment hca
8 where haa.appraise_id = hca.appraise_id and haa.if_submit=1
9 and (
10 (hca.recheck_state = 3004001 and hca.check_result in (3003002, 3003003) )
11 or
12 (hca.recheck_state = 3004002 and hca.recheck_result in (3003002, 3003003))
13 )
14 )
15 and ha.assignment_id not in
16 (
17 select assignment_id from hom_assignment_appraise where if_submit=0 and result_type = 0
18 )
19 and ha.assignment_id in
20 (
21 select haa.assignment_id from hom_assignment_appraise haa, hom_check_assignment hca
22 where haa.appraise_id = hca.appraise_id and haa.if_submit=1
23 and hca.check_result in (3003002, 3003003)
24 );
这还只是个中间过程,这要是用程序实时处理,即使编程人员不罢工,数据库也会歇了。
选择合适的引擎
Mysql提供了很多种引擎,我们用的最多的是myisam,innodb,memory这三类。官方手册上说道myisqm比innodb的读速度要快,大概是3倍。不过书不能尽信啊,《OreIlly.High.Performance.Mysql》这本书里提到了myisam和innodb的比较,在测试中myisam的表现还不及innodb。至于memory,哈哈,还是比较好用的。在批处理种作临时表是个不错的选择(如果内存够大)。在我的一个批处理中,速度比近乎1:10。
1
第1页:数据库设计第2页:适当建立索引/对表进行水平划分第3页:对表进行垂直划分第4页:字段类型/文件系统/外键/表的写入时机第5页:避免频繁读写/选择合适的引擎第6页:SQL语句优化工具:慢日志和mysqldumpslow.pl第7页:SQL语句优化工具:Explain(1)第8页:SQL语句优化工具:Explain(2)第9页:SQL语句优化工具:Explain(3)第10页:SQL语句优化工具:Explain(4)第11页:SQL语句优化工具:Explain(5)第12页:SQL语句优化工具:Explain(6)第13页:SQL语句优化工具:Explain(7)第14页:SQL语句优化工具:Explain(8)第15页:SQL语句优化工具:Explain(9)第16页:SQL语句优化工具:Show [full] processlist(1)第17页:SQL语句优化工具:Show [full] processlist(2)第18页:SQL语句优化工具:Show [full] processlist(3)第19页:减少不必要的表关联第20页:尽可能的限制条件第21页:过大的子查询用临时表处理效果会好的多第22页:Join Vs select 结果集作列第23页:全索引扫描第24页:Join、In、not in、exist、not exist并不是绝对的/Like第25页:数据库参数配置第26页:合理的硬件资源和操作系统/读写分离
相关文章