A method, apparatus, device, and storage medium for locating execution statements.
By intercepting SQL execution statements and adding identification information within the MyBatis framework, and monitoring response time, the problem of low efficiency in locating SQL execution statements in multi-system databases was solved, enabling rapid location and adjustment, and improving user satisfaction.
Patent Information
- Application Number
- CN202310658967.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-05
- Publication Date
- 2026-01-30
- Estimated Expiration
- 2043-06-05
AI Technical Summary
In databases shared by multiple systems, when the response time of SQL execution statements is too long, existing technologies require manual scanning of code scripts to find the query, resulting in low search efficiency and impacting business processes.
By calling the interceptor of the MyBatis framework, query statements are intercepted, identification information is added, response time is monitored, and the identification information is obtained when the threshold is exceeded to locate the position of the SQL execution statement.
It improves the efficiency of SQL execution statement lookup, reduces manual intervention, and enhances user satisfaction.
Smart Images

Figure CN116821151B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device, and storage medium for locating execution statements. Background Technology
[0002] In industries such as fintech, the complex interactions of business processes often result in multiple systems sharing databases, such as those of banks. For example, lending systems and bank-enterprise interaction systems may share a database used to store information about external lending companies. Consequently, due to the sharing and interaction between multiple systems, a single database may have tens of thousands or even hundreds of millions of Structured Query Language (SQL) statements executed.
[0003] However, if a problem arises with a specific SQL statement in the database (such as an excessively long response time), locating the problematic SQL statement often requires manual scanning of the code by technical personnel. This manual search is inefficient and impacts subsequent business processes. Therefore, quickly locating SQL statements is a pressing issue that needs to be addressed. Summary of the Invention
[0004] In view of this, in order to overcome the shortcomings of the existing technology, this application provides a method, apparatus, device and storage medium for locating execution statements that can be applied to fields such as financial technology or other fields.
[0005] Firstly, this application provides a method for locating execution statements, including:
[0006] Call the interceptors registered and defined in the configuration file corresponding to the MyBatis framework to intercept all query statements;
[0007] Locate the SQL execution statement corresponding to the query statement, and add a predetermined form of identification information to the SQL execution statement; wherein, one SQL execution statement corresponds to one piece of identification information;
[0008] Monitor the response time of each of the aforementioned SQL execution statements during runtime;
[0009] When the response time is determined to be greater than a preset threshold, the identification information of the corresponding SQL execution statement is obtained, and then the location information of the SQL execution statement is determined through the identification information.
[0010] In an optional implementation, the method further includes:
[0011] Based on the location information, the corresponding SQL execution statement can be located;
[0012] The SQL execution statement is adjusted so that the response time of the SQL execution statement is less than or equal to the preset threshold.
[0013] In an optional implementation, finding the SQL execution statement corresponding to the query statement includes:
[0014] The StatementHandler in the MyBatis framework extracts the SQL execution statement corresponding to the query statement from the XML file in the mapper directory of the target project and the code annotation file of the target project.
[0015] In an optional implementation, the identification information includes a name domain identifier and a declaration identifier.
[0016] In an optional implementation, adding a predetermined form of identification information to the SQL execution statement includes:
[0017] Pre-defined annotation identifiers are used to label the identification information;
[0018] The identification information is added after the annotation is added to the SQL execution statement.
[0019] In an optional implementation, after monitoring the response time of each of the SQL execution statements, the method further includes:
[0020] Based on the execution status of all the SQL statements, an execution record is generated accordingly; wherein, the execution record contains the SQL statement associated with the record, the identification information corresponding to the SQL statement, and the response time of the SQL statement.
[0021] In an optional implementation, obtaining the identification information of the corresponding SQL execution statement includes:
[0022] The target response time is found from the execution record; wherein the target response time is greater than the preset threshold.
[0023] Based on the target response time, obtain the identification information of the SQL execution statement associated with the target response time.
[0024] Secondly, this application provides an execution statement locating device, comprising:
[0025] The interception module is used to call the interceptors registered and defined in the configuration file corresponding to the MyBatis framework to intercept all query statements.
[0026] An add module is used to find the SQL execution statement corresponding to the query statement and add a predetermined form of identification information to the SQL execution statement; wherein, one SQL execution statement corresponds to one piece of identification information;
[0027] The monitoring module is used to monitor the response time of each SQL execution statement when it is run.
[0028] The determination module is used to obtain the identification information of the corresponding SQL execution statement when the response time is determined to be greater than a preset threshold, and then determine the location information of the SQL execution statement through the identification information.
[0029] Thirdly, this application provides a computer device, the computer device including a memory and at least one processor, the memory storing a computer program, and the processor executing the computer program to implement the aforementioned execution statement location method.
[0030] Fourthly, this application provides a computer storage medium storing a computer program, which, when executed, implements the aforementioned method for locating execution statements.
[0031] The embodiments of this application have the following beneficial effects:
[0032] This application provides a method for locating execution statements. The method includes: calling an interceptor defined in the configuration file of the MyBatis framework to intercept all query statements; finding the SQL execution statement corresponding to the query statement and adding predetermined identification information to the SQL execution statement; wherein each SQL execution statement corresponds to one identification information; monitoring the response time of each SQL execution statement during runtime; and when the response time is determined to be greater than a preset threshold, obtaining the identification information of the corresponding SQL execution statement, and then determining the location information of the SQL execution statement through the identification information. This application intercepts query statements by calling an interceptor in the MyBatis framework to obtain the SQL execution statement corresponding to the actual query, and adds identification information to the SQL execution statement. This allows the corresponding SQL execution statement to be directly found through the identification information when the response time of the SQL execution statement exceeds a preset threshold, thereby improving the efficiency of SQL execution statement retrieval and increasing user satisfaction. Attached Figure Description
[0033] To more clearly illustrate the technical solutions of this application, the accompanying drawings used in the embodiments will be briefly described below. It should be understood that the following drawings only show some embodiments of this application and should not be considered as a limitation on the scope of protection of this application. In the various drawings, similar components are numbered similarly.
[0034] Figure 1 A schematic diagram of the first embodiment of the execution statement locating method in this application is shown;
[0035] Figure 2 A schematic diagram of a second embodiment of the execution statement locating method in this application is shown;
[0036] Figure 3 A schematic diagram of a third embodiment of the execution statement locating method in this application is shown;
[0037] Figure 4 A schematic diagram of the fourth embodiment of the execution statement locating method in this application is shown;
[0038] Figure 5 A schematic diagram of the fifth embodiment of the execution statement locating method in this application is shown;
[0039] Figure 6 A schematic diagram of the sixth embodiment of the execution statement locating method in this application is shown;
[0040] Figure 7 This shows a first structural schematic diagram of the execution statement locating device in an embodiment of this application;
[0041] Figure 8 A second structural schematic diagram of the execution statement locating device in an embodiment of this application is shown. Detailed Implementation
[0042] The technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments.
[0043] The components of the embodiments of this application described and illustrated in the accompanying drawings can be arranged and designed in a variety of different configurations. Therefore, the following detailed description of the embodiments of this application provided in the drawings is not intended to limit the scope of the claimed application, but merely to illustrate selected embodiments of the application. All other embodiments obtained by those skilled in the art based on the embodiments of this application without inventive effort are within the scope of protection of this application.
[0044] In the following, the terms “comprising,” “having,” and their cognates, which may be used in various embodiments of this application, are intended only to indicate a particular feature, number, step, operation, element, component, or combination thereof, and should not be construed as excluding, firstly, the presence of one or more other features, numbers, steps, operations, elements, components, or combinations thereof, or adding the possibility of one or more features, numbers, steps, operations, elements, components, or combinations thereof.
[0045] Furthermore, the terms "first," "second," and "third" are used only to distinguish descriptions and should not be interpreted as indicating or implying relative importance.
[0046] Unless otherwise specified, all terms used herein (including technical and scientific terms) shall have the same meaning as commonly understood by one of ordinary skill in the art to which the various embodiments of this application pertain. Terms (such as those defined in commonly used dictionaries) shall be interpreted as having the same meaning as in their contextual meaning in the relevant technical field and shall not be construed as having an idealized or overly formal meaning, unless clearly defined in the various embodiments of this application.
[0047] XML: A markup language used to mark up electronic documents to give them structure.
[0048] SQL execution statement: Structured Query Language.
[0049] MyBatis is a Java-based persistence layer framework, an open-source project from Apache, widely used in Java application systems.
[0050] Due to its superior performance, cross-platform compatibility, robustness, and security, Java is widely used in financial IT systems and other applications, with the Mybatis framework being the primary framework for persistence. The Mybatis framework supports custom SQL, stored procedures, and advanced mappings. Mybatis allows configuration and mapping of primitive types, interfaces, and Java POJOs (Plain Ordinary Java Objects) to database records using simple XML or annotations.
[0051] The MyBatis framework is mainly divided into the following four parts:
[0052] (1) Loading Configuration: There are two types of configurations. One type is the framework's own configuration, such as data source configuration (e.g., database configuration information) and mapper configuration. The SQL configuration information is loaded into a MappedStatement object (including input parameter mapping configuration, executed SQL statement, and result mapping configuration) and stored in memory. The other type is annotations in the JAVA code.
[0053] (2) SQL parsing: When the API interface layer receives a call request, it will receive the SQL ID and the passed object (which can be a Map, JAVABean or basic data type). Mybatis will find the corresponding MappedStatement based on the SQL ID, and then parse the MappedStatement based on the passed parameter object. After parsing, the final SQL execution statement and parameters to be executed can be obtained.
[0054] (3) SQL execution: The final SQL and parameters are taken to the database and executed to obtain the results of the database operation.
[0055] (4) Result mapping: The results of database operations are transformed according to the mapping configuration. They can be converted into HashMap, JAVABean or basic data types, and the final result is returned to the service caller.
[0056] In industries such as fintech, the complex interactions of business processes often lead to multiple systems sharing databases, with a single database frequently containing tens or even hundreds of millions of Structured Query Language (SQL) statements. Sometimes, operations staff need to identify which application a particular SQL statement originates from. Colleagues unfamiliar with the business logic or tables often find this difficult and must contact developers of the relevant database systems to scan the code and locate the corresponding SQL statement. This manual search is inefficient.
[0057] Mybatis (ibatis) essentially encapsulates Java database connectivity (i.e., JDBC), with SQL execution statements written in specific XML files. For a service to locate the specific SQL execution statement, two key factors are needed: the namespace ID and the statement ID.
[0058] Based on this, this application provides a method for locating execution statements. In the plugin mechanism of MyBatis (IBatis), by inheriting the Interceptor interface of MyBatis (IBatis), the executor of MyBatis (IBatis) is intercepted, and the name field identifier and declaration identifier of the executed statement are obtained. This allows information to be identified in each script code to trace the specific SQL execution statement.
[0059] Please refer to Figure 1 The method will now be described in detail. This method can be executed using a server, terminal, or other processor-equipped device or application system. This embodiment uses a server as an example, where a database and a Java application system are pre-configured, and the Java application system runs the MyBatis framework.
[0060] S10 calls the interceptors registered and defined in the configuration file corresponding to the MyBatis framework to intercept all query statements.
[0061] S20, find the SQL execution statement corresponding to the query statement, and add a predefined form of identification information to the SQL execution statement.
[0062] The server is configured with the MyBatis framework, and the configuration file for the MyBatis framework pre-registers and defines interceptors. By inheriting the Interceptor interface in the MyBatis (ibatis) framework, which is used to intercept the executor of MyBatis (ibatis), all query statements are intercepted.
[0063] It's understandable that the MyBatis framework's org.apache.ibatis.plugin package contains an Interceptor interface, which intercepts query statements by calling the interface's Object intercept(Invocation var1) throws Throwable.
[0064] Then, the specific SQL execution statement is retrieved from the target project (i.e., the currently executing Java project) using a query. Specifically, the SQL execution statement can be obtained using the following statement:
[0065] StatementHandler statementHandler=
[0066] (StatementHandler)invocation.getTarget(); / / Retrieves the SQL execution statement
[0067] BoundSql boundSql=statementHandler.getBoundSql()
[0068] In one implementation, such as Figure 2 As shown, in this embodiment of the application, "finding the SQL execution statement corresponding to the query statement" in S20 specifically includes the following steps:
[0069] S21. Using the StatementHandler in the MyBatis framework, extract the SQL execution statement corresponding to the query statement from the XML file in the mapper directory of the target project and the code annotation file of the target project.
[0070] It's understandable that the MyBatis framework is commonly used for database access in Java projects. When using MyBatis, SQL execution statements are typically written in XML files and / or code annotation files, specifically in the XML files and / or code annotation files located in the mapper directory of each Java project.
[0071] Therefore, when searching for the SQL execution statement, the StatementHandler in the MyBatis framework can read the XML file in the mapper directory of the target project (i.e., the target Java project) and the code annotation file of the target Java project. According to the pre-configured SQL execution statement syntax format matching strategy, the SQL execution statement corresponding to the query statement can be extracted from the XML file and the code annotation file.
[0072] In one implementation, such as Figure 3 As shown, in this embodiment of the application, the step of "adding a predetermined form of identification information to the SQL execution statement" in S20 specifically includes the following steps:
[0073] S22, use preset annotation identifiers to mark the identification information.
[0074] S23 adds annotation information to the SQL execution statement.
[0075] In this embodiment, identification information for each SQL execution statement is pre-defined, as are comment identifiers. These comment identifiers can be " / *" and "* / ", and are used to identify the SQL execution statement and its specific location. Furthermore, one SQL execution statement corresponds to one set of identification information. Optionally, the identification information includes a namespace ID and a statement ID.
[0076] Then, the identification information is marked using the preset comment identifier. Subsequently, the marked identification information is inserted into each SQL execution statement so that the specific location of the SQL execution statement can be found through the identification information each time the SQL execution statement is executed.
[0077] It is understandable that the namespace id and statement id are added to each SQL execution statement in the form of " / *...* / ".
[0078] S30 monitors the response time of each SQL execution statement during runtime.
[0079] When executing SQL statements, the response time of each SQL statement is monitored synchronously. Optionally, this can be done by pre-setting a listener to monitor each SQL statement, or by starting a listening service through the server's listening component to monitor the response time of each SQL statement during execution.
[0080] As an optional implementation method, such as Figure 4 As shown, after S30, this embodiment of the application further includes the following steps:
[0081] S50 generates execution records based on the execution status of all SQL statements.
[0082] While monitoring the response time of each SQL statement being executed, the server also generates execution records based on the execution status (i.e., the execution status) of each SQL statement.
[0083] The execution record contains the SQL execution statement associated with the record, the identification information corresponding to the SQL execution statement, and the response time of the SQL execution statement.
[0084] It is understandable that when the server executes an SQL statement to complete the corresponding task, it generates a corresponding execution record. This execution record is used to record the execution process of each SQL statement executed by the server. Furthermore, this execution record is also used to reflect the status of each SQL statement being run and the completion status of the task.
[0085] S40: When the response time is determined to be greater than a preset threshold, the identification information of the corresponding SQL execution statement is obtained, and then the location information of the SQL execution statement is determined through the identification information.
[0086] SQL execution statements with response times exceeding a preset threshold are selected as target execution statements. Furthermore, when the response time of an SQL execution statement is detected to be greater than the preset threshold, the identification information of that SQL execution statement is obtained accordingly.
[0087] The specific value of the preset threshold can be set according to actual needs and is not limited here; for example, the preset threshold can be 15s, 30s, or 40s, etc.
[0088] As an optional implementation method, such as Figure 5 As shown, in this embodiment of the application, "obtaining the identification information of the SQL execution statement" in S30 specifically includes the following steps:
[0089] S41, find the target response time from the execution log.
[0090] S42, based on the target response time, obtain the identification information of the SQL execution statement associated with the target response time.
[0091] The system retrieves the response times of all completed SQL statements from the execution logs and identifies response times greater than a preset threshold. These response times are then used as the target response time. In other words, the target response time is greater than the preset threshold.
[0092] Then, the identification information of the SQL execution statement associated with the target response time is searched in the execution record, that is, the identification information used to identify the SQL execution statement is searched.
[0093] It is understandable that this identification information can determine the specific location of the SQL execution statement in the code file.
[0094] As an optional implementation method, such as Figure 6 As shown, the embodiments of this application also specifically include the following steps:
[0095] S61, based on the location information, find the corresponding SQL execution statement.
[0096] S62, adjust the SQL execution statement so that the response time of the SQL execution statement is less than or equal to a preset threshold.
[0097] Based on the location information of the SQL execution statement, the specific location of the SQL execution statement can be located in the code file.
[0098] Subsequently, based on user needs, the SQL execution statement will be adjusted, including modifications and deletions, until the response time of the SQL execution statement is less than or equal to a preset threshold.
[0099] This embodiment of the application intercepts all query statements by calling the interceptors registered and defined in the configuration file corresponding to the MyBatis framework; it finds the SQL execution statement corresponding to the query statement and adds predetermined identification information to the SQL execution statement; it monitors the response time of each SQL execution statement; when the response time is determined to be greater than a preset threshold, it obtains the identification information of the corresponding SQL execution statement, and then determines the location information of the SQL execution statement through the identification information. This embodiment of the application intercepts query statements by calling the interceptors in the MyBatis framework to obtain the SQL execution statements involved in the actual execution of the query operation by the server. This allows identification information to be added to the SQL execution statements, so that when the response time of an SQL execution statement exceeds a preset threshold, the corresponding SQL execution statement can be directly found through this identification information. This improves the efficiency of SQL execution statement retrieval and increases user satisfaction.
[0100] Please refer to Figure 7 This application provides an execution statement locating device, the device comprising:
[0101] Interceptor module 110 is used to call the interceptors registered and defined in the configuration file corresponding to the MyBatis framework to intercept all query statements;
[0102] Adding module 120 is used to find the SQL execution statement corresponding to the query statement and add a predetermined form of identification information to the SQL execution statement; wherein, one SQL execution statement corresponds to one piece of identification information;
[0103] The monitoring module 130 is used to monitor the response time of each SQL execution statement when it is run.
[0104] The determination module 140 is used to obtain the identification information of the corresponding SQL execution statement when it is determined that the response time is greater than a preset threshold, and then determine the location information of the SQL execution statement through the identification information.
[0105] Optional, please refer to Figure 8 The execution statement locator also includes:
[0106] The lookup module 150 is used to find the corresponding SQL execution statement based on the location information;
[0107] The adjustment module 160 is used to adjust the SQL execution statement so that the response time of the SQL execution statement is less than or equal to the preset threshold.
[0108] The above-described execution statement locating device corresponds to the execution statement locating method in the above embodiments; any of the options in the above embodiments are also applicable to this embodiment, and will not be described in detail here.
[0109] This application also provides a computer device, which includes a memory and at least one processor. The memory stores a computer program, and the processor executes the computer program to implement the execution statement location method of the above embodiments.
[0110] The memory may include a stored program area and a stored data area. The stored program area may store the operating system and application programs required for at least one function. The stored data area may store data created based on the use of the computer device (such as identification information, response time, etc.). In addition, the memory may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0111] This application also provides a computer storage medium storing computer-executable instructions. When the computer-executable instructions are invoked and executed by a processor, the computer-executable instructions cause the processor to perform the steps of the execution statement location method described above.
[0112] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can also be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the flowcharts and block diagrams in the accompanying drawings show the architecture, functionality, and operation of possible implementations of apparatus, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that, in alternative implementations, the functions marked in the blocks may occur in a different order than those marked in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagram and / or flowchart, and combinations of blocks in the block diagram and / or flowchart, can be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.
[0113] In addition, the functional modules or units in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.
[0114] If the aforementioned functions are implemented as software functional modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, essentially, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a smartphone, personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0115] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. A method of performing statement positioning, the method comprising: The method comprises the following steps: calling an interceptor registered and defined in a configuration file corresponding to a mybatis framework to intercept all query statements; finding a SQL execution statement corresponding to the query statement from an XML file in a mapper directory of a target project and a code annotation file of the target project through a StatementHandler in the mybatis framework; marking identification information with a preset annotation marker, and adding the marked identification information in the SQL execution statement; one SQL execution statement corresponds to one identification information; the identification information comprises a name domain identification number and a declaration identification number; listening to a response time when each SQL execution statement is run; generating an execution record according to execution conditions of all SQL execution statements; the execution record contains the SQL execution statement associated with the record, the identification information corresponding to the SQL execution statement, and the response time of the SQL execution statement; the execution record is used to reflect the state when each SQL execution statement is run and the completion state of the task; when it is determined that the response time is greater than a preset threshold, obtaining the identification information of the corresponding SQL execution statement, and then determining position information of the SQL execution statement through the identification information.
2. The method of claim 1, wherein, The method further comprises the following steps: finding the corresponding SQL execution statement according to the position information; adjusting the SQL execution statement so that the response time when the SQL execution statement is run is less than or equal to the preset threshold.
3. The method of claim 1, wherein, The method further comprises the following steps: finding a target response time from the execution record; the target response time is greater than the preset threshold; obtaining the identification information of the SQL execution statement associated with the target response time according to the target response time.
4. An apparatus for performing statement positioning, the apparatus comprising: The method comprises the following steps: an intercepting module for calling an interceptor registered and defined in a configuration file corresponding to a mybatis framework to intercept all query statements; an adding module for finding a SQL execution statement corresponding to the query statement from an XML file in a mapper directory of a target project and a code annotation file of the target project through a StatementHandler in the mybatis framework; marking identification information with a preset annotation marker, and adding the marked identification information in the SQL execution statement; one SQL execution statement corresponds to one identification information; the identification information comprises a name domain identification number and a declaration identification number; a listening module for listening to a response time when each SQL execution statement is run; generating an execution record according to execution conditions of all SQL execution statements; the execution record contains the SQL execution statement associated with the record, the identification information corresponding to the SQL execution statement, and the response time of the SQL execution statement; the execution record is used to reflect the state when each SQL execution statement is run and the completion state of the task; The determining module is configured to, when determining that the response time is greater than a preset threshold, acquire identification information of a corresponding SQL execution statement, and then determine position information of the SQL execution statement through the identification information.
5. A computer device, comprising: The computer device comprises a memory and at least one processor, the memory stores a computer program, and the processor is configured to execute the computer program to implement the execution statement positioning method in any one of claims 1-3.
6. A computer storage medium, characterized in that The computer program is stored in the computer device and is executed to implement the execution statement positioning method in any one of claims 1-3.
Citation Information
Patent Citations
Performance monitoring method and device for database operation, equipment and storage medium
CN110297814A