Slow SQL monitoring system and method based on JAVA agent and bytecode enhancement
By intercepting the PrepareStatement of Mysql Jdbc when the JVM is loading and using bytecode enhancement technology, combined with JAVA Agent technology and expert experience rules, real-time and efficient monitoring of slow SQL is achieved, solving the monitoring lag and inefficiency in existing technologies. problems, improving monitoring efficiency and accuracy.
Patent Information
- Application Number
- CN202111271312.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-29
- Publication Date
- 2024-10-01
- Estimated Expiration
- 2041-10-29
AI Technical Summary
The existing technology has lag in monitoring slow SQL in the production environment, resulting in reduced efficiency and risk of management failure, and lacks real-time and efficient monitoring methods.
Adopt a slow SQL monitoring system based on JAVA agent and bytecode enhancement. By intercepting the PrepareStatement of Mysql Jdbc when the JVM is loaded, use ASM technology to enhance the bytecode of the executeQuery() method, add the explain keyword to obtain the execution plan, and Matches expert experience rules, interrupts the execution of slow SQL, and uses JAVA Agent technology and internal communication systems to provide timely notifications.
Real-time and efficient monitoring of slow SQL is realized. The systematic SQL review process avoids the intrusion of application systems, improves monitoring efficiency, and finds and solves slow SQL problems in a timely manner in the development and testing environment.
Smart Images

Figure CN113986736B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database technology, and in particular to a slow SQL monitoring system and method based on JAVA agent and bytecode enhancement. Background Art
[0002] Slow SQL (i.e., slow sql) and inefficient SQL are common problems in database application systems. In order to avoid the negative impact of slow SQL, the efficiency of SQL is usually evaluated and monitored at the application system level and the database level. At the database level, the slow SQL monitoring function provided by the database can effectively obtain information about slow SQL, but it requires the application system to run in a production environment and has a certain lag; at the application level, in order to eliminate the impact of slow SQL, it is usually necessary to organize SQL code reviews or review the SQL workflow. Generally speaking, the increase in process links will inevitably lead to a decrease in overall efficiency and the risk of management failure caused by inadequate process execution. Therefore, a real-time and efficient method and system for monitoring slow SQL is urgently needed. Summary of the invention
[0003] The present invention intends to provide a slow SQL monitoring system and method based on JAVA agent and bytecode enhancement.
[0004] The slow SQL monitoring method based on JAVA agent and bytecode enhancement includes the following:
[0005] Intercept PrepareStatment in Mysql Jdbc when JVM loads the application system class;
[0006] After intercepting PrepareStatement.class, perform bytecode enhancement on the executeQuery() method in PreparesStatement.class. The bytecode enhancement method is as follows:
[0007] Get the connection instance of the current database; get the code
[0008] Rewrite the SQL and add the explain keyword to the original SQL statement;
[0009] Execute the modified SQL and obtain the execution plan returned by MySQL;
[0010] Analyze the SQL execution plan;
[0011] Match the analysis results of the SQL execution plan with expert experience rules;
[0012] The execution of SQL statements whose analysis results match the expert experience rules is interrupted, and notification information is sent through the internal instant messaging system.
[0013] The beneficial effect of the present invention is that: in the present invention, the connection instance of the current database is obtained, and its purpose is to obtain the execution code of the current database, so that after the original SQL statement is modified, the database can still be read through the obtained code.
[0014] The explain keyword is added to the original SQL statement in order to obtain the execution plan of the current database. The explain keyword is a function provided by the existing database.
[0015] The present invention uses JAVA's Agent technology, JAVA's bytecode enhancement technology and MySQL's execution plan analysis function to integrate and match the analysis results of MySQL's execution plan with the expert experience rules in the expert experience system of SQL review at the application layer, and interrupts the execution of SQL whose analysis results match the expert experience rules, that is, using the execution interruption and notification functions, slow SQL is discovered in the development and testing environment in a timely manner.
[0016] Furthermore, in analyzing the SQL execution plan, rule matching is also included for SQL of full table scan, large table query, and file sort.
[0017] If the matched SQL execution plan contains SQLs for full table scan, large table query, and file sort, the execution of such SQLs is considered slow and needs to be interrupted.
[0018] Furthermore, it also includes continuing to execute the SQL whose analysis results do not match the expert experience rules according to the original logic, indicating that there is no problem of slow SQL, so there is no need to interrupt the execution, and the SQL can continue to be executed according to the original logic.
[0019] Furthermore, the method further includes packaging the bytecode enhancement function into a sqlagent.jar package, and adding sqlagent.jar to the startup parameters of the corresponding system.
[0020] A slow SQL monitoring system based on JAVA agent and bytecode enhancement includes an interception module, which intercepts PrepareStatment in Mysql Jdbc when JVM loads the application system class;
[0021] The bytecode enhancement module performs bytecode enhancement on the executeQuery() method in PreparesStatement.class based on ASM technology after intercepting PrepareStatement.class.
[0022] Further, the bytecode enhancement module includes a connection instance acquisition submodule for acquiring a connection instance of the current database;
[0023] SQL rewriting submodule, used to add explain keyword to the original SQL statement;
[0024] The execution plan acquisition submodule is used to execute the modified SQL and obtain the execution plan returned by MySQL;
[0025] Analysis submodule, used to analyze SQL execution plan;
[0026] The matching submodule is used to match the analysis results of the SQL execution plan with the expert experience rules. If the expert experience rules are matched, the execution of the SQL that hits the expert experience rules is interrupted;
[0027] The notification submodule is used to send notification information through the internal instant messaging system after the SQL that hits the expert experience rule is interrupted.
[0028] Furthermore, the analysis submodule is also used to perform rule matching on SQL for full table scanning, large table query, and file sorting.
[0029] The present invention makes full use of JAVA agent technology and bytecode enhancement technology, so that the functions of the present invention do not have any intrusion on the application system, including dependency intrusion and code intrusion of the application system Jar. At the same time, the execution plan analysis of MySQL and expert experience rules are matched and integrated, the SQLReview process of the application layer is systematized, and the execution interruption and notification functions are used to discover and solve slow SQL in the test and development stage. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] Figure 1 It is a schematic block diagram of an embodiment of a slow SQL monitoring system based on JAVA agent and bytecode enhancement of the present invention. DETAILED DESCRIPTION
[0031] The following is further described in detail through specific implementation methods:
[0032] The slow SQL monitoring method based on JAVA agent and bytecode enhancement includes the following:
[0033] Intercept PrepareStatment in Mysql Jdbc when JVM (JAVA virtual machine) loads the application system class;
[0034] After intercepting PrepareStatement.class, perform bytecode enhancement on the executeQuery() method in PreparesStatement.class.
[0035] This embodiment implements bytecode enhancement of the executeQuery() method in PreparesStatement.class based on ASM technology. ASM is a Java bytecode manipulation framework. It can be used to dynamically generate classes or enhance the functions of existing classes. ASM can directly generate binary class files, and can also dynamically change class behaviors before the class is loaded into the Java virtual machine.
[0036] In this embodiment, Jdbc refers to Java Database Connectivity (Jdbc for short), which is an application program interface in the Java language used to regulate how client programs access databases, and provides methods such as querying and updating data in the database. Simply put, it is a type of Java API used to execute SQL statements. Through Jdbc, we can directly use Java programming to operate relational databases. Through encapsulation, developers can use pure Java API to complete SQL execution.
[0037] PrepareStatement is precompiled, which can greatly improve the efficiency of batch processing. Use PrepareStatement to precompile SQL, without re-parsing and compiling SQL for each statement.
[0038] The executeQuery() method is used to generate a single result set (ResultSet) statement, such as a SELECT statement. It is the most commonly used method for executing SQL statements. This method is used to execute a SELECT statement, execute a query statement, and return a ResultSet object representing the query results after execution.
[0039] There are two stages in executing Java: compile time and run time. The compile time is to compile Java type files into Java bytecode files, that is, class files; the run time is to load the bytecode files into the JVM memory and finally form a type that can be executed by the virtual machine. The bytecode enhancement technology is to enhance the code performance and reduce the redundant code operations by modifying the bytecode files at run time.
[0040] The bytecode enhancement method is as follows:
[0041] Get the connection instance of the current database;
[0042] Rewrite the SQL and add the explain keyword to the original SQL statement;
[0043] Execute the modified SQL and obtain the execution plan returned by MySQL;
[0044] Analyze the SQL execution plan; in this embodiment, the analysis of the SQL execution plan also includes rule matching of SQL for full table scanning, large table query, and file sorting.
[0045] Full table scan: When querying a database table, you need to scan every row of data in a table;
[0046] Large table query: The query needs to scan a large number of rows of data. The more rows are scanned, the worse the query performance.
[0047] File sorting: A data sorting implementation method that introduces files to store temporary sorting results, which can sort data sets that exceed the memory capacity. The disadvantage is that the performance is poor, so it should be avoided as much as possible in actual production.
[0048] The analysis results of the SQL execution plan are matched with expert experience rules; the expert experience rules described in this embodiment are such as: table query with more than 100,000 rows in full table scan, file sorting, etc.
[0049] The execution of SQL statements whose analysis results match the expert experience rules is interrupted, and a notification message is sent through the internal instant messaging system; the execution of SQL statements whose analysis results do not match the expert experience rules continues according to the original logic.
[0050] The method disclosed in this embodiment also includes packaging the bytecode enhancement function into a sqlagent.jar package, and adding sqlagent.jar to the startup parameters of the corresponding system. The bytecode enhancement extension method used in this solution is JVMAgent technology, and its extension specification requires that the agent function be built into a JAVA jar package, and the name of the function jar package to be added is specified in the startup parameters of the extended application.
[0051] As attached Figure 1 As shown, this embodiment also discloses a slow SQL monitoring system based on JAVA agent and bytecode enhancement, including an interception module, which intercepts PrepareStatment in Mysql Jdbc when JVM loads the application system class;
[0052] The bytecode enhancement module performs bytecode enhancement on the executeQuery() method in PreparesStatement.class based on ASM technology after intercepting PrepareStatement.class.
[0053] A slow SQL monitoring system based on JAVA agent and bytecode enhancement, wherein the bytecode enhancement module includes a connection instance acquisition submodule for acquiring a connection instance of the current database;
[0054] SQL rewriting submodule, used to add explain keyword to the original SQL statement;
[0055] The execution plan acquisition submodule is used to execute the modified SQL and obtain the execution plan returned by MySQL;
[0056] The analysis submodule is used to analyze the SQL execution plan and to match the rules of SQL for full table scan, large table query, and file sorting.
[0057] The matching submodule is used to match the analysis results of the SQL execution plan with the expert experience rules. If the expert experience rules are matched, the execution of the SQL that hits the expert experience rules is interrupted;
[0058] The notification submodule is used to send notification information through the internal instant messaging system after the SQL that hits the expert experience rule is interrupted.
[0059] The above is only an embodiment of the present invention. The common sense such as the known specific structure and characteristics in the scheme is not described in detail here. The ordinary technicians in the relevant field know all the common technical knowledge in the technical field of the invention before the application date or priority date, can know all the existing technologies in the field, and have the ability to apply the conventional experimental means before that date. The ordinary technicians in the relevant field can improve and implement this scheme in combination with their own abilities under the enlightenment given by this application. Some typical known structures or known methods should not become obstacles for ordinary technicians in the relevant field to implement this application. It should be pointed out that for those skilled in the art, without departing from the structure of the present invention, several deformations and improvements can be made, which should also be regarded as the protection scope of the present invention, which will not affect the effect of the implementation of the present invention and the practicality of the patent. The protection scope required by this application shall be based on the content of its claims, and the specific implementation methods and other records in the specification can be used to interpret the content of the claims.
Claims
1. A slow SQL monitoring method based on JAVA agent and bytecode enhancement, characterized in that: Includes the following: Intercept PrepareStatment in Mysql Jdbc when JVM loads the application system class; After intercepting PrepareStatement.class, perform bytecode enhancement on the executeQuery() method in PreparesStatement.class. The bytecode enhancement method is as follows: Get the connection instance of the current database; Rewrite the SQL and add the explain keyword to the original SQL statement; Execute the modified SQL and obtain the execution plan returned by MySQL; Analyze the SQL execution plan; Match the analysis results of the SQL execution plan with expert experience rules; For SQL statements whose analysis results match the expert experience rules, the execution is interrupted and a notification message is sent through the internal instant messaging system; for SQL statements whose analysis results do not match the expert experience rules, the execution continues according to the original logic; The bytecode enhancement function is packaged into a sqlagent.jar package, and sqlagent.jar is added to the startup parameters of the corresponding system.
2. The slow SQL monitoring method based on JAVA agent and bytecode enhancement according to claim 1 is characterized in that: When analyzing the SQL execution plan, rule matching is also included for SQLs of full table scans, large table queries, and file sorting.
3. The slow SQL monitoring system based on JAVA agent and bytecode enhancement is characterized by: It includes an interception module, which intercepts PrepareStatment in Mysql Jdbc when JVM loads the application system class; A bytecode enhancement module, which performs bytecode enhancement on the executeQuery() method in PreparesStatement.class based on ASM technology after intercepting PrepareStatement.class, including a connection instance acquisition submodule for acquiring a connection instance of the current database; SQL rewriting submodule, used to add explain keyword to the original SQL statement; The execution plan acquisition submodule is used to execute the modified SQL and obtain the execution plan returned by MySQL; Analysis submodule, used to analyze SQL execution plan; The matching submodule is used to match the analysis results of the SQL execution plan with the expert experience rules. If the expert experience rules are matched, the execution of the SQL that hits the expert experience rules will be interrupted; the SQL whose analysis results do not match the expert experience rules will continue to be executed according to the original logic; The notification submodule is used to send notification information through the internal instant messaging system after the SQL that hits the expert experience rule is interrupted; The packaging module is used to package the bytecode enhancement function into a sqlagent.jar package, and add sqlagent.jar to the startup parameters of the corresponding system.
4. The slow SQL monitoring system based on JAVA agent and bytecode enhancement according to claim 3 is characterized in that: The analysis submodule is also used to perform rule matching on SQL for full table scanning, large table query, and file sorting.
