SQL statement checking method and device

By analyzing application log files and using the convolutional neural network model to check SQL statements, the adaptability and accuracy problems of SQL statement checking in the existing technology are solved, the risk of full table scan and false positive rate are reduced, and more efficient SQL is achieved. Statement check.

CN113254471BActive Publication Date: 2024-10-29INDUSTRIAL AND COMMERCIAL BANK OF CHINA
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202110589989.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-05-28
Publication Date
2024-10-29
Estimated Expiration
2041-05-28

AI Technical Summary

Technical Problem

When the existing technology checks SQL statements that cannot match the index, the adaptability of static code scanning is not high, and there are false positives in dynamic monitoring. It is also difficult to accurately locate data operation problems in medium and large projects, resulting in frequent problems such as full table scans.

Method used

By obtaining application log files, extracting text data and preprocessing, using the pre-established convolutional neural network model to predict statements, generate complete SQL statements and obtain execution plan results, generate statement check results, reduce the hidden dangers of full table scans, and reduce errors. Report rate.

Benefits of technology

It improves the integrity and accuracy of SQL statement checking, reduces the risk of full table scanning, and reduces the false positive rate. It is more targeted than dynamic monitoring and gets rid of the configuration dependence of static code scanning.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113254471B_ABST
    Figure CN113254471B_ABST
Patent Text Reader

Abstract

The present invention provides a method and device for checking SQL statements, which can be used in the financial field or other fields. The method includes: extracting statements from the obtained application log file to obtain text data; obtaining data information based on the text statement and the input parameters, inputting the data information corresponding to the text statement into a pre-established prediction model, and obtaining a statement prediction result corresponding to the text statement; if the statement prediction result is an SQL statement, a complete SQL statement is generated based on the text statement and the input parameters; obtaining the corresponding execution plan result, and generating a statement checking result based on the statement prediction result and the execution plan result corresponding to the complete SQL statement. The present invention obtains text statements by analyzing the application log file, and predicts the text statements in combination with the prediction model and the execution plan result, thereby reducing the hidden dangers of full table scanning, and at the same time getting rid of the configuration dependence of static code scanning, which is more targeted than dynamic monitoring and reduces the false alarm rate.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of SQL checking technology, and in particular to a SQL statement checking method and device. Background Art

[0002] Database management system is a large-scale software for manipulating and managing databases. It is indispensable in any platform and system. The data manipulation language DML (Data Manipulation Language) it provides allows users to perform operations such as adding, deleting, checking and modifying data. Especially in distributed systems and large applications, the amount of data is large, many tables are involved, and the operations on data are more frequent and the logic is complex. Therefore, the requirements for writing data operation sql statements are higher. Different sql statements may consume very different time. The execution of sql leading to full table scan is also one of the reasons for the timeout, downtime and other problems currently encountered. Therefore, it is extremely important to retrieve the sql that does not match the index and leads to full table scan.

[0003] At present, there are two methods for checking SQL that cannot match the index: static code scanning and dynamic monitoring system. Static code scanning is not highly adaptable to the diversity of SQL statements and parameters. Dynamic monitoring system has certain false positives when analyzing through dimensions such as time and memory consumption. It cannot accurately locate tables with small amounts of data in the test environment. For actual medium and large-scale standard projects, enterprises often store the log specifications involved in the corresponding files. For example, SQL statements involving data operations will be uniformly stored in a certain SQL log file, and the data types of the outgoing parameters will be printed together. Summary of the invention

[0004] In view of the problems existing in the prior art, the main purpose of the embodiments of the present invention is to provide a method and device for checking SQL statements, so as to improve the integrity and accuracy of SQL statement checking.

[0005] In order to achieve the above object, an embodiment of the present invention provides a SQL statement checking method, the method comprising:

[0006] Obtaining an application log file, and extracting statements from the application log file to obtain text data; wherein the text data includes text statements and input parameters;

[0007] According to the text sentence and the input parameters, data information corresponding to the text sentence is obtained, and the data information corresponding to the text sentence is input into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence;

[0008] If the statement prediction result is an SQL statement, a complete SQL statement is generated according to the corresponding text statement and the input parameters;

[0009] The execution plan result corresponding to the complete SQL statement is obtained, and a statement checking result is generated according to the statement prediction result and the execution plan result corresponding to the complete SQL statement.

[0010] Optionally, in an embodiment of the present invention, obtaining data information corresponding to the text statement according to the text statement and the input parameter includes:

[0011] Preprocessing the text statement and the input parameters; wherein the preprocessing includes text filling and format conversion;

[0012] According to a preset dictionary, text conversion is performed on the preprocessed text sentence and the input parameters to obtain data information corresponding to the text sentence.

[0013] Optionally, in one embodiment of the present invention, the prediction model is established in the following manner:

[0014] Obtaining a historical log file and its corresponding prediction results, extracting statements from the historical log file to obtain historical data; wherein the historical data includes historical statements and historical parameters;

[0015] According to the historical statements and the historical parameters, historical data information corresponding to the historical statements is obtained, and training samples are obtained according to the prediction results corresponding to the historical log files and the historical data information;

[0016] The initial convolutional neural network model is trained using the training samples to obtain the prediction model.

[0017] Optionally, in an embodiment of the present invention, the method further includes:

[0018] The application log file is classified into statements according to the statement prediction results; wherein the statement prediction results also include non-SQL statements.

[0019] The embodiment of the present invention further provides a SQL statement checking device, the device comprising:

[0020] A statement extraction module is used to obtain an application log file and perform statement extraction on the application log file to obtain text data; wherein the text data includes text statements and input parameters;

[0021] A prediction result module, used to obtain data information corresponding to the text sentence according to the text sentence and the input parameters, and input the data information corresponding to the text sentence into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence;

[0022] A complete statement module, used to generate a complete SQL statement according to the corresponding text statement and input parameters if the statement prediction result is an SQL statement;

[0023] The checking result module is used to obtain the execution plan result corresponding to the complete SQL statement, and generate a statement checking result according to the statement prediction result and the execution plan result corresponding to the complete SQL statement.

[0024] Optionally, in one embodiment of the present invention, the prediction result module includes:

[0025] A preprocessing unit, used for preprocessing the text statement and the input parameters; wherein the preprocessing includes text filling and format conversion;

[0026] The data information unit is used to perform text conversion on the preprocessed text sentence and the input parameters according to a preset dictionary to obtain data information corresponding to the text sentence.

[0027] Optionally, in one embodiment of the present invention, the device further includes:

[0028] A historical data module is used to obtain historical log files and their corresponding prediction results, extract statements from the historical log files, and obtain historical data; wherein the historical data includes historical statements and historical parameters;

[0029] A training sample module, used to obtain historical data information corresponding to the historical sentence according to the historical sentence and the historical parameter, and obtain a training sample according to the prediction result corresponding to the historical log file and the historical data information;

[0030] The prediction model module is used to train the initial convolutional neural network model using the training samples to obtain the prediction model.

[0031] Optionally, in one embodiment of the present invention, the device further includes:

[0032] A statement classification module is used to classify the application log file according to the statement prediction result; wherein the statement prediction result also includes non-SQL statements.

[0033] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above method when executing the program.

[0034] The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program for executing the above method.

[0035] The present invention obtains text statements by analyzing application log files, and predicts the text statements in combination with prediction models and execution plan results, so as to judge whether the statements in the application log files are non-SQL statements, thereby reducing the hidden dangers of full table scans caused by them, and at the same time getting rid of the configuration dependence of static code scanning. Compared with dynamic monitoring, it is more targeted and reduces the false alarm rate. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0037] Figure 1 A flowchart of a method for checking SQL statements according to an embodiment of the present invention;

[0038] Figure 2 A flowchart of obtaining data information in an embodiment of the present invention;

[0039] Figure 3 A flowchart of establishing a prediction model in an embodiment of the present invention;

[0040] Figure 4 A flowchart of a SQL statement checking method in a specific embodiment of the present invention;

[0041] Figure 5 This is a schematic diagram of prediction model training in a specific embodiment of the present invention;

[0042] Figure 6 A schematic diagram of a prediction model training process in another specific embodiment of the present invention;

[0043] Figure 7 This is a schematic diagram of the structure of a SQL statement checking device according to an embodiment of the present invention;

[0044] Figure 8 This is a schematic diagram of the structure of the prediction result module in an embodiment of the present invention;

[0045] Fig. 9 It is a structural diagram of a SQL statement checking device in a specific embodiment of the present invention;

[0046] Fig.10 It is a structural schematic diagram of a SQL statement checking device in another specific embodiment of the present invention;

[0047] Fig.11 The figure is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0048] The embodiments of the present invention provide a SQL statement checking method and device, which can be used in the financial field or other fields. It should be noted that the SQL statement checking method and device of the present invention can be used in the financial field, and can also be used in any field other than the financial field. The application field of the SQL statement checking method and device of the present invention is not limited.

[0049] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0050] like Figure 1 The flowchart of a SQL statement checking method according to an embodiment of the present invention is shown. The execution subject of the SQL statement checking method provided by the embodiment of the present invention includes but is not limited to a computer. The method shown in the figure includes:

[0051] Step S1, obtaining an application log file, and extracting statements from the application log file to obtain text data; wherein the text data includes text statements and input parameters.

[0052] The application log file is obtained from the server where the application is deployed through a program or manually, and the text statements and input parameters in the log file generated by the application are extracted using regular expressions, and useless statements are deleted, such as time nodes in the log.

[0053] Furthermore, the statements in the application log file are extracted to obtain text data, which includes text statements and data information of input parameters. The text data is specifically as follows:

[0054] Select a from table where col1=? and col2=?

[0055] Params:(String)c1,(Integer)2

[0056] Among them, c1 and 2 are the input parameters, and the input types are String and Integer.

[0057] Step S2, obtaining data information corresponding to the text sentence according to the text sentence and the input parameters, and inputting the data information corresponding to the text sentence into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence.

[0058] Among them, the text sentences and input parameters are preprocessed, including text filling and format conversion, and the preprocessed text sentences and input parameters are input and stored. Specifically, the text sentences are separated by spaces and stored in the list list, each short text is processed to the same length, and the missing parts can be filled with a specific text. Then, a dictionary is created or an already created dictionary is used to attach an integer number to each word in the text sentence. Furthermore, the text information is replaced with numerical information according to the dictionary number to obtain the data information corresponding to the text sentence.

[0059] Specifically, suppose there is a statement:

[0060] select accno from table where cardno=?

[0061] Params:(String)*

[0062] The parameter statement is "(String)*". You can use spaces as separators to get a list set of length 8 and a parameter set of length 1. The missing parts can be used. Fill it up and get:

[0063] ['select','accno','from','table','where','cardno','=','? '],['(String)*']

[0064] Create a dictionary, and add dictionary values ​​to newly appearing words in turn to enrich the dictionary, as follows:

[0065] {' ':0,"select':1,'delete':2,'update':3,'cardno':4,

[0066] 'from':5,'where':6,'order':7,'group':8,'by':9,'accno':10,

[0067] '=':11,'table':12,'?':13,'and':14,'date':15,'(String)*':16}, convert text to numerical information such as: [1,10,5,12,6,4,11,13],[0,0,0,0,0,0,0,0,16].

[0068] Furthermore, the data information corresponding to the text sentence is input into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence. Specifically, the prediction model is established using a convolutional neural network to predict whether the text sentence is an SQL statement, that is, the prediction result is an SQL statement or a non-SQL statement.

[0069] Step S3: If the statement prediction result is an SQL statement, a complete SQL statement is generated according to the corresponding text statement and the input parameters.

[0070] If the statement prediction result is an SQL statement, a complete SQL statement is obtained by splicing the text statement and the type of the passed parameter. Specifically, since there are 12 basic types of SQL fields, parameters can be configured in advance according to the type. For example, for String type, you can set the parameter to "abc" etc., for Integer type, you can set it to 1 etc., and for Date type, you can set it to now() etc.

[0071] Step S4: Obtain the execution plan result corresponding to the complete SQL statement, and generate a statement check result based on the statement prediction result and the execution plan result corresponding to the complete SQL statement.

[0072] Among them, the corresponding database information of each application is obtained, including URL, user, password, etc., and the execution plan result of the SQL statement is obtained in combination with the complete SQL statement. The execution plan result refers to the description of the execution process or access path of a SQL statement. The performance problem of the SQL statement can be analyzed through the execution plan result.

[0073] Specifically, in practice, the results can be obtained by executing the Explain+sql statement through the tool. Here, the program code can be used to obtain the results, as follows:

[0074] Class.foeName("com.mysql.jdbc.Driver"); / / Load the driver

[0075] Connection conn = DriverManager.getConnection(url, username, password); / / Establish database connection and obtain connection object

[0076] Statement stat = conn.createStatement(); / / Create a Statement object

[0077] ResultSet rs = stat.executeQuery (Explain + sql statement); / / Get the sql execution plan results

[0078] Furthermore, for an SQL statement in which the access type in the execution plan result is All (full table scan) or the index key is null, the generated statement check result is an abnormal SQL statement.

[0079] As an embodiment of the present invention, Figure 2 As shown, according to the text statement and the input parameters, the data information corresponding to the text statement is obtained, including:

[0080] Step S21, preprocessing the text statement and the input parameters; wherein the preprocessing includes text filling and format conversion;

[0081] Step S22, performing text conversion on the preprocessed text sentence and the input parameters according to a preset dictionary to obtain data information corresponding to the text sentence.

[0082] Among them, the text sentences and input parameters are preprocessed, including text filling and format conversion, and the preprocessed text sentences and input parameters are input and stored. Specifically, the text sentences are separated by spaces and stored in the list list, each short text is processed to the same length, and the missing parts can be filled with a specific text. Then, a dictionary is created or an already created dictionary is used to attach an integer number to each word in the text sentence. Furthermore, the text information is replaced with numerical information according to the dictionary number to obtain the data information corresponding to the text sentence.

[0083] Specifically, suppose there is a statement:

[0084] select accno from table where cardno=?

[0085] Params:(String)*

[0086] The parameter statement is "(String)*". You can use spaces as separators to get a list set of length 8 and a parameter set of length 1. The missing parts can be used. Fill it up and get:

[0087] ['select','accno','from','table','where','cardno','=','? '],['(String)*']

[0088] Create a dictionary, and add dictionary values ​​to newly appearing words in turn to enrich the dictionary, as follows:

[0089] {' ':0,"select':1,'delete':2,'update':3,'cardno':4,

[0090] 'from':5,'where':6,'order':7,'group':8,'by':9,'accno':10,

[0091] '=':11,'table':12,'?':13,'and':14,'date':15,'(String)*':16}, convert text to numerical information such as: [1,10,5,12,6,4,11,13],[0,0,0,0,0,0,0,0,16].

[0092] As an embodiment of the present invention, Figure 3 As shown in Figure 2, the prediction model is established in the following way:

[0093] Step S31, obtaining a historical log file and its corresponding prediction result, extracting statements from the historical log file to obtain historical data; wherein the historical data includes historical statements and historical parameters;

[0094] Step S32, obtaining historical data information corresponding to the historical sentence according to the historical sentence and the historical parameter, and obtaining a training sample according to the prediction result corresponding to the historical log file and the historical data information;

[0095] Step S33: Use the training samples to train the initial convolutional neural network model to obtain the prediction model.

[0096] Among them, similar to the application log file processing process, historical log files are obtained through programs or manual methods, and regular expressions are used to extract historical statements and historical parameters in historical log files, and useless statements are deleted, such as time nodes in the logs.

[0097] Furthermore, the statements in the historical log files are extracted to obtain historical data, which includes data information of historical statements and historical parameters. The prediction results and historical data information corresponding to the historical log files are processed by feature extraction and other processing to obtain training samples. Specifically, the training samples include SQL regular add, delete, modify, and query operation statements, and the input parameter types include String, Integer, Date, Long, etc. The initial convolutional neural network model is trained using the training samples to obtain a prediction model.

[0098] As an embodiment of the present invention, the method further includes: classifying statements of the application log file according to the statement prediction results; wherein the statement prediction results also include non-SQL statements.

[0099] Among them, SQL statements with abnormal execution plan results are classified as abnormal SQL statements, and non-SQL statements in statement prediction results are classified as abnormal SQL statements. The prediction model is trained again using abnormal SQL statements, and the model is updated through self-learning optimization.

[0100] Furthermore, SQL statements with execution errors will be automatically output to another file and marked as abnormal SQL statements. After the scan is completed, the statements in this file will be used as negative samples for self-learning to train an updated model.

[0101] In a specific embodiment of the present invention, Figure 4 The flowchart of the SQL statement checking method in a specific embodiment of the present invention is shown, which specifically includes:

[0102] 1. Since there is a lot of log data at present, the logs generated in the application test environment are generally cleared daily. Therefore, the log files can be obtained manually or by program at regular intervals, and the log statements in the log files can be extracted using regular expressions. At the same time, the input parameters such as (String)123 and (Integer)12 can be replaced with (String)* and (Integer)* using regular expressions.

[0103] 2. Data preprocessing: collect SQL statements and input parameters as raw data, as the text SQL data set to be processed. After preprocessing and format conversion according to SQL statements and parameters, input and store. Specifically, the text is separated by spaces and stored in the list list. First, each short text is processed to the same length. The missing part can be filled with a specific text. Then a dictionary is created, and an integer number is attached to each word. Finally, the text information is replaced with numerical information according to the dictionary number.

[0104] Specifically, suppose there is a statement

[0105] select accno from table where cardno=?

[0106] Params:(String)*

[0107] The parameter statement is "(String)*". You can use spaces as separators to get a list set of length 8 and a parameter set of length 1. The missing parts can be used. Fill it up and get:

[0108] ['select','accno','from','table','where','cardno','=','? '],['(String)*']

[0109] Create a dictionary, and add dictionary values ​​to newly appearing words in turn to enrich the dictionary, as follows:

[0110] {' ':0,"select':1,'delete':2,'update':3,'cardno':4,

[0111] 'from':5,'where':6,'order':7,'group':8,'by':9,'accno':10,

[0112] '=':11,'table':12,'?':13,'and':14,'date':15,'(String)*':16}, the text is converted into numerical information as follows: [1,10,5,12,6,4,11,13],[0,0,0,0,0,0,0,16].

[0113] 3. Use convolutional neural network to preprocess the collected data samples to obtain numerical information and then conduct training. The data samples include SQL regular add, delete, modify, and query statements. The input parameter types include String, Integer, Date, Long, etc. The model training diagram is as follows: Figure 5 As shown, the process is as follows Figure 6 shown.

[0114] Step 3.1: According to the processing in step 2, the input data is converted into a fixed length m, and the number of words involved in the text is n, so the data format after the embedding layer is processed is [n,m]. Set s filters to perform convolution operations with the data to obtain a feature map.

[0115] Specifically, the fixed length of the embedding layer is set to m=5. For example, the SQL statement in the above example involves 9 words, so the matrix data of size [9,5] can be obtained. At this time, the width can be set to be consistent with the number of data columns, and the heights can be set to 4, 3, and 2 respectively, with 6 filters filter1, filter2, and filter3, as follows:

[0116]

[0117] When the step size is set to 1, the convolution operation outputs feature maps of [4, 1] [5, 1] ​​[6, 1] respectively.

[0118] Step 3.2: Perform feature extraction. To reduce the size of the matrix, compress the matrix data and retain important information. Here, a pooling layer is added. At the same time, to reduce the influence of the increase in the variance of the estimated value caused by the limited neighborhood size and the deviation of the estimated mean caused by the error of the convolution layer parameters, random pooling is used. The specific process is as follows:

[0119] Step 3.2.1: Divide the elements in the matrix by the sum of the elements to obtain a probability matrix, in which each element value represents the probability of the value at the corresponding position;

[0120] Step 3.2.2: Randomly select elements in the matrix according to probability, and the pooling result is the element value corresponding to the original matrix.

[0121] Step 3.3: Add a fully connected layer, flatten the two-dimensional matrix data obtained in step 3.2 to obtain a one-dimensional matrix data, then multiply it by the weight weight and sum it, add the bias σ, and process it through the activation function. Here, the Relu activation function is used. The gradient descent method optimizes the weights, and finally the data is normalized to output the probability of whether it is a SQL statement, as shown in the formula, where x represents the sample vector, j represents the classification category, and K represents the number of output values.

[0122]

[0123] 4. Scan the application log file to convert each line of text information in the file into numerical information, and predict the numerical information based on the prediction model obtained in step 3. For example:

[0124] Select a from table where col1=? and col2=?

[0125] (String)*,(Integer)*

[0126] At this time, the prediction result is an SQL statement, and the SQL statement is: Select a from table where col1=(String)*and col2=(Integer)*. The statement can be replaced by the program code to obtain the complete SQL statement: Select a from table where col1=”123”and col2=123, and then the SQL execution plan result is obtained. For SQL statements with access type type All (full table scan) or index key null, they will be output to the result file. The program code is as follows:

[0127] Class.forName("com.mysql.jdbc.Driver"); / / Load the driver

[0128] Connection conn = DriverManager.getConnection(url, username, password); / / Establish database connection and obtain connection object

[0129] Statement stat = conn.createStatement(); / / Create a Statement object

[0130] ResultSet rs = stat.executeQuery (Explain + sql statement); / / Get the sql execution plan results

[0131] 5. The statements with abnormal execution plan results in step 4 are classified as abnormal SQL statements, and trained again to obtain a new model through self-learning optimization.

[0132] The present invention obtains text statements by analyzing application log files, and predicts the text statements in combination with prediction models and execution plan results, so as to judge whether the statements in the application log files are non-SQL statements, thereby reducing the hidden dangers of full table scans caused by them, and at the same time getting rid of the configuration dependence of static code scanning. Compared with dynamic monitoring, it is more targeted and reduces the false alarm rate.

[0133] like Figure 7 FIG. 1 is a schematic diagram of the structure of a SQL statement checking device according to an embodiment of the present invention. The device shown in the figure includes:

[0134] The statement extraction module 10 is used to obtain an application log file and perform statement extraction on the application log file to obtain text data; wherein the text data includes text statements and input parameters.

[0135] The application log file is obtained from the server where the application is deployed through a program or manually, and the text statements and input parameters in the log file generated by the application are extracted using regular expressions, and useless statements are deleted, such as time nodes in the log.

[0136] Furthermore, the statements in the application log file are extracted to obtain text data, which includes text statements and data information of input parameters. The text data is specifically as follows:

[0137] Select a from table where col1=? and col2=?

[0138] Params:(String)c1,(Integer)2

[0139] Among them, c1 and 2 are the input parameters, and the input types are String and Integer.

[0140] The prediction result module 20 is used to obtain data information corresponding to the text sentence according to the text sentence and the input parameters, and input the data information corresponding to the text sentence into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence.

[0141] Among them, the text sentences and input parameters are preprocessed, including text filling and format conversion, and the preprocessed text sentences and input parameters are input and stored. Specifically, the text sentences are separated by spaces and stored in the list list, each short text is processed to the same length, and the missing parts can be filled with a specific text. Then, a dictionary is created or an already created dictionary is used to attach an integer number to each word in the text sentence. Furthermore, the text information is replaced with numerical information according to the dictionary number to obtain the data information corresponding to the text sentence.

[0142] Specifically, suppose there is a statement:

[0143] select accno from table where cardno=?

[0144] Params:(String)*

[0145] The parameter statement is "(String)*". You can use spaces as separators to get a list set of length 8 and a parameter set of length 1. The missing parts can be used. Fill it up and get:

[0146] ['select','accno','from','table','where','cardno','=','? '],['(String)*']

[0147] Create a dictionary, and add dictionary values ​​to newly appearing words in turn to enrich the dictionary, as follows:

[0148] {' ':0,"select':1,'delete':2,'update':3,'cardno':4,

[0149] 'from':5,'where':6,'order':7,'group':8,'by':9,'accno':10,

[0150] '=':11,'table':12,'?':13,'and':14,'date':15,'(String)*':16}, convert text to numerical information such as: [1,10,5,12,6,4,11,13],[0,0,0,0,0,0,0,0,16].

[0151] Furthermore, the data information corresponding to the text sentence is input into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence. Specifically, the prediction model is established using a convolutional neural network to predict whether the text sentence is an SQL statement, that is, the prediction result is an SQL statement or a non-SQL statement.

[0152] The complete statement module 30 is used to generate a complete SQL statement according to the corresponding text statement and input parameters if the statement prediction result is an SQL statement.

[0153] If the statement prediction result is an SQL statement, a complete SQL statement is obtained by splicing the text statement and the type of the passed parameter. Specifically, since there are 12 basic types of SQL fields, parameters can be configured in advance according to the type. For example, for String type, you can set the parameter to "abc", etc., for Integer type, you can set it to 1, etc., for Date type, you can set it to now(), etc.

[0154] The check result module 40 is used to obtain the execution plan result corresponding to the complete SQL statement, and generate a statement check result according to the statement prediction result and the execution plan result corresponding to the complete SQL statement.

[0155] Among them, the corresponding database information of each application is obtained, including URL, user, password, etc., and the execution plan result of the SQL statement is obtained in combination with the complete SQL statement. The execution plan result refers to the description of the execution process or access path of a SQL statement. The performance problem of the SQL statement can be analyzed through the execution plan result.

[0156] Specifically, in practice, the results can be obtained by executing the Explain+sql statement through the tool. Here, the program code can be used to obtain the results, as follows:

[0157] Class.foeName("com.mysql.jdbc.Driver"); / / Load the driver

[0158] Connection conn = DriverManager.getConnection(url, username, password); / / Establish database connection and obtain connection object

[0159] Statement stat = conn.createStatement(); / / Create a Statement object

[0160] ResultSet rs = stat.executeQuery (Explain + sql statement); / / Get the sql execution plan results

[0161] Furthermore, for an SQL statement in which the access type in the execution plan result is All (full table scan) or the index key is null, the generated statement check result is an abnormal SQL statement.

[0162] As an embodiment of the present invention, Figure 8 As shown, the prediction result module 20 includes:

[0163] A preprocessing unit 21, used for preprocessing the text sentence and the input parameter; wherein the preprocessing includes text filling and format conversion;

[0164] The data information unit 22 is used to perform text conversion on the preprocessed text sentence and the input parameters according to a preset dictionary to obtain data information corresponding to the text sentence.

[0165] As an embodiment of the present invention, Fig. 9 As shown, the device also includes:

[0166] The historical data module 50 is used to obtain the historical log file and its corresponding prediction results, extract the statements from the historical log file, and obtain the historical data; wherein the historical data includes historical statements and historical parameters;

[0167] A training sample module 60, for obtaining historical data information corresponding to the historical sentence according to the historical sentence and the historical parameter, and obtaining a training sample according to the prediction result corresponding to the historical log file and the historical data information;

[0168] The prediction model module 70 is used to train the initial convolutional neural network model using the training samples to obtain the prediction model.

[0169] As an embodiment of the present invention, Fig.10 As shown, the device also includes:

[0170] The statement classification module 80 is used to classify the application log file according to the statement prediction result; wherein the statement prediction result also includes non-SQL statements.

[0171] Based on the same application concept as the above-mentioned SQL statement checking method, the present invention also provides the above-mentioned SQL statement checking device. Since the principle of solving the problem by the SQL statement checking device is similar to that of the SQL statement checking method, the implementation of the SQL statement checking device can refer to the implementation of the SQL statement checking method, and the repeated parts will not be repeated.

[0172] The present invention obtains text statements by analyzing application log files, and predicts the text statements in combination with prediction models and execution plan results, so as to judge whether the statements in the application log files are non-SQL statements, thereby reducing the hidden dangers of full table scans caused by them, and at the same time getting rid of the configuration dependence of static code scanning. Compared with dynamic monitoring, it is more targeted and reduces the false alarm rate.

[0173] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above method when executing the program.

[0174] The present invention also provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program for executing the above method.

[0175] like Fig.11 As shown, the electronic device 600 may further include: a communication module 110, an input unit 120, an audio processing unit 130, a display 160, and a power supply 170. It is worth noting that the electronic device 600 does not necessarily have to include Fig.11 In addition, the electronic device 600 may also include Fig.11 For components not shown, reference may be made to the prior art.

[0176] like Fig.11 As shown, the central processor 100 is sometimes also referred to as a controller or an operation control, and may include a microprocessor or other processor devices and / or logic devices. The central processor 100 receives inputs and controls the operations of various components of the electronic device 600.

[0177] The memory 140 may be, for example, one or more of a cache, a flash memory, a hard drive, a removable medium, a volatile memory, a non-volatile memory or other suitable devices. The above-mentioned information related to the failure may be stored, and a program for executing the relevant information may also be stored. The CPU 100 may execute the program stored in the memory 140 to implement information storage or processing.

[0178] The input unit 120 provides input to the CPU 100. The input unit 120 is, for example, a key or a touch input device. The power supply 170 is used to provide power to the electronic device 600. The display 160 is used to display display objects such as images and text. The display may be, for example, an LCD display, but is not limited thereto.

[0179] The memory 140 may be a solid-state memory, such as a read-only memory (ROM), a random access memory (RAM), a SIM card, etc. It may also be a memory that saves information even when the power is off, can be selectively erased, and is provided with more data, examples of which are sometimes referred to as EPROMs, etc. The memory 140 may also be some other type of device. The memory 140 includes a buffer memory 141 (sometimes referred to as a buffer). The memory 140 may include an application / function storage unit 142, which is used to store application programs and function programs or processes for executing the operation of the electronic device 600 through the central processor 100.

[0180] The memory 140 may also include a data storage unit 143 for storing data, such as contacts, digital data, pictures, sounds, and / or any other data used by the electronic device. The driver storage unit 144 of the memory 140 may include various drivers for communication functions of the electronic device and / or for executing other functions of the electronic device (such as messaging applications, address book applications, etc.).

[0181] The communication module 110 is a transmitter / receiver 110 that transmits and receives signals via an antenna 111. The communication module (transmitter / receiver) 110 is coupled to the central processor 100 to provide input signals and receive output signals, which may be the same as the case of a conventional mobile communication terminal.

[0182] Based on different communication technologies, multiple communication modules 110 may be provided in the same electronic device, such as a cellular network module, a Bluetooth module and / or a wireless LAN module. The communication module (transmitter / receiver) 110 is also coupled to a speaker 131 and a microphone 132 via an audio processor 130 to provide an audio output via the speaker 131 and receive an audio input from the microphone 132, thereby realizing a common telecommunication function. The audio processor 130 may include any suitable buffer, decoder, amplifier, etc. In addition, the audio processor 130 is also coupled to the central processor 100, so that the sound can be recorded on the local machine through the microphone 132, and the sound stored on the local machine can be played through the speaker 131.

[0183] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0184] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0185] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0186] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0187] The present invention uses specific embodiments to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of ​​the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.

Claims

1. A method for checking SQL statements, characterized in that: The method comprises: Obtaining an application log file, and extracting statements from the application log file to obtain text data; wherein the text data includes text statements and input parameters; According to the text sentence and the input parameters, data information corresponding to the text sentence is obtained, and the data information corresponding to the text sentence is input into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence; If the statement prediction result is an SQL statement, a complete SQL statement is generated according to the corresponding text statement and the input parameters; The execution plan result corresponding to the complete SQL statement is obtained, and a statement checking result is generated according to the statement prediction result and the execution plan result corresponding to the complete SQL statement.

2. The method according to claim 1, characterized in that The obtaining of data information corresponding to the text statement according to the text statement and the input parameter includes: Preprocessing the text statement and the input parameters; wherein the preprocessing includes text filling and format conversion; According to a preset dictionary, text conversion is performed on the preprocessed text sentence and the input parameters to obtain data information corresponding to the text sentence.

3. The method according to claim 1, characterized in that The prediction model is established in the following way: Obtaining a historical log file and its corresponding prediction results, extracting statements from the historical log file to obtain historical data; wherein the historical data includes historical statements and historical parameters; According to the historical sentences and the historical parameters, historical data information corresponding to the historical sentences is obtained, and training samples are obtained according to the prediction results corresponding to the historical log files and the historical data information; The initial convolutional neural network model is trained using the training samples to obtain the prediction model.

4. The method according to claim 1, characterized in that: The method further comprises: The application log file is classified into statements according to the statement prediction results; wherein the statement prediction results also include non-SQL statements.

5. A SQL statement checking device, characterized in that: The device comprises: A statement extraction module is used to obtain an application log file and perform statement extraction on the application log file to obtain text data; wherein the text data includes text statements and input parameters; A prediction result module, used to obtain data information corresponding to the text sentence according to the text sentence and the input parameters, and input the data information corresponding to the text sentence into a pre-established prediction model to obtain a sentence prediction result corresponding to the text sentence; A complete statement module, used to generate a complete SQL statement according to the corresponding text statement and input parameters if the statement prediction result is an SQL statement; The checking result module is used to obtain the execution plan result corresponding to the complete SQL statement, and generate a statement checking result according to the statement prediction result and the execution plan result corresponding to the complete SQL statement.

6. The device according to claim 5, characterized in that The prediction result module comprises: A preprocessing unit, used for preprocessing the text sentence and the input parameter; wherein the preprocessing includes text filling and format conversion; The data information unit is used to perform text conversion on the preprocessed text sentences and input parameters according to a preset dictionary to obtain data information corresponding to the text sentences.

7. The device according to claim 5, characterized in that The device also includes: A historical data module is used to obtain historical log files and their corresponding prediction results, extract statements from the historical log files, and obtain historical data; wherein the historical data includes historical statements and historical parameters; A training sample module, used to obtain historical data information corresponding to the historical sentence according to the historical sentence and the historical parameter, and obtain a training sample according to the prediction result corresponding to the historical log file and the historical data information; The prediction model module is used to train the initial convolutional neural network model using the training samples to obtain the prediction model.

8. The device according to claim 5, characterized in that The device also includes: A statement classification module is used to classify the application log file according to the statement prediction result; wherein the statement prediction result also includes non-SQL statements.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method according to any one of claims 1 to 4 is implemented.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for executing the method according to any one of claims 1 to 4.

Citation Information

Patent Citations

  • OLAP (on-line analytical processing) data model automatic modeling method and classifier

    CN108153894A

  • Database anomaly detection method based on cyclic marking time point process

    CN110580213A

  • Inception-based database operation and maintenance method, device, computer equipment and storage medium

    CN110716922A

  • SQL statement verification method and device, data acquisition method and device and storage device

    CN111857721A

  • SQL risk identification method, device and equipment

    CN112784273A