How to troubleshoot abnormal memory increase in MySQL production database
Modify performance_schema
Because the company's production environment uses Alibaba Cloud RDS, it is relatively convenient to modify parameters. The default performance_schema is 0, and this time it is modified to 1. After modifying the parameters and submitting them, the database will be restarted. It is recommended to do this during low business peaks.
Turn on memory monitoring
Log in to the MySQL database, execute the following SQL, and turn on memory monitoring.
update performance_schema.setup_instruments set enabled = 'yes' where name like 'memory%';
Verify it after opening it.
select * from performance_schema.setup_instruments where name like 'memory%innodb%' limit 5;
**Note: **This command is to open memory statistics online, so only newly added memory objects after opening will be counted. Memory objects before opening will not be counted. It is recommended that you wait for a while after opening. Perform the next steps to identify threads with high memory usage.
Find memory consumption
Statistical event memory consumption
select event_name, SUM_NUMBER_OF_BYTES_ALLOC from performance_schema.memory_summary_global_by_event_name order by SUM_NUMBER_OF_BYTES_ALLOC desc LIMIT 10; +---------------------------------------+-------------------------------------+ | event_name | SUM_NUMBER_OF_BYTES_ALLOC | +---------------------------------------+-------------------------------------+ | memory/sql/Filesort_buffer::sort_keys | 763523904056 | | memory/memory/HP_PTRS | 118017336096 | | memory/sql/thd::main_mem_root | 114026214600 | | memory/mysys/IO_CACHE | 59723548888 | | memory/sql/QUICK_RANGE_SELECT::alloc | 14381459680 | | memory/sql/test_quick_select | 12859304736 | | memory/innodb/mem0mem | 7607681148 | | memory/sql/String::value | 1405409537 | | memory/sql/TABLE | 1117918354 | | memory/innodb/btr0sea | 984013872 | +---------------------------------------+-------------------------------------+
You can see that the event with the highest memory consumption is Filesort_buffer. According to experience, this should be related to sorting.
Statistical thread consumption of memory
select thread_id, event_name, SUM_NUMBER_OF_BYTES_ALLOC from performance_schema.memory_summary_by_thread_by_event_name order by SUM_NUMBER_OF_BYTES_ALLOC desc limit 10; +---------------------+---------------------------------------+-------------------------------------+ | thread_id | event_name | SUM_NUMBER_OF_BYTES_ALLOC | +---------------------+---------------------------------------+-------------------------------------+ | 105 | memory/memory/HP_PTRS | 69680198792 | | 183 | memory/sql/Filesort_buffer::sort_keys | 49210098808 | | 154 | memory/sql/Filesort_buffer::sort_keys | 43304339072 | | 217 | memory/sql/Filesort_buffer::sort_keys | 37752275360 | | 2773 | memory/sql/Filesort_buffer::sort_keys | 31460644712 | | 218 | memory/sql/Filesort_buffer::sort_keys | 31128994280 | | 2331 | memory/sql/Filesort_buffer::sort_keys | 28763981248 | | 106 | memory/memory/HP_PTRS | 27938197584 | | 191 | memory/sql/Filesort_buffer::sort_keys | 27701610224 | | 179 | memory/sql/Filesort_buffer::sort_keys | 25624723968 | +---------------------+---------------------------------------+-------------------------------------+
It can be seen that threads that consume a lot of memory are related to Filesort_buffer
.
Locate the specific SQL
According to the thread_id
we found earlier, search the log for the corresponding SQL. The Alibaba Cloud RDS audit log is relatively powerful. We retrieve directly based on thread_id.
We saw a large number of such SQLs in the logs, and the number of scanned rows ranged from thousands to tens of thousands. Although the time of each query is not long, usually between tens and hundreds of milliseconds, the number of concurrent requests is large.
The above is the detailed content of How to troubleshoot abnormal memory increase in MySQL production database. For more information, please follow other related articles on the PHP Chinese website!

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
