Data query method and device, storage medium and electronic equipment
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-05-23
- Publication Date
- 2026-08-11
AI Technical Summary
[0005]本申请的主要目的在于提供一种数据的查询方法及装置、存储介质和电子设备,以解决相关技术中在数据库中以时间字段查询数据时,会导致出现全表扫描的情况,从而导致数据库资源占用率过高,进而会导致查询数据的效率较低的问题
[0022] This application employs the following steps: receiving a first statement sent by a target object, wherein the first statement is used to query target data in the database using a time field; parsing the first statement to obtain a parsing result; checking whether a target function exists in the first statement based on the parsing result, and obtaining a check result, wherein the target function is a function for obtaining timestamps in the database; and querying the target data in the database using the time field based on the check result. This solves the problem in related technologies where querying data in the database using a time field can lead to a full table scan, resulting in excessive database resource consumption and consequently low query efficiency. By parsing the first statement used to query target data in the database using a time field, obtaining a parsing result, checking whether a target function for obtaining timestamps in the first statement exists in the database based on the parsing result, obtaining a check result, and then querying the target data in the database using the time field based on the check result, a full table scan is avoided when querying data in the database using a time field, thereby reducing database resource consumption and improving query efficiency.
Smart Images

Figure CN116628006B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of big data technology, and more specifically, to a data query method and apparatus, storage medium and electronic device. Background Technology
[0002] When writing SQL (Structured Query Language) queries, it's sometimes necessary to query data using time fields. In related technologies, if the query uses the `unix_timestamp()` function (a database function for retrieving timestamps) as a filter condition, and the scanned partition field doesn't match a timestamp in `unix_timestamp()`, a full table scan will occur. This is because `unix_timestamp()` is an indeterminate function that scans the entire table to retrieve data. Furthermore, a full table scan is a complete process that involves a large amount of data completely unrelated to the result. Additionally, whether in MySQL or Hive databases, using the indeterminate `unix_timestamp()` function will always result in a full table scan. Therefore, using `unix_timestamp()` in SQL consumes significant database resources, leading to high resource usage and impacting database response speed. In severe cases, it can cause lag in applications using the database.
[0003] Furthermore, in today's data-driven era, the big data field is booming. More and more enterprises, especially medium and large-sized ones, are developing sophisticated methods to optimize SQL (Structured Query Language) query times, considering the time required for data output. Moreover, many enterprises have numerous database-based data analysis needs daily. If a large number of SQL queries trigger the unix_timestamp() function, it will inevitably impact the efficiency of data retrieval and the overall efficiency of data analysis.
[0004] There is currently no effective solution to the problem that querying data in a database using a time field can lead to a full table scan, resulting in high database resource consumption and low query efficiency. Summary of the Invention
[0005] The main objective of this application is to provide a data query method, apparatus, storage medium, and electronic device to solve the problem in related technologies where querying data in a database using a time field results in a full table scan, leading to excessive database resource consumption and consequently low data query efficiency.
[0006] To achieve the above objectives, according to one aspect of this application, a data query method is provided. The method includes: receiving a first statement sent by a target object, wherein the first statement is used to query target data in a database using a time field; parsing the first statement to obtain a parsing result; checking whether a target function exists in the first statement based on the parsing result, obtaining a check result, wherein the target function is a function for obtaining timestamps from the database; and querying the target data in the database using the time field based on the check result.
[0007] Further, checking whether the target function exists in the first statement based on the parsing result, and obtaining the check result includes: obtaining the abstract syntax tree corresponding to the first statement based on the parsing result; traversing the nodes of the abstract syntax tree to obtain the traversal result; checking the first statement based on the traversal result to obtain the check result, wherein the check result is used to indicate whether the target function exists in the first statement.
[0008] Further, checking the first statement based on the traversal result to obtain the check result includes: determining whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; if the target node does not exist in the abstract syntax tree, then executing the first statement and querying the target data in the database using the time field; if the target node exists in the abstract syntax tree, then obtaining N functions in the target node, wherein N is a positive integer; checking the first statement based on the N functions to obtain the check result.
[0009] Further, the inspection of the first statement based on the N functions to obtain the inspection result includes: determining whether the target function exists among the N functions; if the target function exists among the N functions, it means that the target function exists in the first statement; if the target function does not exist among the N functions, it means that the target function does not exist in the first statement.
[0010] Further, querying the target data in the database using a time field based on the inspection result includes: if the inspection result indicates that the target function does not exist in the first statement, then executing the first statement and querying the target data in the database using a time field; if the inspection result indicates that the target function exists in the first statement, then determining whether the target function has parameters and obtaining a determination result; and querying the target data in the database using a time field based on the determination result.
[0011] Further, querying the target data in the database using the time field based on the judgment result includes: if the judgment result indicates that the target function contains the parameter, then executing the first statement and querying the target data in the database using the time field; if the judgment result indicates that the target function does not contain the parameter, then sending a reminder message to the target object, wherein the reminder message is used to remind the target object to add the parameter in the target function; obtaining the second statement after the target object adds the parameter in the target function; executing the second statement and querying the target data in the database using the time field.
[0012] Furthermore, after parsing the first statement and obtaining the parsing result, the method further includes: determining whether the first statement was successfully parsed based on the parsing result; if the first statement was successfully parsed, obtaining the abstract syntax tree corresponding to the first statement; if the first statement failed to be parsed, executing the first statement and querying the target data in the database using the time field.
[0013] To achieve the above objectives, according to another aspect of this application, a data query apparatus is provided. The apparatus includes: a first receiving unit for receiving a first statement sent by a target object, wherein the first statement is used to query target data in a database using a time field; a first parsing unit for parsing the first statement to obtain a parsing result; a first checking unit for checking whether a target function exists in the first statement based on the parsing result, and obtaining a checking result, wherein the target function is a function for obtaining timestamps from the database; and a first query unit for querying the target data in the database using a time field based on the checking result.
[0014] Further, the first checking unit includes: a first determining subunit, used to obtain the abstract syntax tree corresponding to the first statement based on the parsing result; a first traversal subunit, used to traverse the nodes of the abstract syntax tree to obtain a traversal result; and a first checking subunit, used to check the first statement based on the traversal result to obtain the checking result, wherein the checking result is used to indicate whether the target function exists in the first statement.
[0015] Further, the first checking subunit includes: a first determining module, configured to determine whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; a first execution module, configured to execute the first statement and query the target data in the database using a time field if the target node does not exist in the abstract syntax tree; a first obtaining module, configured to obtain N functions in the target node if the target node exists in the abstract syntax tree, wherein N is a positive integer; and a first checking module, configured to check the first statement based on the N functions to obtain the checking result.
[0016] Further, the first checking module includes: a first judging submodule, used to judge whether the target function exists among the N functions; a first determining submodule, used to indicate that the target function exists in the first statement if the target function exists among the N functions; and a second determining submodule, used to indicate that the target function does not exist in the first statement if the target function does not exist among the N functions.
[0017] Further, the first query unit includes: a first execution subunit, configured to execute the first statement and query the target data in the database using a time field if the check result indicates that the target function does not exist in the first statement; a first judgment subunit, configured to determine whether the target function has parameters if the check result indicates that the target function exists in the first statement, and obtain a judgment result; and a first query subunit, configured to query the target data in the database using a time field based on the judgment result.
[0018] Further, the first query subunit includes: a second execution module, configured to execute the first statement and query the target data in the database using a time field if the judgment result indicates that the target function contains the parameter; a first sending module, configured to send a reminder message to the target object if the judgment result indicates that the target function does not contain the parameter, wherein the reminder message is used to remind the target object to add the parameter in the target function; a second acquisition module, configured to acquire the second statement after the target object adds the parameter in the target function; and a third execution module, configured to execute the second statement and query the target data in the database using a time field.
[0019] Furthermore, the apparatus further includes: a first determining unit, configured to determine whether the parsing of the first statement was successful based on the parsing result after parsing the first statement; a second determining unit, configured to obtain the abstract syntax tree corresponding to the first statement if the parsing of the first statement is successful; and a first execution unit, configured to execute the first statement and query the target data in the database using the time field if the parsing of the first statement fails.
[0020] To achieve the above objectives, according to another aspect of this application, a computer-readable storage medium is provided, the storage medium storing a program, wherein the program executes the data query method described in any of the above claims.
[0021] To achieve the above objectives, according to another aspect of this application, an electronic device is provided, the electronic device including one or more processors and a memory, the memory being used to store one or more programs, wherein, when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the data query method described in any of the above.
[0022] This application employs the following steps: receiving a first statement sent by a target object, wherein the first statement is used to query target data in the database using a time field; parsing the first statement to obtain a parsing result; checking whether a target function exists in the first statement based on the parsing result, and obtaining a check result, wherein the target function is a function for obtaining timestamps in the database; and querying the target data in the database using the time field based on the check result. This solves the problem in related technologies where querying data in the database using a time field can lead to a full table scan, resulting in excessive database resource consumption and consequently low query efficiency. By parsing the first statement used to query target data in the database using a time field, obtaining a parsing result, checking whether a target function for obtaining timestamps in the first statement exists in the database based on the parsing result, obtaining a check result, and then querying the target data in the database using the time field based on the check result, a full table scan is avoided when querying data in the database using a time field, thereby reducing database resource consumption and improving query efficiency. Attached Figure Description
[0023] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments and descriptions of this application are used to explain this application and do not constitute an undue limitation of this application. In the drawings:
[0024] Figure 1 This is a flowchart of a data query method provided according to an embodiment of this application;
[0025] Figure 2 This is a flowchart of the data query method provided in the embodiments of this application. Figure 1 ;
[0026] Figure 3 This is a flowchart of an optional data query method provided according to an embodiment of this application;
[0027] Figure 4 This is a schematic diagram of a data query device provided according to an embodiment of this application;
[0028] Figure 5 This is a schematic diagram of an electronic device provided according to an embodiment of this application. Detailed Implementation
[0029] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. This application will now be described in detail with reference to the accompanying drawings and embodiments.
[0030] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present application, and not all embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of the present application.
[0031] It should be noted that the terms "first," "second," etc., in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate for the embodiments of this application described herein. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0032] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.
[0033] For ease of description, the following explains some of the nouns or terms used in the embodiments of this application:
[0034] The unix_timestamp function: This function retrieves the timestamp from the database. If no parameters are added to the function, it retrieves the current timestamp. If a string date parameter (a custom timestamp parameter) is added to the function, it retrieves the timestamp corresponding to the parameter.
[0035] SQL (Structured Query Language) is a standard language for managing relational databases. It allows users to insert, update, query, and delete data in a database. SQL's syntax is simple and easy to understand, making it easy to query and manage databases.
[0036] MySQL is an open-source relational database management system that supports the SQL language and can run on various operating system platforms.
[0037] Hive is an open-source database for storing and querying large amounts of data. Using Hive, you can store massive amounts of data in a file system and query it using SQL or embedded languages. Hive also provides a rich set of functions and aggregate functions for various data manipulations. Furthermore, Hive supports multiple caching mechanisms to improve query efficiency.
[0038] Oracle is a relational database management system. It is a high-performance and scalable database management system that can run on various operating system platforms.
[0039] An Abstract Syntax Tree (AST) is a tree model that represents the syntactic structure of a programming language. It is an abstract syntax tree that not only represents the syntactic structure of the source code but also the language's syntactic rules and the logical relationships between statements.
[0040] sqlnode is a syntax tree used to represent SQL query statements.
[0041] ANTLR (Antimalware Tool Rating) is a parser and security software evaluation tool. It uses a hybrid syntax and dynamic analysis to identify and evaluate the syntactic expressions and actions of various security software.
[0042] Javacc is a syntax parser that parses source code and can convert it into assembly code.
[0043] The present invention will now be described in conjunction with preferred implementation steps. Figure 1 This is a flowchart of a data query method provided according to an embodiment of this application, such as... Figure 1 As shown, the method includes the following steps:
[0044] Step S101: Receive the first statement sent by the target object, wherein the first statement is used to query the target data in the database using the time field.
[0045] For example, when a user (the target object mentioned above) is writing an SQL statement (the first statement mentioned above), and the SQL statement (the first statement mentioned above) is used to query data in the database (the target data mentioned above) using a time field, the device or apparatus can receive the SQL statement (the first statement mentioned above) submitted by the user (the target object mentioned above), and the SQL statement submitted by the user is used to query data in the database (the target data mentioned above) using a time field.
[0046] Step S102: Parse the first statement to obtain the parsing result.
[0047] For example, the parsing and judgment module in the device or apparatus can be responsible for parsing the SQL statements submitted by the user (the aforementioned target object).
[0048] Step S103: Check whether there is a target function in the first statement based on the parsing result, and obtain the check result. The target function is the function that retrieves the timestamp from the database.
[0049] For example, after parsing the SQL statement submitted by the user (the target object mentioned above), it can be determined whether the SQL statement submitted by the user (the target object mentioned above) used the unix_timestamp() function, and the unix_timestamp function is a function that retrieves timestamps in the database.
[0050] Step S104: Based on the inspection results, query the target data in the database using the time field.
[0051] For example, if the SQL statement submitted by the user (the target object mentioned above) uses the unix_timestamp() function, then it is further determined whether the unix_timestamp() function has parameters, and then the target data is queried in the database using the time field based on the result of the determination of whether the unix_timestamp() function has parameters; if the SQL statement submitted by the user (the target object mentioned above) does not use the unix_timestamp() function, then the SQL statement submitted by the user (the target object mentioned above) is executed, and the target data is queried in the database using the time field.
[0052] It should be noted that the data query method provided in this application embodiment can be applied to financial scenarios.
[0053] Through the above steps S101 to S104, the first statement used to query target data in the database using the time field is parsed to obtain the parsing result. Based on the parsing result, the first statement is checked to see if there is a target function for obtaining timestamps in the database. The check result is obtained, and then the target data is queried in the database using the time field based on the check result. This avoids the situation of full table scan when querying data in the database using the time field, thereby reducing the database resource usage and improving the efficiency of data query.
[0054] Optionally, in the data query method provided in the embodiments of this application, after parsing the first statement and obtaining the parsing result, the method further includes: determining whether the first statement was successfully parsed based on the parsing result; if the first statement was successfully parsed, obtaining the abstract syntax tree corresponding to the first statement; if the first statement was not parsed, executing the first statement and querying the target data in the database using the time field.
[0055] For example, the parsing and judgment module can be responsible for parsing the SQL statement (the first statement mentioned above) submitted by the user (the target object). Furthermore, this module can use a general-purpose parser to receive and parse the SQL statement (the first statement mentioned above) submitted by the user (the target object), obtaining an abstract syntax tree. The parser can also support syntax extensions to accommodate the parsing needs of different data sources such as MySQL, Oracle, and Hive databases. For instance, open-source parsers like ANTLR and Javacc can achieve multi-data source syntax support by extending syntax rule files. Calcite (an open-source dynamic data management framework) and Druid (a distributed data analysis platform, time-series database, or cluster system) have built-in programmable parsers. In other words, the parsing and judgment module can parse the SQL statement (the first statement mentioned above) passed by the caller (the target object mentioned above) through the syntax parser; if the parsing is successful, the abstract syntax tree corresponding to the SQL statement (the first statement mentioned above) is obtained; if the parsing fails, it is allowed directly. That is, if the parsing fails, the SQL statement (the first statement mentioned above) submitted by the user (the target object mentioned above) can be executed directly, and the target data mentioned above can be queried in the database using the time field by executing the SQL statement (the first statement mentioned above) submitted by the user (the target object mentioned above).
[0056] The above method can quickly and accurately parse the SQL statements submitted by users.
[0057] Optionally, in the data query method provided in this application embodiment, checking whether a target function exists in the first statement based on the parsing result and obtaining the check result includes: obtaining the abstract syntax tree corresponding to the first statement based on the parsing result; traversing the nodes of the abstract syntax tree to obtain the traversal result; checking the first statement based on the traversal result to obtain the check result, wherein the check result is used to indicate whether a target function exists in the first statement.
[0058] For example, the parsing and judgment module can be responsible for parsing the SQL statement (the first statement mentioned above) submitted by the user (the target object), and after obtaining the abstract syntax tree (Abstract Syntax Tree), it can traverse all nodes of the Abstract Syntax Tree to filter out problematic SQL statements. For instance, when the parsing and judgment module parses the SQL statement (the first statement mentioned above) passed by the caller (the target object), and the parsing of the SQL statement (the first statement mentioned above) is successful, it can obtain the corresponding Abstract Syntax Tree of the SQL statement (the first statement mentioned above). Then, it can recursively check all nodes of the passed SQL statement (the first statement mentioned above), and after recursively checking all nodes of the passed SQL statement (the first statement mentioned above), it can determine whether the SQL statement (the first statement mentioned above) submitted by the user (the target object) used the unix_timestamp() function to obtain the timestamp from the database.
[0059] Using the above method, the abstract syntax tree corresponding to the SQL statement submitted by the user can be obtained quickly and accurately, thereby enabling quick and accurate inspection of the nodes of the abstract syntax tree.
[0060] Optionally, in the data query method provided in this application embodiment, checking the first statement based on the traversal result to obtain the check result includes: determining whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; if the target node does not exist in the abstract syntax tree, then executing the first statement and querying the target data in the database using the time field; if the target node exists in the abstract syntax tree, then obtaining N functions in the target node, wherein N is a positive integer; checking the first statement based on the N functions to obtain the check result.
[0061] For example, when traversing the abstract syntax tree corresponding to the SQL statement (the first statement mentioned above), all WHERE nodes (nodes representing queried data, the target nodes mentioned above) in the abstract syntax tree can be recursively checked. If there is no WHERE node in the abstract syntax tree, it can be directly allowed. That is, if there is no WHERE node in the abstract syntax tree, the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above) can be directly executed, and the target data mentioned above can be queried in the database using the time field by executing the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above). If there is a WHERE node in the abstract syntax tree, multiple functions (the N functions mentioned above) in the WHERE node are obtained, and based on the multiple functions (the N functions mentioned above), it is checked whether the SQL statement submitted by the user (the target object mentioned above) uses the unix_timestamp() function.
[0062] The above method can quickly and accurately check whether there is a WHERE clause in the SQL statement submitted by the user for querying data.
[0063] Optionally, in the data query method provided in the embodiments of this application, the first statement is checked based on N functions to obtain the check result, which includes: determining whether a target function exists among the N functions; if a target function exists among the N functions, it means that a target function exists in the first statement; if a target function does not exist among the N functions, it means that a target function does not exist in the first statement.
[0064] For example, determine whether the above N functions involve the unix_timestamp() function; if the above N functions involve the unix_timestamp() function, it means that the SQL statement submitted by the user (the above target object) uses the unix_timestamp() function; if the above N functions do not involve the unix_timestamp() function, it means that the SQL statement submitted by the user (the above target object) does not use the unix_timestamp() function.
[0065] The above method can quickly and accurately check whether the SQL statement submitted by the user uses the unix_timestamp() function to obtain the timestamp in the database.
[0066] Figure 2 This is a flowchart of the data query method provided in the embodiments of this application. Figure 1 ,like Figure 2 As shown, in the data query method provided in this application embodiment, querying target data in the database using the time field based on the inspection results includes:
[0067] Step S201: If the check result indicates that the target function does not exist in the first statement, then execute the first statement and query the target data in the database using the time field;
[0068] Step S202: If the check result indicates that there is an objective function in the first statement, then determine whether there are parameters in the objective function and obtain the judgment result;
[0069] Step S203: Based on the judgment result, query the target data in the database using the time field.
[0070] For example, if the SQL statement submitted by the user (the target object mentioned above) does not use the unix_timestamp() function (the target function mentioned above), the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above) can be executed directly, and the target data can be queried in the database using the time field by executing the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above). If the SQL statement submitted by the user (the target object mentioned above) uses the unix_timestamp() function (the target function mentioned above), it is determined whether the unix_timestamp() function (the target function mentioned above) has parameters, and after determining whether the unix_timestamp() function (the target function mentioned above) has parameters, the target data can be queried in the database using the time field.
[0071] The above method can quickly and accurately check whether the unix_timestamp() function in the SQL statement submitted by the user has parameters.
[0072] Optionally, in the data query method provided in this application embodiment, querying target data in the database using the time field based on the judgment result includes: if the judgment result indicates that the target function has parameters, then executing the first statement and querying the target data in the database using the time field; if the judgment result indicates that the target function does not have parameters, then sending a reminder message to the target object, wherein the reminder message is used to remind the target object to add parameters to the target function; obtaining the second statement after the target object adds parameters to the target function; executing the second statement and querying the target data in the database using the time field.
[0073] For example, determine if the unix_timestamp() function (the target function mentioned above) has parameters. If parameters are added, the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above) can be executed directly, and the target data can be queried in the database using the time field by executing the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above). If no parameters are added, jump to the hit rules, add it to the hit list, and the SQL statement submitted by the user (the target object mentioned above) (the first statement mentioned above) is prohibited from execution. Rectification information for the SQL statement (the first statement mentioned above) can be sent to the user (the target object mentioned above). After the user adds parameters to the previously submitted SQL statement (the first statement mentioned above), the SQL statement with added parameters (the second statement mentioned above) is executed, and the target data can be queried in the database using the time field by executing the SQL statement with added parameters (the second statement mentioned above).
[0074] Using the above method, data can be quickly and accurately retrieved from the database using the time field.
[0075] For example, Figure 3 This is a flowchart of an optional data query method provided according to an embodiment of this application, such as... Figure 3 As shown, the optional data query methods include the following steps:
[0076] Step 301: The incoming task begins execution, that is, receiving the SQL statement submitted by the user.
[0077] Step 302: The parsing and judgment module is responsible for parsing the SQL statements submitted by the user, obtaining the sqlnode syntax tree, traversing all nodes, and filtering out the problematic SQL statements.
[0078] Furthermore, the parsing and judgment modules can use a general-purpose syntax parser to receive SQL statements and parse them into an abstract syntax tree. The parser also supports syntax extensions to accommodate the different syntaxes required by various data sources such as MySQL, Oracle, and Hive. For example, open-source syntax parsers like ANTLR and Javacc can achieve multi-datasource syntax support by extending syntax rule files. Calcite (an open-source dynamic data management framework) and Druid (a distributed data analysis platform, time-series database, or cluster system) have built-in programmable syntax parsers. After parsing the user-input SQL, the parser traverses all nodes.
[0079] In other words, step 302 can parse the SQL statement passed in by the caller. If parsing fails, proceed to step 303 and allow the request to proceed directly. If parsing succeeds, proceed to step 304.
[0080] Step 303: Allow the SQL statement to run, that is, execute the SQL statement directly.
[0081] Step 304: Recursively check all WHERE nodes in the input SQL (sqlnode syntax tree). If the input SQL has no WHERE node, proceed to step 305 and the SQL is allowed to run. If the input SQL has a WHERE node, proceed to step 306.
[0082] Step 305: Allow the SQL statement to run, that is, execute the SQL statement directly.
[0083] Step 306: Retrieve the function in the where node, then proceed to step 307.
[0084] Step 307: Determine if the function involves the unix_timestamp() function. If not, proceed to step 308 and iterate through the next where node. If it does, proceed to step 309.
[0085] Step 308: Check the next where node.
[0086] Step 309: Determine if the unix_timestamp() function has parameters. If parameters are added, proceed to step 310 to check the unix_timestamp() function in the next WHERE node. If no parameters are added, proceed to step 311 to match the rules, add the SQL to the match list, and prevent the SQL from being executed.
[0087] Step 310: Check the unix_timestamp() function of the next where node.
[0088] Step 311: Hit rules, add to the hit list.
[0089] The method provided in this application provides a method and apparatus for checking the unix_timestamp() function. It checks whether the unix_timestamp() function is used based on the submitted SQL. This apparatus provides a more flexible unix_timestamp() function scanning and checking.
[0090] Furthermore, the unix_timestamp() function checking method and apparatus provided in this embodiment parses the SQL passed in by the user, determines whether the unix_timestamp() function is used, and if it is used, the SQL is determined to have hit the rule, prohibiting the SQL from continuing to execute. Finally, the checking result is returned to the caller.
[0091] In summary, the data query method provided in this application embodiment receives a first statement sent by a target object, wherein the first statement is used to query target data in the database using a time field; parses the first statement to obtain a parsing result; checks whether a target function exists in the first statement based on the parsing result, obtaining a check result, wherein the target function is a function for obtaining timestamps in the database; and queries the target data in the database using the time field based on the check result. This solves the problem in related technologies where querying data in the database using a time field can lead to a full table scan, resulting in excessive database resource consumption and low data query efficiency. By parsing the first statement used to query target data in the database using a time field, obtaining a parsing result, checking whether a target function for obtaining timestamps in the first statement exists in the database based on the parsing result, obtaining a check result, and then querying the target data in the database using the time field based on the check result, the full table scan situation is avoided when querying data in the database using a time field, thereby reducing database resource consumption and improving data query efficiency.
[0092] It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0093] This application also provides a data query device. It should be noted that the data query device of this application can be used to execute the data query method provided in this application. The data query device provided in this application is described below.
[0094] Figure 4 This is a schematic diagram of a data query device according to an embodiment of this application. Figure 4 As shown, the device includes: a first receiving unit 401, a first parsing unit 402, a first checking unit 403, and a first querying unit 404.
[0095] Specifically, the first receiving unit 401 is used to receive a first statement sent by the target object, wherein the first statement is used to query target data in the database using a time field;
[0096] The first parsing unit 402 is used to parse the first statement and obtain the parsing result;
[0097] The first checking unit 403 is used to check whether there is a target function in the first statement based on the parsing result, and to obtain the checking result. The target function is a function that retrieves timestamps from the database.
[0098] The first query unit 404 is used to query target data in the database using the time field based on the inspection results.
[0099] In summary, the data query device provided in this application embodiment receives a first statement sent by a target object through a first receiving unit 401, wherein the first statement is used to query target data in the database using a time field; a first parsing unit 402 parses the first statement to obtain a parsing result; a first checking unit 403 checks whether a target function exists in the first statement based on the parsing result, and obtains a checking result, wherein the target function is a function for obtaining timestamps in the database; and a first query unit 404 queries the target data in the database using a time field based on the checking result. This solves the problem in related technologies where querying data in the database using a time field can lead to a full table scan, resulting in excessive database resource usage and consequently low data query efficiency. By parsing the first statement used to query target data in the database using a time field, obtaining a parsing result, checking whether a target function for obtaining timestamps in the database exists in the first statement based on the parsing result, obtaining a checking result, and then querying the target data in the database using a time field based on the checking result, a full table scan is avoided when querying data in the database using a time field, thereby reducing database resource usage and improving data query efficiency.
[0100] Optionally, in the data query device provided in the embodiments of this application, the first checking unit includes: a first determining subunit, used to obtain the abstract syntax tree corresponding to the first statement based on the parsing result; a first traversal subunit, used to traverse the nodes of the abstract syntax tree to obtain the traversal result; and a first checking subunit, used to check the first statement based on the traversal result to obtain the checking result, wherein the checking result is used to indicate whether there is an objective function in the first statement.
[0101] Optionally, in the data query device provided in this application embodiment, the first checking subunit includes: a first determining module, used to determine whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; a first execution module, used to execute a first statement and query the target data in the database using a time field if the target node does not exist in the abstract syntax tree; a first obtaining module, used to obtain N functions in the target node if the target node exists in the abstract syntax tree, wherein N is a positive integer; and a first checking module, used to check the first statement based on the N functions and obtain a checking result.
[0102] Optionally, in the data query device provided in the embodiments of this application, the first checking module includes: a first judging submodule, used to judge whether there is a target function among N functions; a first determining submodule, used to indicate that there is a target function in the first statement if there is a target function among N functions; and a second determining submodule, used to indicate that there is no target function in the first statement if there is no target function among N functions.
[0103] Optionally, in the data query device provided in this application embodiment, the first query unit includes: a first execution subunit, configured to execute the first statement and query the target data in the database using the time field if the check result indicates that the target function does not exist in the first statement; a first judgment subunit, configured to determine whether there are parameters in the target function and obtain a judgment result if the check result indicates that the target function exists in the first statement; and a first query subunit, configured to query the target data in the database using the time field based on the judgment result.
[0104] Optionally, in the data query device provided in this application embodiment, the first query subunit includes: a second execution module, configured to execute a first statement and query target data in the database using a time field if the judgment result indicates that the target function has parameters; a first sending module, configured to send a reminder message to the target object if the judgment result indicates that the target function has no parameters, wherein the reminder message is used to remind the target object to add parameters to the target function; a second acquisition module, configured to acquire a second statement after the target object adds parameters to the target function; and a third execution module, configured to execute the second statement and query target data in the database using a time field.
[0105] Optionally, in the data query device provided in the embodiments of this application, the device further includes: a first determining unit, used to determine whether the first statement was successfully parsed based on the parsing result after parsing the first statement; a second determining unit, used to obtain the abstract syntax tree corresponding to the first statement if the first statement was successfully parsed; and a first execution unit, used to execute the first statement and query the target data in the database using the time field if the first statement was not parsed.
[0106] The data query device includes a processor and a memory. The first receiving unit 401, the first parsing unit 402, the first checking unit 403, and the first query unit 404 are all stored in the memory as program units. The processor executes the program units stored in the memory to realize the corresponding functions.
[0107] The processor contains a kernel, which retrieves the corresponding program units from memory. One or more kernels can be configured, and the efficiency of data retrieval can be improved by adjusting kernel parameters.
[0108] The memory may include non-permanent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.
[0109] This invention provides a computer-readable storage medium storing a program thereon, which, when executed by a processor, implements a method for querying the data.
[0110] This invention provides a processor for running a program, wherein the program executes a data query method during runtime.
[0111] like Figure 5 As shown, this embodiment of the invention provides an electronic device, which includes a processor, a memory, and a program stored in the memory and executable on the processor. When the processor executes the program, it performs the following steps: receiving a first statement sent by a target object, wherein the first statement is used to query target data in a database using a time field; parsing the first statement to obtain a parsing result; checking whether a target function exists in the first statement based on the parsing result to obtain a check result, wherein the target function is a function for obtaining a timestamp from the database; and querying the target data in the database using a time field based on the check result.
[0112] When the processor executes the program, it also performs the following steps: checking whether the target function exists in the first statement based on the parsing result, and obtaining the check result includes: obtaining the abstract syntax tree corresponding to the first statement based on the parsing result; traversing the nodes of the abstract syntax tree to obtain the traversal result; checking the first statement based on the traversal result to obtain the check result, wherein the check result is used to indicate whether the target function exists in the first statement.
[0113] When the processor executes the program, it also performs the following steps: checking the first statement based on the traversal result to obtain the check result includes: determining whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; if the target node does not exist in the abstract syntax tree, then executing the first statement and querying the target data in the database using the time field; if the target node exists in the abstract syntax tree, then obtaining N functions in the target node, wherein N is a positive integer; checking the first statement based on the N functions to obtain the check result.
[0114] When the processor executes the program, it also performs the following steps: checking the first statement based on the N functions, and obtaining the check result includes: determining whether the target function exists among the N functions; if the target function exists among the N functions, it means that the target function exists in the first statement; if the target function does not exist among the N functions, it means that the target function does not exist in the first statement.
[0115] When the processor executes the program, it also performs the following steps: querying the target data in the database using a time field based on the check result includes: if the check result indicates that the target function does not exist in the first statement, then the first statement is executed, and the target data is queried in the database using a time field; if the check result indicates that the target function exists in the first statement, then it is determined whether the target function has parameters, and a determination result is obtained; and the target data is queried in the database using a time field based on the determination result.
[0116] When the processor executes the program, it also performs the following steps: querying the target data in the database using the time field based on the judgment result includes: if the judgment result indicates that the target function contains the parameter, then executing the first statement and querying the target data in the database using the time field; if the judgment result indicates that the target function does not contain the parameter, then sending a reminder message to the target object, wherein the reminder message is used to remind the target object to add the parameter in the target function; obtaining the second statement after the target object adds the parameter in the target function; executing the second statement and querying the target data in the database using the time field.
[0117] When the processor executes the program, it also performs the following steps: after parsing the first statement and obtaining the parsing result, the method further includes: determining whether the first statement was successfully parsed based on the parsing result; if the first statement was successfully parsed, obtaining the abstract syntax tree corresponding to the first statement; if the first statement was unparsedated, executing the first statement and querying the target data in the database using the time field.
[0118] The devices mentioned in this article can be servers, PCs, tablets, mobile phones, etc.
[0119] This application also provides a computer program product, which, when executed on a data processing device, is suitable for executing an initialization program having the following method steps: receiving a first statement sent by a target object, wherein the first statement is used to query target data in a database using a time field; parsing the first statement to obtain a parsing result; checking whether a target function exists in the first statement based on the parsing result, and obtaining a check result, wherein the target function is a function for obtaining a timestamp from the database; and querying the target data in the database using a time field based on the check result.
[0120] When executed on a data processing device, it is also suitable to execute an initialization program with the following steps: checking whether a target function exists in the first statement based on the parsing result, and obtaining the check result includes: obtaining an abstract syntax tree corresponding to the first statement based on the parsing result; traversing the nodes of the abstract syntax tree to obtain a traversal result; checking the first statement based on the traversal result to obtain the check result, wherein the check result is used to indicate whether the target function exists in the first statement.
[0121] When executed on a data processing device, it is also suitable to execute an initialization program with the following steps: checking the first statement based on the traversal result, and obtaining the check result includes: determining whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; if the target node does not exist in the abstract syntax tree, then executing the first statement and querying the target data in the database using a time field; if the target node exists in the abstract syntax tree, then obtaining N functions in the target node, wherein N is a positive integer; checking the first statement based on the N functions to obtain the check result.
[0122] When executed on a data processing device, it is also suitable to execute an initialization program with the following method steps: checking the first statement based on the N functions, and obtaining the check result includes: determining whether the target function exists among the N functions; if the target function exists among the N functions, it indicates that the target function exists in the first statement; if the target function does not exist among the N functions, it indicates that the target function does not exist in the first statement.
[0123] When executed on a data processing device, it is also suitable to execute an initialization program with the following method steps: querying the target data in the database using a time field based on the check result includes: if the check result indicates that the target function does not exist in the first statement, then executing the first statement and querying the target data in the database using a time field; if the check result indicates that the target function exists in the first statement, then determining whether the target function has parameters and obtaining a determination result; and querying the target data in the database using a time field based on the determination result.
[0124] When executed on a data processing device, it is also suitable to execute an initialization program with the following method steps: querying the target data in the database using a time field based on the judgment result includes: if the judgment result indicates that the target function contains the parameter, then executing the first statement and querying the target data in the database using a time field; if the judgment result indicates that the target function does not contain the parameter, then sending a reminder message to the target object, wherein the reminder message is used to remind the target object to add the parameter in the target function; obtaining the second statement after the target object adds the parameter in the target function; executing the second statement and querying the target data in the database using a time field.
[0125] When executed on a data processing device, it is also suitable to execute an initialization program with the following method steps: after parsing the first statement and obtaining the parsing result, the method further includes: determining whether the first statement was successfully parsed based on the parsing result; if the first statement was successfully parsed, obtaining the abstract syntax tree corresponding to the first statement; if the first statement was not parsed, executing the first statement and querying the target data in the database using the time field.
[0126] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied 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.
[0127] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0128] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0129] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0130] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0131] Memory may include non-persistent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0132] Computer-readable media includes both permanent and non-permanent, removable and non-removable media that can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable media does not include transient computer-readable media, such as modulated data signals and carrier waves.
[0133] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0134] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied 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.
[0135] The above are merely embodiments of this application and are not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. A data query method, characterized in that, include: Receive a first statement sent by the target object, wherein the first statement is used to query target data in the database using a time field; The first statement is parsed to obtain the parsing result, and the parsing is based on a syntax parser that supports syntax extension; Based on the parsing result, check whether there is a target function in the first statement, and obtain the check result, wherein the target function is the function that retrieves timestamps from the database; Based on the inspection results, the target data is queried in the database using the time field. Specifically, querying the target data in the database using a time field based on the inspection results includes: If the inspection result indicates that the target function exists in the first statement, then it is determined whether the target function has parameters, and a determination result is obtained; If the judgment result indicates that the objective function contains the parameter, then the first statement is executed, and the objective data is queried in the database using the time field; If the judgment result indicates that the parameter is not present in the objective function, a reminder message is sent to the target object, wherein the reminder message is used to remind the target object to add the parameter to the objective function; Obtain the second statement following the addition of the parameter to the target function for the target object; Execute the second statement and query the target data in the database using the time field; If the judgment result indicates that the parameter is not in the target function, before sending the reminder information to the target object, the system jumps to the hit rule, adds the first statement to the hit list, and prohibits the execution of the first statement; Based on the parsing results, the presence of a target function in the first statement is checked, and the check results include: Based on the parsing results, the abstract syntax tree corresponding to the first statement is obtained; Traverse the nodes of the abstract syntax tree to obtain the traversal result; The first statement is examined based on the traversal results to obtain the examination result, wherein the examination result is used to indicate whether the target function exists in the first statement; Based on the traversal results, the first statement is examined, and the examination results include: Based on the traversal results, it is determined whether a target node exists in the abstract syntax tree, wherein the target node is a node used to query data in the database; If the target node does not exist in the abstract syntax tree, then the first statement is executed, and the target data is queried in the database using the time field; If the target node exists in the abstract syntax tree, then obtain N functions from the target node, where N is a positive integer; The first statement is examined based on the N functions to obtain the examination results.
2. The method according to claim 1, characterized in that, Based on the N functions, the first statement is examined, and the examination results include: Determine whether the target function exists among the N functions; If the target function exists among the N functions, it means that the target function exists in the first statement; If the target function is not present among the N functions, it means that the target function is not present in the first statement.
3. The method according to claim 1, characterized in that, Based on the inspection results, querying the target data in the database using the time field includes: If the check result indicates that the target function does not exist in the first statement, then the first statement is executed, and the target data is queried in the database using the time field.
4. The method according to claim 1, characterized in that, After parsing the first statement and obtaining the parsing result, the method further includes: Based on the parsing results, determine whether the first statement was parsed successfully; If the first statement is successfully parsed, the abstract syntax tree corresponding to the first statement is obtained; If parsing the first statement fails, then the first statement is executed, and the target data is queried in the database using the time field.
5. A data query device, characterized in that, include: The first receiving unit is used to receive a first statement sent by the target object, wherein the first statement is used to query target data in the database using a time field; The first parsing unit is used to parse the first statement and obtain the parsing result, wherein the parsing is based on a syntax parser that supports syntax extension; The first checking unit is used to check whether there is a target function in the first statement based on the parsing result, and to obtain the checking result, wherein the target function is a function for obtaining timestamps from the database; The first query unit is used to query the target data in the database using a time field based on the inspection results; The first query unit includes: a first judgment subunit, used to determine whether there are parameters in the target function if the check result indicates that there is a target function in the first statement, and obtain a judgment result; and a first query subunit, used to query the target data in the database using the time field based on the judgment result. The first query subunit includes: a second execution module, used to execute a first statement and query target data in the database using a time field if the judgment result indicates that the target function has parameters; a first sending module, used to send a reminder message to the target object if the judgment result indicates that the target function has no parameters, wherein the reminder message is used to remind the target object to add parameters to the target function; a second acquisition module, used to acquire a second statement after the target object adds parameters to the target function; and a third execution module, used to execute the second statement and query target data in the database using a time field. If the judgment result indicates that the parameter is not in the target function, before sending the reminder information to the target object, the system jumps to the hit rule, adds the first statement to the hit list, and prohibits the execution of the first statement; The first checking unit includes: a first determining subunit, used to obtain the abstract syntax tree corresponding to the first statement based on the parsing result; a first traversal subunit, used to traverse the nodes of the abstract syntax tree and obtain the traversal result; and a first checking subunit, used to check the first statement based on the traversal result and obtain the checking result, wherein the checking result is used to indicate whether the objective function exists in the first statement. The first checking subunit includes: a first determining module, used to determine whether a target node exists in the abstract syntax tree based on the traversal result, wherein the target node is a node used to query data in the database; a first execution module, used to execute the first statement and query the target data in the database using the time field if the target node does not exist in the abstract syntax tree; a first obtaining module, used to obtain N functions in the target node if the target node exists in the abstract syntax tree, wherein N is a positive integer; and a first checking module, used to check the first statement based on the N functions and obtain the checking result.
6. A computer-readable storage medium, characterized in that, The storage medium stores a program, wherein the program executes the data query method according to any one of claims 1 to 4.
7. An electronic device, characterized in that, It includes one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the data query method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Data processing method and device based on database engine, terminal and medium
CN111522816A
Data processing method and device, equipment and medium
CN114297230A