A SQL tracking method and tool for Oracle database
By backing up and comparing the v$sqlarea view, we can quickly obtain metrics such as the execution time and disk read/write operations of Oracle database SQL, solving the problem of difficult SQL tracing in Oracle databases and achieving efficient performance analysis.
Patent Information
- Application Number
- CN202010806440.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-08-12
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2040-08-12
AI Technical Summary
In existing technologies, Oracle databases lack fast and convenient SQL tracing methods, resulting in high SQL acquisition costs and long processing times, making it difficult to effectively analyze performance issues.
By backing up and comparing the v$sqlarea view, metrics such as SQL execution time and disk read/write are recorded and calculated. Using the accumulated value of the v$sqlarea view, combined with left join and NVL transformation, SQL-related metrics can be obtained quickly.
It enables rapid tracing of Oracle database SQL, saving time, simplifying the performance analysis process, and reducing the cost of SQL retrieval.
Smart Images

Figure CN111984501B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of oracle database, in particular to a sql tracking method and tool of oracle database. Background Art
[0002] In the business software production environment, many product backends use various databases, among which Oracle database accounts for a high proportion, especially in some large projects. In the process of using the product, you may encounter various performance problems. To locate the problems encountered, you need to analyze and study from various aspects. One of the more important ones is database SQL tuning, that is, analyzing SQL execution time, CPU usage, disk read and write, etc., to see if there is room for optimization.
[0003] Compared with other databases, such as Microsoft's SQL Server database, which has its own SQL tracking and analysis tool SQL Profiler, Oracle database does not officially provide a convenient, easy-to-use and fast method to track SQL for a certain period of time. Therefore, during the tuning process, a lot of time is spent on SQL acquisition, and it may require multiple operations and multiple analyses, which makes SQL acquisition more expensive. Currently, there are generally two ways to obtain Oracle SQL:
[0004] The first method is that product developers output the SQL used in the application and calculate the SQL execution time. However, this method is not universal and requires developers to write additional output code. It can only calculate the SQL time consumption, but cannot calculate the SQL CPU usage, disk read and write usage, etc.
[0005] The second method is to use Oracle's own ARW report to generate an AWR report for a certain period and obtain the SQL-related indicators in the report. This is a common method, but the disadvantage is that the steps for generating an AWR report each time are cumbersome, especially when multiple captures and multiple analyses are required, which takes a long time. In addition, there are many other indicators in the report, and it is very difficult to find SQL. Summary of the invention
[0006] In view of the needs and shortcomings of current technology development, the present invention provides a SQL tracking method and tool for an Oracle database, aiming to achieve fast and convenient tracking of the SQL of an Oracle database within a certain period of time.
[0007] First, the present invention provides a SQL tracking method for an Oracle database, and the technical solution adopted to solve the above technical problems is as follows:
[0008] A SQL tracking method for an Oracle database, the implementation process of the method comprising:
[0009] 1) After logging in and connecting to the Oracle database that needs to track SQL, back up the current v$sqlarea view through SQL and record the database time at this moment.
[0010] 2) Start the business operating system and execute business operations that require SQL tracking, such as executing voucher query or voucher saving operations in the financial management system.
[0011] 3) After completing the business operations that need to be tracked, back up the current database v$sqlarea view again.
[0012] 4) Compare the corresponding fields of the v$sqlarea view in step 3) and the v$sqlarea view in step 1), and obtain all SQL and related indicators through the difference of the corresponding fields in the two views.
[0013] Furthermore, in step 1), execute create table trace_start as select*from v$sqlarea, and use this SQL to back up the entire v$sqlarea table data at the current moment to the table trace_start.
[0014] Furthermore, in step 1), select sysdate from dual is executed, and the queried time is assigned to the program variable T.
[0015] Furthermore, in step 3), execute create table trace_end as select*from v$sqlarea to back up the entire v$sqlarea table data at this moment to the trace_end table.
[0016] Furthermore, in Oracle database, the data of each field of v$sqlarea view is cumulative value, which records the SQL executed in the recent period and various cumulative indicators. The indicators are not limited to the total SQL execution time, total number of times, total disk read and write, and CPU usage;
[0017] During step 4):
[0018] By querying the trace_end table for last_active_time>T, you can get the SQL data after trace_start is generated, that is, the SQL data during the execution of business operations.
[0019] By comparing the sql_id fields in the trace_end and trace_start tables, if they are the same, they are the same SQL. Subtract the corresponding indicator field values in the trace_start table from the remaining indicator field values in the trace_end table of the same SQL to obtain the total execution time, total number of times, total disk read and write, and CPU usage indicators of all SQLs during business execution.
[0020] Further preferably, during the execution of step 4), there may be new sql appearing in trace_end, that is, sql_id appearing in trace_end but not in trace_start. At this time, left join trace_end and nvl conversion of null values are required in the data processing sql.
[0021] Secondly, the present invention provides a SQL tracking tool for an Oracle database, and the technical solution adopted to solve the above technical problems is as follows:
[0022] A SQL tracking tool for an Oracle database includes a login module, a backup module 1, a backup module 2, and a comparison processing module.
[0023] The login module is used to assist users in logging in to the Oracle database that needs to track SQL.
[0024] Backup module 1 is used to back up the current v$sqlarea view to the table trace_start through SQL, and record the database time at this moment, and assign the queried time to the program variable T.
[0025] Start the business operating system that needs to trace SQL. After performing a certain business operation, backup module 2 is used to back up the current database v$sqlarea view to table trace_end.
[0026] The comparison processing module is used to compare the corresponding fields of the v$sqlarea view backed up by backup module one and backup module two, and obtain all SQL and related indicators through the difference of the corresponding fields in the two views.
[0027] Furthermore, in the Oracle database, the data in each field of the v$sqlarea view is a cumulative value, which records the SQL statements executed in the recent period and various cumulative indicators. The indicators are not limited to the total SQL execution time, total number of times, total disk reads and writes, and CPU usage.
[0028] The comparison processing module first obtains the sql data after the trace_start is generated by querying the last_active_time>T in the trace_end table, that is, the sql data during the execution of business operations, and then compares the sql_id fields in the trace_end and trace_start tables. If they are the same, they are the same sql. The remaining indicator field values in the trace_end table of the same sql are subtracted from the corresponding indicator field values in the trace_start table to obtain the total execution time, total number of times, total disk read and write, and CPU occupancy of all sql during the business execution.
[0029] Furthermore, when the comparison processing module compares the sql_id fields in the trace_end and trace_start tables, there may be new sql in trace_end, that is, sql_id that appears in trace_end but not in trace_start. At this time, it is necessary to use left join trace_end and use nvl to convert null values in the data processing sql.
[0030] The sql tracking method and tool of an oracle database of the present invention have the following beneficial effects compared with the prior art:
[0031] The present invention can realize rapid tracking of the Oracle database SQL in a certain business scenario, including SQL execution time, disk reading and writing, etc., solves the problem of difficulty in tracking Oracle database SQL, and realizes rapid acquisition of SQL indicators and analysis of performance problems without relying on AWR reports, thus greatly saving time. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] Attached Figure 1 is a flow chart of the tracking method of the present invention;
[0033] Attached Figure 2 It is a framework diagram of the tracking tool of the present invention.
[0034] The information of each reference number in the accompanying drawings indicates:
[0035] 1. Login module, 2. Backup module 1, 3. Backup module 2, 4. Comparison processing module. DETAILED DESCRIPTION
[0036] In order to make the technical solution, the technical problem solved and the technical effect of the present invention more clearly understood, the technical solution of the present invention is clearly and completely described below in conjunction with specific embodiments.
[0037] Embodiment 1:
[0038] Combined with Figure 1 This embodiment proposes a SQL tracking method for an Oracle database, and the implementation process of the method includes:
[0039] 1) After logging in to the Oracle database that needs to trace SQL, back up the current database v$sqlarea view through SQL: create table trace_start as select*from v$sqlarea, back up the data contained in the current database v$sqlarea view to table A, which is table trace_start; after the backup is completed, record the database time at this moment: select sysdate from dual, and assign the queried time to the program variable T. The specific implementation code is as follows:
[0040]
[0041] 2) Start the business operating system and execute business operations that require SQL tracking, such as executing voucher query or voucher preservation operations in the financial management system.
[0042] 3) After completing the business operations that need to be traced, back up the current database v$sqlarea view again: createtable trace_end as select*from v$sqlarea, and back up the data contained in the entire v$sqlarea view to table B, which is table trace_end. The specific implementation code is as follows:
[0043]
[0044] In Oracle database, the data in each field of v$sqlarea view is cumulative value, which records the SQL executed in the recent period and various cumulative indicators. The indicators are not limited to the total SQL execution time, total number of times, total disk read and write, and CPU usage.
[0045] 4) Compare the corresponding fields of the v$sqlarea view in step 3) and the v$sqlarea view in step 1), and obtain all SQL and related indicators through the difference of the corresponding fields in the two views. The specific process is as follows:
[0046] 4.1) By querying the last_active_time>T in the trace_end table, you can get the SQL data after the trace_start is generated, that is, the SQL data during the execution of business operations.
[0047] 4.2) Then compare the sql_id fields in the trace_end and trace_start tables. If they are the same, they are the same SQL. Subtract the corresponding indicator field values in the trace_start table from the remaining indicator field values in the trace_end table of the same SQL to obtain the total execution time, total number of times, total disk read and write, and CPU usage indicators of all SQLs during business execution.
[0048] During step 4), there may be new SQL in trace_end, that is, SQL_ID that appears in trace_end but not in trace_start. In this case, left join trace_end and nvl conversion of null values are required in data processing SQL.
[0049] The specific implementation code of step 4) is:
[0050]
[0051]
[0052] Embodiment 2:
[0053] Reference Figure 1 , 2 This embodiment proposes a SQL tracking tool for an Oracle database, which includes a login module 1, a backup module 1 2, a backup module 2 3, and a comparison processing module 4.
[0054] Login module 1 is used to assist users in logging in to connect to the Oracle database that requires SQL tracking.
[0055] Backup module 1 2 is used to back up the current v$sqlarea view to the table trace_start through sql, record the database time at this moment, and assign the queried time to the program variable T.
[0056] Start the business operating system that needs to track SQL and perform a certain business operation, such as executing the voucher query or voucher preservation business of the financial management system. After the execution is completed, the backup module 2 3 is used to back up the current database v$sqlarea view to the trace_end table.
[0057] The comparison processing module 4 is used to compare the corresponding fields of the v$sqlarea view backed up by the backup module 1 2 and the backup module 2 3, and obtain all SQL and related indicators through the difference of the corresponding fields in the two views.
[0058] In Oracle database, the data in each field of v$sqlarea view is cumulative value, which records the SQL executed in the recent period and various cumulative indicators. The indicators are not limited to the total SQL execution time, total number of times, total disk read and write, and CPU usage.
[0059] In this embodiment, the comparison processing module 4 first obtains the sql data after the trace_start is generated, that is, the sql data during the execution of the business operation, by querying the last_active_time>T in the trace_end table, and then compares the sql_id fields in the trace_end table and the trace_start table. If they are the same, they are the same sql. The remaining indicator field values of the trace_end table of the same sql are subtracted from the corresponding indicator field values in the trace_start table to obtain the total execution time, total number of times, total disk read and write, and CPU occupancy indicators of all sql during the business execution.
[0060] When the comparison processing module 4 compares the sql_id fields in the trace_end and trace_start tables, there may be new sql in trace_end, that is, sql_id that appears in trace_end but not in trace_start. At this time, it is necessary to use left join trace_end and use nvl to convert null values in the data processing sql.
[0061] In summary, the sql tracking method and tool of an oracle database of the present invention can realize rapid tracking of the oracle database sql in a certain business scenario, including sql execution time, disk reading and writing, etc., and solve the problem of difficulty in oracle database sql tracking.
[0062] The above specific examples are used to explain the principles and implementation methods of the present invention in detail. These examples are only used to help understand the core technical content of the present invention. Based on the above specific embodiments of the present invention, any improvements and modifications made by technicians in this technical field without departing from the principles of the present invention should fall within the scope of patent protection of the present invention.
Claims
1. A SQL tracking method for Oracle database, Features ,The implementation process of this method includes: 1) After logging in to the Oracle database that needs to track SQL, back up the current v$sqlarea view through SQL and record the database time at this moment; 2) Start the business operating system and execute business operations that require SQL tracking. 3) After completing the business operations that need to be tracked, back up the current database v$sqlarea view again. 4) In Oracle database, the data of each field of v$sqlarea view is cumulative value, which records the SQL executed in the recent period and various cumulative indicators. The indicators are not limited to the total SQL execution time, total number of times, total disk read and write, and CPU usage. Compare the corresponding fields of v$sqlarea view in step 3) and v$sqlarea view in step 1), and obtain all SQL and related indicators through the difference of corresponding fields in the two views. In this process, by querying last_active_time>T in trace_end table, the SQL data after trace_start is generated, that is, the SQL data during the business operation, and then by comparing the sql_id field in trace_end and trace_start tables, if they are the same, they are the same SQL. The remaining indicator field values of the trace_end table of the same SQL minus the corresponding indicator field values in the trace_start table can obtain the total execution time, total number of times, total disk read and write, and CPU usage indicators of all SQL during the business execution.
2. The sql tracking method of an oracle database according to claim 1, It is characterized in that In step 1), execute create table trace_start as select * from v$sqlarea to back up the entire v$sqlarea table data at the current moment to the trace_start table through this SQL.
3. The sql tracking method of an oracle database according to claim 2, It is characterized in that In step 1), execute select sysdate from dual and assign the queried time to the program variable T.
4. The sql tracking method of an oracle database according to claim 3, It is characterized in that In step 3), execute create table trace_end as select * from v$sqlarea to back up the entire v$sqlarea table data at this moment to the table trace_end.
5. The sql tracking method of an oracle database according to claim 4, It is characterized in that During step 4), there may be new SQL in trace_end, that is, SQL_ID that appears in trace_end but not in trace_start. In this case, you need to use left join trace_end and use nvl to convert null values in the data processing SQL.
6. A SQL tracking tool for Oracle database, It is characterized in that It includes a login module, a backup module 1, a backup module 2, and a comparison processing module. The login module is used to assist users in logging in to connect to the Oracle database that needs to track SQL. The backup module 1 is used to back up the current v$sqlarea view to the table trace_start through sql, and record the database time at this moment, and assign the queried time to the program variable T. Start the business operating system that needs to track SQL. After performing a certain business operation, the backup module 2 is used to back up the current database v$sqlarea view to the table trace_end. In the Oracle database, the data of each field of the v$sqlarea view is a cumulative value, which records the SQL executed in the recent period and various cumulative indicators, including but not limited to the total SQL execution time, total number of times, total disk read and write, and CPU usage; The comparison processing module is used to compare the corresponding fields of the v$sqlarea views backed up by the backup module one and the backup module two, and obtain all SQLs and related indicators through the difference of the corresponding fields in the two views. The specific process is: first, by querying the last_active_time>T in the trace_end table, the SQL data after the trace_start is generated, that is, the SQL data during the execution of the business operation, and then by comparing the sql_id fields in the trace_end table and the trace_start table, if they are the same, they are the same SQL. The remaining indicator field values of the trace_end table of the same SQL are subtracted from the corresponding indicator field values in the trace_start table, and the total execution time, total number of times, total disk read and write, and CPU occupancy indicators of all SQLs during the business execution period can be obtained.
7. The sql tracking tool for Oracle database according to claim 6, It is characterized in that When the comparison processing module compares the sql_id fields in the trace_end and trace_start tables, there may be new sql in trace_end, that is, sql_id that appears in trace_end but not in trace_start. At this time, left join trace_end and nvl conversion of null values are required in the data processing sql.
Citation Information
Patent Citations
MYSQL database heterogeneous log based replication
CN103221949A
Consensus point capturing method based on Oracle database
CN103399921A
