Author:Echo Chen(陈斌)Email:chenb19870707@gmail.comBlog:Blog.csdn.net/chen19870707Date:September 9th, 2014
如果没有在/etc/my.cnf中配置bin-log位置,MySQL的bin-log默认文件位置在/var/lib/mysql下:
cd /var/lib/mysql ll -t
找到想要查找的时间段的SQL文件,如果时间在两个个文件内,两个文件都需要。例如:这里要查找的是8月21 15:30 ~16:00,需要的文件就是mysql-bin.000006
mysqlbinlog mysql-bin.000006 > mysql-bin.000006.txt
这个需要等待一点时间,需要等待一会儿.
将文本文件压缩拷贝到本地tar jcvf binlog.tar.bz2 mysql-bin.000006.txt sz binlog.tar.bz2
先看一下文本格式 bin-log 的记录格式:
# at 7473 #110630 11:56:05 server id 1 end_log_pos 7612 Query thread_id=6 exec_time=0 error_code=0 SET TIMESTAMP=1309406165/*!*/; UPDATE ssmatch.young_league_match_7 SET status='playing' WHERE mid=699617 /*!*/;
这里有每一条SQL的执行时间,根据自己的需要,将不需要的时间段内的SQL删掉,这里最好用UltraEdit,因为文件比较大。
分析bin-log文件-----执行次数分析table_list=( Account_tbl Activity_tbl AwardMsg_tbl BBRankFightPos_tbl BloodBattleRank_tbl BloodBattle_tbl Card_tbl Checkin_tbl ClickMsg_tbl DuelRank_tbl DynamicRune_tbl EquipFragment_tbl Equipment_tbl FightingPos_tbl Friends_tbl Gemstone_tbl Ghost_tbl HeroAttribute_tbl HeroJuedi_table ItemMarket_tbl Item_tbl LadderData_tbl LadderPlayer_tbl LadderRankList_tbl Mission_tbl MysteryShop_tbl PlayerStatistics_tbl Player_tbl RuneScapeRecovery_tbl Skill_tbl SkyLadderFightingPosition_tbl TipsMsg_tbl Treasure_tbl UserRuneScape_tbl VipCard_tbl ) for i in ${table_list[@]}; do echo ${i} grep -w ${i} . -r | grep -w UPDATE | wc -l done
table_list为所有表的表名,执行以上脚本将打印所有表的UPDATE次数。
Account_tbl 0 Activity_tbl 4281 AwardMsg_tbl 0 BBRankFightPos_tbl 1527 BloodBattleRank_tbl 190 BloodBattle_tbl 4281 Card_tbl 376 Checkin_tbl 4273 ClickMsg_tbl 0 DuelRank_tbl 83 DynamicRune_tbl 4276 EquipFragment_tbl 0 Equipment_tbl 95 FightingPos_tbl 103 Friends_tbl 34 Gemstone_tbl 43 Ghost_tbl 3 HeroAttribute_tbl 4271 HeroJuedi_table 0 ItemMarket_tbl 0 Item_tbl 486 LadderData_tbl 0 LadderPlayer_tbl 3616 LadderRankList_tbl 0 Mission_tbl 4281 MysteryShop_tbl 4279 PlayerStatistics_tbl 0 Player_tbl 4282 RuneScapeRecovery_tbl 10 Skill_tbl 15 SkyLadderFightingPosition_tbl 3744 TipsMsg_tbl 0 Treasure_tbl 4274 <span style="color:#ff0000;">UserRuneScape_tbl 15519</span> VipCard_tbl 6
再看一下文本格式 bin-log 的记录格式:
# at 7473 #110630 11:56:05 server id 1 end_log_pos 7612 Query thread_id=6 exec_time=0 error_code=0 SET TIMESTAMP=1309406165/*!*/; UPDATE ssmatch.young_league_match_7 SET status='playing' WHERE mid=699617 /*!*/;
exec_time即为执行时间,执行
grep -w exec_time=1 -r . |wc -l
grep -a1b6 -w exec_time=1 -r . > ~/test/result.txt
将结果保存在result.txt中,再grep UPDATE 即可得到执行慢的SQL.
cd ~/test grep -w UPDATE -r . > ~/Desktop/result.txt
Echo Chen:Blog.csdn.net/chen19870707