Database query method, database query device, electronic equipment and storage medium
By opening the asynchronous parallel thread in the session objects of the database query request, the matchmaker is used to query the preset execution strategy to cache and detect the query grammar tree, which solves the problem of the existing database query method compilation and calculation. Database query processing.
Patent Information
- Application Number
- CN202411274403.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-12
- Publication Date
- 2025-01-28
AI Technical Summary
The existing database query method takes too long to compile and calculate the compiler and cannot meet the needs of transaction-based computing. How to improve the efficiency of database query has become an urgent problem.
By opening the asynchronous parallel thread in the session objects of the database query request, use the front -threaded matching matching to query the preset execution strategy cache, and generate the query grammar tree in the second thread to detect the correctness of the target execution strategy. Continue to implement the strategy, otherwise they will return.
The query of the cache of the asynchronous parallel processing and the preset execution strategy has significantly reduced the time of the compiler calculation, improved the efficiency of the database query, and can meet the needs of trading computing.
Smart Images

Figure CN119377244A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of database technology, and in particular to a database query method, a database query device, an electronic device and a storage medium. Background Art
[0002] After receiving the database query request, the database server converts the database query request into a syntax tree data structure that can be understood by the program through the compiler, and performs database query based on the syntax tree data structure. However, the compilation calculation of the compiler consumes a lot of time and cannot meet the needs of transactional computing such as order transaction processing. Therefore, how to improve the efficiency of database query has become an urgent problem to be solved. Summary of the invention
[0003] The main purpose of the embodiments of the present application is to propose a database query method, a database query device, an electronic device and a storage medium, aiming to improve the efficiency of database query.
[0004] To achieve the above object, a first aspect of an embodiment of the present application proposes a database query method, the method comprising:
[0005] Get database query request;
[0006] Creating a session object for the database query request to obtain a session object instance;
[0007] Opening a target thread in the session object instance; the target thread includes a first thread and a second thread; the first thread and the second thread are asynchronous parallel threads;
[0008] Calling a matcher to query a preset execution strategy cache according to the database query request through the first thread;
[0009] If the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, then calling the executor through the first thread to execute the target execution strategy while waiting for the query syntax tree of the database query request generated by the second thread;
[0010] Detecting the target execution strategy according to the query syntax tree;
[0011] If the target execution strategy is correct, the first thread calls the executor to continue executing the target execution strategy and query the preset database.
[0012] In some embodiments, after detecting the target execution strategy according to the query syntax tree, the database query method further includes:
[0013] If the target execution strategy is incorrect, the execution of the target execution strategy is interrupted by the first thread, and the process falls back to the rewriter, and the rewriter and the optimizer are called in sequence to process the query syntax tree to obtain a new target execution strategy;
[0014] The first thread calls the executor to execute the new target execution strategy and query the preset database.
[0015] In some embodiments, after calling the matcher through the first thread to query the preset execution strategy cache according to the database query request, the database query method further includes:
[0016] If the target execution strategy corresponding to the database query request is not found from the preset execution strategy cache, waiting, through the first thread, for a query syntax tree of the database query request generated by the second thread;
[0017] Calling a rewriter through a first thread to convert the syntax structure of the query syntax tree to obtain a target syntax tree;
[0018] Invoking the optimizer through the first thread to optimize the target syntax tree to obtain a target execution strategy;
[0019] The target execution strategy is called by the first thread to query the preset database.
[0020] In some embodiments, the query syntax tree is obtained according to the following steps:
[0021] The second thread calls a compiler to compile the database query request to obtain the query syntax tree.
[0022] In some embodiments, if the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, then calling the executor by the first thread to execute the target execution strategy while waiting for the query syntax tree of the database query request generated by the second thread includes:
[0023] If the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, the target execution strategy is executed by calling the executor through the first thread, and at each stage of executing the target execution strategy, after the execution of the target execution strategy is completed and before submitting the query result, it is detected whether the second thread has generated the query syntax tree.
[0024] In some embodiments, if the target execution strategy is correct, then the first thread calls the executor to continue to execute the target execution strategy, and after querying the preset database, the database query method further includes:
[0025] Storing the target execution strategy in the preset execution strategy cache;
[0026] Get the target data query request;
[0027] Query the preset execution strategy cache according to the target data query request to obtain the target execution strategy;
[0028] The preset database is queried according to the target execution strategy.
[0029] In some embodiments, calling the matcher through the first thread to query the preset execution strategy cache according to the database query request includes:
[0030] Calling the matcher to obtain the request structure of the database query request through the first thread;
[0031] The preset execution strategy cache is queried according to the request structure.
[0032] To achieve the above-mentioned purpose, a second aspect of an embodiment of the present application provides a database query device, the device comprising:
[0033] An acquisition module is used to obtain database query requests;
[0034] A creation module, used to create a session object for the database query request to obtain a session object instance;
[0035] An opening module, used for opening a target thread in the session object instance; the target thread includes a first thread and a second thread; the first thread and the second thread are asynchronous parallel threads;
[0036] A query module, configured to call a matcher through the first thread to query a preset execution strategy cache according to the database query request;
[0037] A first execution module, configured to, if a target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, call an executor through a first thread to execute the target execution strategy, while waiting for a query syntax tree of the database query request generated by the second thread;
[0038] A detection module, used for detecting the target execution strategy according to the query syntax tree;
[0039] The second execution module is used to call the executor through the first thread to continue to execute the target execution strategy and query the preset database if the target execution strategy is correct.
[0040] To achieve the above-mentioned purpose, the third aspect of an embodiment of the present application proposes an electronic device, the electronic device comprising a memory and a processor, the memory storing a computer program, and the processor implementing the database query method of the above-mentioned first aspect when executing the computer program.
[0041] To achieve the above objectives, the fourth aspect of the embodiments of the present application proposes a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the database query method of the first aspect is implemented.
[0042] The database query method, database query device, electronic device and computer-readable storage medium proposed in the present application obtain a database query request, create a session object for the database query request, obtain a session object instance, and avoid the database query request triggering the compilation calculation of the compiler. In the session object instance, the target thread is opened, and the target thread includes a first thread and a second thread. The first thread and the second thread are asynchronous parallel threads, so that multiple tasks can be processed simultaneously through the first thread and the second thread without waiting for the tasks to be completed in sequence, thereby improving the processing efficiency of the database query request. In order to avoid repeatedly generating the execution strategy of the same database query request, the matcher is called through the first thread to query the preset execution strategy cache according to the database query request. If the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, the executor is called by the first thread to execute the target execution strategy, avoiding the time delay caused by calling the compiler to generate the execution strategy, and improving the acquisition efficiency of the execution strategy. In order to ensure the accuracy of the target execution strategy, the query syntax tree of the database query request generated by the second thread is waited at the same time, and the target execution strategy is detected according to the query syntax tree. If the target execution strategy is correct, the first thread calls the executor to continue executing the target execution strategy and query the preset database. While ensuring the accuracy of the query, the response speed of the preset database to the database query request is improved, thereby improving the efficiency of the database query to meet the query needs of transactional computing. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Figure 1 is a flowchart of a database query method provided in an embodiment of the present application;
[0044] Figure 2 yes Figure 1 Flow chart of step S140 in FIG.
[0045] Figure 3 is another flow chart of the database query method provided in an embodiment of the present application;
[0046] Figure 4 is another flow chart of the database query method provided in an embodiment of the present application;
[0047] Figure 5 is another flow chart of the database query method provided in an embodiment of the present application;
[0048] Figure 6 It is a structural diagram of a database query device provided in an embodiment of the present application;
[0049] Figure 7 It is a schematic diagram of the hardware structure of the electronic device provided in the embodiment of the present application. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0051] It should be noted that, although the functional modules are divided in the device schematic diagram and the logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first", "second", etc. in the specification, claims and the above drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.
[0052] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0053] After receiving the database query request, the database server converts the database query request into a syntax tree data structure that can be understood by the program through the compiler, and performs database query based on the syntax tree data structure. However, the compilation calculation of the compiler consumes a lot of time and cannot meet the needs of transactional computing such as order transaction processing. Therefore, how to improve the efficiency of database query has become an urgent problem to be solved.
[0054] Based on this, the embodiments of the present application provide a database query method, a database query device, an electronic device and a computer-readable storage medium, aiming to improve the efficiency of database query.
[0055] The database query method, database query device, electronic device and computer-readable storage medium provided in the embodiments of the present application are specifically described through the following embodiments. First, the database query method in the embodiments of the present application is described.
[0056] The database query method provided in the embodiment of the present application relates to the field of database technology. The database query method provided in the embodiment of the present application can be applied to a terminal, can be applied to a server side, or can be software running in a terminal or a server side. In some embodiments, the terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, etc.; the server side can be configured as an independent physical server, or can be configured as a server cluster or a distributed system composed of multiple physical servers, and can also be configured as a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application that implements the database query method, etc., but is not limited to the above forms.
[0057] The present application can be used in many general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, etc. The present application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application can also be practiced in distributed computing environments, in which tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0058] Figure 1 is an optional flowchart of a database query method provided in an embodiment of the present application. Figure 1 The method may include but is not limited to steps S110 to S160.
[0059] Step S110, obtaining a database query request;
[0060] Step S120, creating a session object for the database query request to obtain a session object instance;
[0061] Step S130, starting a target thread in the session object instance; the target thread includes a first thread and a second thread; the first thread and the second thread are asynchronous parallel threads;
[0062] Step S140, calling the matcher through the first thread to query the preset execution strategy cache according to the database query request;
[0063] Step S150, if the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, then the first thread calls the executor to execute the target execution strategy while waiting for the query syntax tree of the database query request generated by the second thread;
[0064] Step S160, detecting the target execution strategy according to the query syntax tree;
[0065] Step S170: If the target execution strategy is correct, the first thread calls the executor to continue executing the target execution strategy and query the preset database.
[0066] In step S110 of some embodiments, the database query request is a query request submitted by the client to the relational database server, which is used to characterize the client's data access logic requirements. The database query request can be expressed in Structured Query Language (SQL), and the database query request includes a database table name, a database column name, and a query condition, etc. For example, a database query request can be "select name, age from user object table where age>18", and the database query request is used to query the name and age of objects in the user object table that are older than 18 years old. The relational database server receives the database query request sent by the client, and executes the database query request to access the data in the preset database. The relational database server is used to manage and maintain the preset database.
[0067] In step S120 of some embodiments, after receiving a database query request from a client in the form of a character string, the relational database server creates a new session for the database query request and obtains a session object instance.
[0068] In step S130 of some embodiments, in the session object instance, the server starts a target thread for executing the query task, the target thread includes a first thread and a second thread, the first thread and the second thread are asynchronous parallel threads, and the first thread and the second thread are respectively used to execute different subtasks involved in the query task. The first thread and the second thread are asynchronously triggered by the database query request. The first thread is used to sequentially execute the processing flow of the rewriter, optimizer and executor. The second thread is used to execute the compilation flow of the compiler. The efficiency of database query is improved by parallel processing of asynchronous threads.
[0069] See also Figure 2 In some embodiments, step S140 may include but is not limited to steps S210 to S220:
[0070] Step S210, calling a matcher through the first thread to obtain a request structure of a database query request;
[0071] Step S220: query the preset execution strategy cache according to the request structure.
[0072] In step S210 of some embodiments, the matcher is called by the first thread, and the constant in the database query request is replaced with a wildcard based on the matching pattern rule to obtain a request structure. The request structure is a string. For example, if the database query request is "Select*from Emp l oyees where sale>6000", then the request structure is "Select*from Emp l oyees where sale>?".
[0073] In step S220 of some embodiments, a preset execution strategy cache located in a relational database server is queried according to the request structure. The preset execution strategy cache is used to store the execution strategy corresponding to the request structure. The execution strategy is an execution plan, which describes in detail the steps of executing the query in the database, including how to access data, how to connect tables, how to apply query conditions (filter conditions), etc.
[0074] Through the above steps S210 to S220, by querying the execution strategy in the preset cache, the efficiency of obtaining the execution strategy can be improved and the time cost brought by the compiler performing the compilation calculation can be reduced.
[0075] See also Figure 3 In some embodiments, after step S140, the database query method may further include but is not limited to steps S310 to S340:
[0076] Step S310: If the target execution strategy corresponding to the database query request is not found from the preset execution strategy cache, the first thread waits for the query syntax tree of the database query request generated by the second thread;
[0077] Step S320, calling the rewriter through the first thread to convert the grammatical structure of the query grammar tree to obtain a target grammar tree;
[0078] Step S330, optimizing the target syntax tree by calling the optimizer through the first thread to obtain a target execution strategy;
[0079] Step S340: The first thread calls the executor to execute the target execution strategy to query the preset database.
[0080] In step S310 of some embodiments, if the preset execution strategy cache does not store the target execution strategy corresponding to the database query request, the first thread synchronously waits for the query syntax tree obtained by the second thread calling the compiler to compile and calculate the database query request. The query syntax tree is a syntax tree data structure obtained by the compiler performing lexical analysis and syntax analysis on the database query request according to the syntax rules.
[0081] In step S320 of some embodiments, the first thread calls a rewriter to perform syntax structure conversion on the query syntax tree, converting the complex syntax structure into a simpler and more standardized form to obtain a target syntax tree.
[0082] In step S330 of some embodiments, the first thread calls the optimizer to optimize the target syntax tree, generates an efficient execution plan, and uses the execution plan as the target execution strategy. If a table has an index, when the table needs to be scanned, the optimizer will evaluate the overhead of using index scan (Index Scan) and full table scan (Sequential Scan), and then select the method with less overhead as the execution plan. For a database, the optimizer's optimization operation will take 0.2 to 0.3 ms.
[0083] In step S340 of some embodiments, the first thread calls the executor to execute the target execution strategy to query the preset database, and obtains the data access result corresponding to the database query request.
[0084] Through the above steps S310 to S340, when the execution strategy is not queried from the preset execution strategy cache, the first thread waits for the query syntax tree generated by the second thread, and uses the query syntax tree to generate the target execution strategy, thereby avoiding the time delay caused by the first thread directly calling the compiler to generate the query syntax tree. Through the asynchronous parallel execution of the first thread and the second thread, the efficiency of the database query is improved.
[0085] In step S150 of some embodiments, if the preset execution strategy cache stores a target execution strategy corresponding to a database query request, and the target execution strategy is an execution strategy predicted by the matcher based on matching pattern rules, the target execution strategy is executed by calling the executor through the first thread, thereby improving the efficiency of obtaining the execution strategy and saving the time of compilation calculation. In order to ensure the accuracy of the target execution strategy, the compiler is called by the second thread to compile the database query request to generate a query syntax tree. The execution of the target execution strategy is divided into multiple stages. The first thread detects whether the compiler generates a compilation result, that is, whether a query syntax tree is generated, at each stage of executing the target execution strategy, after the execution of the target execution strategy is completed and before submitting the query result. The query result refers to the data access result in response to the database query request.
[0086] In step S160 of some embodiments, if the second thread generates a query syntax tree, the first thread detects the target execution strategy according to the query syntax tree to determine the correctness of the target execution strategy. The logical execution plan represented by the query syntax tree is compared with the target execution strategy. If the logical execution plan represented by the query syntax tree is the same as the target execution strategy, the target execution strategy is correct; if the logical execution plan represented by the query syntax tree is different from the target execution strategy, the target execution strategy is wrong.
[0087] See also Figure 4 In some embodiments, after step S160, the database query method may further include but is not limited to steps S410 to S420:
[0088] Step S410: If the target execution strategy is incorrect, the execution of the target execution strategy is interrupted by the first thread, and the process returns to the rewriter, and the rewriter and the optimizer are called in sequence to process the query syntax tree to obtain a new target execution strategy.
[0089] Step S420: The first thread calls the executor to execute the new target execution strategy and query the preset database.
[0090] In step S410 of some embodiments, if the target execution strategy is wrong, the execution of the target execution strategy is terminated by the first thread, and the process falls back to the rewriter, and the query syntax tree is converted into a target syntax tree by the rewriter. The target syntax tree is optimized by the optimizer to obtain a new target execution strategy. At this time, the second thread can call the compiler to compile and calculate the new database query request, and generate a query syntax tree corresponding to the new database query request, which greatly improves the efficiency of database query.
[0091] In step S420 of some embodiments, the first thread calls the executor to execute the new target execution strategy and query the preset database.
[0092] Through the above steps S410 to S420, when the target execution strategy is incorrect, the generated query syntax tree is directly used to obtain the correct target execution strategy, which improves the efficiency and accuracy of obtaining the target execution strategy and avoids incorrect database query results. The rollback mechanism is used to ensure that the executed data changes can be quickly rolled back when the prediction fails, ensure the consistency of the database state, and switch to the traditional SQL execution path, effectively controlling the cost of re-execution when the prediction fails.
[0093] In step S170 of some embodiments, if the target execution strategy is correct, the first thread calls the executor to continue executing the target execution strategy and query the preset database. While ensuring the accuracy of the database query, the asynchronous parallel execution of the first thread and the second thread is utilized to improve the efficiency of the database query to meet the needs of transaction-type computing with high timeliness requirements.
[0094] See also Figure 5 In some embodiments, after step S170, the database query method may further include but is not limited to steps S510 to S540:
[0095] Step S510, storing the target execution strategy in a preset execution strategy cache;
[0096] Step S520, obtaining a target data query request;
[0097] Step S530, querying the preset execution strategy cache according to the target data query request to obtain the target execution strategy;
[0098] Step S540: query the preset database according to the target execution strategy.
[0099] In step S510 of some embodiments, the syntax trees of the same type of database query requests are the same, for example, all of them are database query requests for transaction amounts greater than a preset amount. In order to improve the response speed to the same type of database query requests, the target execution strategy is stored in the preset execution strategy cache so that the execution strategy can be directly obtained from the preset execution strategy cache.
[0100] In step S520 of some embodiments, a target data query request from a client is accepted, where the target data query request is a query request sent after the database query request.
[0101] In step S530 of some embodiments, a preset execution strategy cache is queried according to the target data query request to obtain a target execution strategy corresponding to the target data query request.
[0102] In step S540 of some embodiments, referring to steps S150 to S170, a preset database is queried according to the target execution strategy.
[0103] Through the above steps S510 to S540, the efficiency of obtaining the execution strategy can be improved, thereby improving the efficiency of database query.
[0104] Traditional relational database servers receive SQL statements from clients in the form of strings to obtain logical requirements for data access. The server calls the compiler to interpret the string into an internal syntax tree data structure based on the syntax, and converts the string requirements into a memory data structure that the program can understand. After obtaining the syntax tree data structure, the database server converts the logical requirements expressed by the syntax tree into an execution plan through the rewriter and optimizer. The subsequent calculation process is executed completely according to the definition of the execution plan to obtain the expected data access results.
[0105] The execution cost of the rewriter and optimizer is significantly higher than that of transaction-based computing requirements that focus on latency. From the perspective of software execution, existing databases generally provide execution plan caching methods to avoid repeatedly running the rewriter and optimizer for SQL requirements that have been identified or executed. For example, the manual PREPARE / EXECUTE method or the automatic execution plan caching solution. These solutions avoid the execution cost of the rewriter and optimizer, but they cannot avoid the execution delay caused by the computational cost of the compiler.
[0106] As database performance improves, especially when the database is close to the performance of memory, the time it takes for the database to complete the execution calculation is extremely short, reaching the level of 10us. At this time, the cost of running the complete compiler calculation process also becomes prominent. For example, the compilation process of the simplest SQL statement in PostgreSQL also takes at least 10-20us, which is equivalent to the expected time cost of executing a complex transaction. Therefore, it is necessary to avoid the compiler calculation cost becoming a necessary component of database latency in order to achieve the purpose of reducing response latency.
[0107] The relevant technologies mainly have the following disadvantages: the global execution plan cache can only speed up predefined query statements, and the cached statement set is usually fixed and cannot be dynamically updated according to actual SQL request changes, resulting in a low cache hit rate. In addition, even if the execution plan hits the cache, the SQL statement still needs to be parsed by the compiler before the syntax tree can be found, and the delay from the compiler cannot be avoided. Existing complex query plans reduce the delay in generating execution plans by predictive execution, but these solutions can usually only optimize analytical databases and are ineffective for scenarios with massive simple queries in transactional databases.
[0108] In order to solve the above problems, an embodiment of the present application provides a database query method, which includes: asynchronously triggering the compilation process of the compiler to execute the SQL statement string, and generating the matching pattern rules of SQL; the SQL statement string is quickly matched based on the matching pattern rules by the matcher; if the match is successful and a high probability of accurate prediction is obtained, the executor executes according to the execution plan in the cache; if the match fails, synchronously wait for the compiler result of the asynchronous execution, and execute the rewriter, optimizer and executor in sequence according to the original method. If the executor executes according to the predicted execution plan, it checks whether the compilation result of the compiler has been generated in several optional execution stages, and if it has been generated, it judges the accuracy of the prediction; if the executor executes according to the predicted execution plan, it synchronously waits for the compilation result of the compiler before the execution of the execution plan ends or before the transaction is submitted. The executor compares the compiler results to confirm whether the prediction is accurate. If it is accurate, the executor completes the execution of the execution plan. If it is inaccurate, roll back the data changes and return to the rewriter to start execution in sequence.
[0109] For high performance, especially transaction load optimization database, when the execution cost of compiling SQL becomes an optimization item, this method provides a fast predictive execution method, avoiding the time cost of complex compiled SQL statements in the critical execution path, and improving SQL query efficiency. Compared with the existing execution plan buffering solution, the compiler calculation cost can be eliminated from the server response delay to achieve the purpose of improving performance. This application can minimize the context switching cost of asynchronous execution by using coroutine technology, and can start two coroutines to asynchronously execute the compiler's compilation logic and other logic of the rewriter, optimizer and executor respectively. The implementation of the compiler waiting technology can be transformed into a coroutine scheduling algorithm, and there is no additional obvious asynchronous execution cost. The matcher can greatly simplify the computational complexity and number of instructions compared to the standard compiler execution process through a simple string form, achieving the effect of replacing the compiler cost on the critical path. In the production system, the SQL statements to be executed are relatively limited and stable, especially the database access logic automatically triggered by the application program is the SQL demand generated by the fixed program, so there is reason to believe that in most cases, the appropriate matcher algorithm can obtain good prediction results, that is, a very low probability of false prediction. For SQL statements with high failure costs, the matcher algorithm can be used to attribute the failure to obtain a high-probability accurate prediction, and the process will automatically return to the normal mode, making the prediction failure cost controllable.
[0110] See also Figure 6 The embodiment of the present application also provides a database query device, which can implement the above database query method, and the database query device includes:
[0111] An acquisition module 610 is used to acquire a database query request;
[0112] A creation module 620 is used to create a session object for a database query request and obtain a session object instance;
[0113] The start module 630 is used to start a target thread in the session object instance; the target thread includes a first thread and a second thread; the first thread and the second thread are asynchronous parallel threads;
[0114] A query module 640 is used to call the matcher through the first thread to query the preset execution strategy cache according to the database query request;
[0115] The first execution module 650 is used to, if a target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, call the executor through the first thread to execute the target execution strategy, while waiting for the query syntax tree of the database query request generated by the second thread;
[0116] A detection module 660, configured to detect a target execution strategy according to a query syntax tree;
[0117] The second execution module 670 is used to call the executor through the first thread to continue to execute the target execution strategy and query the preset database if the target execution strategy is correct.
[0118] The specific implementation of the database query device is basically the same as the specific implementation example based on the above-mentioned database query method, and will not be repeated here.
[0119] See also Figure 7 , Figure 7 The hardware structure of an electronic device of another embodiment is illustrated, and the electronic device includes:
[0120] The processor 710 may be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (Application Specific Integrated Circuit, ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present application;
[0121] The memory 720 can be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 720 can store an operating system and other application programs. When the technical solution provided in the embodiment of this specification is implemented by software or firmware, the relevant program code is stored in the memory 720, and the processor 710 calls and executes the database query method of the embodiment of this application;
[0122] Input / output interface 730, used to implement information input and output;
[0123] Communication interface 740, used to realize communication interaction between the device and other devices, which can be realized through wired mode (such as USB, network cable, etc.) or wireless mode (such as mobile network, WI FI, Bluetooth, etc.);
[0124] bus 750 , which transmits information between the various components of the device (e.g., processor 710 , memory 720 , input / output interface 730 , and communication interface 740 );
[0125] The processor 710 , the memory 720 , the input / output interface 730 , and the communication interface 740 are connected to each other in communication within the device via the bus 750 .
[0126] An embodiment of the present application further provides a computer-readable storage medium, which stores a computer program, and when the computer program is executed by a processor, the above-mentioned database query method is implemented.
[0127] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely disposed relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0128] The embodiments described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. Those skilled in the art will appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0129] Those skilled in the art will appreciate that the technical solutions shown in the figures do not constitute a limitation on the embodiments of the present application, and may include more or fewer steps than shown in the figures, or a combination of certain steps, or different steps.
[0130] The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separated, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0131] Those skilled in the art will appreciate that all or some of the steps in the methods disclosed above, and the functional modules / units in the systems and devices may be implemented as software, firmware, hardware, or a suitable combination thereof.
[0132] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0133] It should be understood that in the present application, "at least one (item)" means one or more, and "plurality" means two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can mean: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the objects associated before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0134] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the above units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. The mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0135] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0136] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0137] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including multiple instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of various embodiments of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, referred to as ROM), random access memory (Random Access Memory, referred to as RAM), disk or optical disk and other media that can store programs.
[0138] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but the scope of the rights of the present invention is not limited thereto. Any modification, equivalent substitution and improvement made by a person skilled in the art without departing from the scope and essence of the present invention should be within the scope of the rights of the present invention.
Claims
1. A database query method, characterized in that: The method comprises: Get database query request; Creating a session object for the database query request to obtain a session object instance; Opening a target thread in the session object instance; the target thread includes a first thread and a second thread; the first thread and the second thread are asynchronous parallel threads; Calling a matcher to query a preset execution strategy cache according to the database query request through the first thread; If the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, then calling the executor through the first thread to execute the target execution strategy while waiting for the query syntax tree of the database query request generated by the second thread; Detecting the target execution strategy according to the query syntax tree; If the target execution strategy is correct, the first thread calls the executor to continue executing the target execution strategy and query the preset database.
2. The database query method according to claim 1, characterized in that: After detecting the target execution strategy according to the query syntax tree, the database query method further includes: If the target execution strategy is incorrect, the execution of the target execution strategy is interrupted by the first thread, and the process falls back to the rewriter, and the rewriter and the optimizer are called in sequence to process the query syntax tree to obtain a new target execution strategy; The first thread calls the executor to execute the new target execution strategy and query the preset database.
3. The database query method according to claim 1, characterized in that: After calling the matcher through the first thread to query the preset execution strategy cache according to the database query request, the database query method further includes: If the target execution strategy corresponding to the database query request is not found from the preset execution strategy cache, waiting, through the first thread, for a query syntax tree of the database query request generated by the second thread; Calling a rewriter through a first thread to convert the syntax structure of the query syntax tree to obtain a target syntax tree; Invoking the optimizer through the first thread to optimize the target syntax tree to obtain a target execution strategy; The target execution strategy is called by the first thread to query the preset database.
4. The database query method according to any one of claims 1 to 3, characterized in that: The query syntax tree is obtained according to the following steps: The second thread calls a compiler to compile the database query request to obtain the query syntax tree.
5. The database query method according to any one of claims 1 to 3, characterized in that: If the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, calling the executor through the first thread to execute the target execution strategy while waiting for the query syntax tree of the database query request generated by the second thread, including: If the target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, the target execution strategy is executed by calling the executor through the first thread, and at each stage of executing the target execution strategy, after the execution of the target execution strategy is completed and before submitting the query result, it is detected whether the second thread has generated the query syntax tree.
6. The database query method according to claim 4, characterized in that: If the target execution strategy is correct, then the first thread calls the executor to continue to execute the target execution strategy, and after querying the preset database, the database query method further includes: Storing the target execution strategy in the preset execution strategy cache; Get the target data query request; Query the preset execution strategy cache according to the target data query request to obtain the target execution strategy; The preset database is queried according to the target execution strategy.
7. The database query method according to any one of claims 1 to 3, characterized in that: The calling of the matcher through the first thread to query the preset execution strategy cache according to the database query request includes: Calling the matcher to obtain the request structure of the database query request through the first thread; The preset execution strategy cache is queried according to the request structure.
8. A database query device, characterized in that: The database query device comprises: An acquisition module is used to obtain database query requests; A creation module, used to create a session object for the database query request to obtain a session object instance; An opening module, used for opening a target thread in the session object instance; the target thread includes a first thread and a second thread; the first thread and the second thread are asynchronous parallel threads; A query module, configured to call a matcher through the first thread to query a preset execution strategy cache according to the database query request; A first execution module, configured to, if a target execution strategy corresponding to the database query request can be queried from the preset execution strategy cache, call an executor through a first thread to execute the target execution strategy, while waiting for a query syntax tree of the database query request generated by the second thread; A detection module, used for detecting the target execution strategy according to the query syntax tree; The second execution module is used to call the executor through the first thread to continue to execute the target execution strategy and query the preset database if the target execution strategy is correct.
9. An electronic device, characterized in that The electronic device comprises a memory and a processor, the memory stores a computer program, and the processor implements the database query method according to any one of claims 1 to 7 when executing the computer program.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the database query method according to any one of claims 1 to 7 is implemented.