Author:Echo Chen(陈斌)Email:chenb19870707@gmail.comBlog:Blog.csdn.net/chen19870707Date:September 9th, 2014
Explain
前段时间,游戏服务器停服的时候总是很慢,幸运的是游戏数据库都开了bin-log,于是可以通过bin-log来分析停服时执行SQL语句的数量和执行时间,下面整理了一些关键步骤。找到对应时间的bin-log文件
如果没有在/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在这里看到UserRuneScape这个表执行的次数很多。
分析bin-log文件-----执行时间分析
再看一下文本格式 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即可查出执行在1s时间的条数,此外greo的参数-b表示在取出前几行,-a表示取出后几行,我们这里找出执行慢的SQL语句。
grep -a1b6 -w exec_time=1 -r . > ~/test/result.txt
将结果保存在result.txt中,再grep UPDATE 即可得到执行慢的SQL.
cd ~/test grep -w UPDATE -r . > ~/Desktop/result.txt在稍作处理,去除每一行的文件名,即可得到SQL语句
Reference
http://www.cnblogs.com/edwardlost/archive/2011/07/13/2105598.htmlEcho Chen:Blog.csdn.net/chen19870707

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,

MySQLstringtypesincludeVARCHAR,TEXT,CHAR,ENUM,andSET.1)VARCHARisversatileforvariable-lengthstringsuptoaspecifiedlimit.2)TEXTisidealforlargetextstoragewithoutadefinedlength.3)CHARisfixed-length,suitableforconsistentdatalikecodes.4)ENUMenforcesdatainte

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,2)VARCHARforvariable-lengthtext,3)BINARYandVARBINARYforbinarydata,4)BLOBandTEXTforlargedata,and5)ENUMandSETforcontrolledinput.Eachtypehasspecificusesandperformancecharacteristics,sochoose

TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use
