Database query method and system
By setting triggers in the database table and comparing the change point information during query, the performance problems caused by the database query cache update strategy in the prior art are solved, and strong consistency of cached data and stability of system performance are achieved.
Patent Information
- Application Number
- CN202010159338.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-03-09
- Publication Date
- 2025-05-16
- Estimated Expiration
- 2040-03-09
AI Technical Summary
In the prior art, the update strategy of database query cache has problems such as increasing the cost of update operations and poor cache effect, especially when writing operations are frequent.
By setting a trigger in the database table, when the data changes, the identification information and change point information of the database table are stored in a specific storage unit, and the change point information in the query cache is compared with the change point information in the specific storage unit during query, and when the data is consistent, data is read from the cache, otherwise the cache will be obtained and updated from the database.
It ensures strong consistency of cached data, disperses the data cache pressure per unit time, and makes the overall performance of the system relatively stable.
Smart Images

Figure CN113377811B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database technology, and in particular to a method and system for querying a database. Background Art
[0002] A common problem in the field of database technology is how to shorten the response time for database queries that need to process a large amount of data. Such database queries are, for example, executed based on fewer input conditions, which generally results in a large number of query results being output, or database queries need to be associated with a large number of database tables, so the data association process takes a long time. Therefore, the database query mechanism under big data has attracted the attention of the industry.
[0003] In the prior art, a method often used in a database query mechanism under big data is: when executing a database query, the query result is updated to the query cache, and subsequent query operations are actually implemented for the query cache. Figure 1a-1b A comparison of the prior art in which query cache is not used and query cache is used is shown. As shown in the figure, for the query statement SQL, in the example in which query cache is not used, the query statement SQL obtains data from the database 101 after operations such as statement analysis 102, optimization 103, and execution 104, while in the example in which query cache is used, the query statement SQL obtains data from the query cache 105 first after operations such as statement analysis 102, optimization 103, and execution 104, and only when the query cache 105 does not contain data that meets the conditions, the data is obtained from the database 101, and the obtained data is then updated to the query cache 105.
[0004] However, the method of implementing queries using query cache must ensure that the data stored in the query cache is the latest data, so as to ensure that the query results provided based on the query cache correctly reflect the current state of the database. If the data in the database changes and the data in the query cache becomes outdated, the correct query results cannot be returned. Therefore, it is necessary to provide a strategy to keep the data in the query cache up to date.
[0005] There are many query cache update strategies known in the prior art. For example, the query cache of MySQL adopts a strategy of invalidating the query cache as a whole when data in the database is updated. However, this method increases the cost of the update operation, and the cache effect is poor if there are many write operations. Summary of the invention
[0006] The purpose of the present invention is to provide a method and system for querying a database to overcome the problems existing in the prior art.
[0007] To achieve this object, according to a first aspect of the present invention, an embodiment of the present invention provides a method for querying a database, the method comprising:
[0008] Retrieving the query cache according to the received query statement to obtain identification information and corresponding change point information of each database table included in the query statement;
[0009] Retrieving the specific storage unit according to the identification information of each database table to obtain the change point information corresponding to each database table;
[0010] If the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, the query result data is obtained from the query cache; otherwise, the query result data is first obtained from the database, and then the corresponding change point information is obtained from the specific storage unit, and the query result data and the corresponding change point information are updated to the query cache, wherein when the data in the database table changes, the identification information of the database table and the corresponding change point information are stored in the specific storage unit.
[0011] In some embodiments, a trigger is set for each database table. When the data therein changes, the execution of the trigger is triggered. The trigger is used to store the identification information of the database table and the corresponding change point information in a specific storage unit.
[0012] In some embodiments, the step of storing the identification information of the database table and the corresponding change point information in the specific storage unit and the operation of changing the data in the database table are performed in the same transaction.
[0013] In some embodiments, the specific storage unit is a database table, so that the step of storing the identification information of the database table and the corresponding change point information in the database table and the operation of changing the data in the database table are performed in the same database transaction.
[0014] In some embodiments, the change point information is a self-incrementing value, and each time the data in the database table changes, the change point information is incremented once.
[0015] In some embodiments, the change point information includes time point information, and whenever the data in the database table changes, the change point information is updated according to the operation time point.
[0016] In some embodiments, the method further comprises:
[0017] Set the flag to indicate whether the cache mechanism is enabled;
[0018] When the identifier indicates that the cache mechanism is enabled, the query operation is implemented for the received query statement using the query step, and when the identifier indicates that the cache mechanism is not enabled, the query result data is obtained from the database for any received query statement.
[0019] In some embodiments, a statement-level flag is set to indicate whether a caching mechanism is enabled on a specific query statement.
[0020] In some embodiments, a global flag is set to indicate whether a cache mechanism is enabled for query statements in a global scope.
[0021] In a second aspect, an embodiment of the present invention provides a system for querying a database, including:
[0022] An updating device, used for storing identification information of the database table and corresponding change point information in a specific storage unit when data in the database table changes;
[0023] A query device, used for retrieving a query cache according to a received query statement to obtain identification information and corresponding change point information of each database table contained therein; retrieving a specific storage unit according to the identification information of each database table to obtain the change point information corresponding to each database table; if the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, then obtaining query result data from the query cache; otherwise, obtaining query result data from the database, and then obtaining the corresponding change point information from the specific storage unit, and updating the query result data and the corresponding change point information to the query cache.
[0024] In some embodiments, a trigger is set for each database table. When the data in the database table changes, the execution of the trigger is triggered. The trigger is used to store the identification information and the latest change point information of the database table in a specific storage unit.
[0025] In some embodiments, the step of storing the identification information of the database table and the corresponding change point information in the specific storage unit and the operation of changing the data in the database table are performed in the same transaction.
[0026] In some embodiments, the change point information is a self-incrementing value, and each time the data in the database table changes, the change point information is incremented once.
[0027] In some embodiments, the change point information includes time point information, and whenever the data in the database table changes, the change point information is updated according to the operation time point.
[0028] In some embodiments, the system further comprises:
[0029] An identification device, used to set an identification indicating whether the cache mechanism is enabled;
[0030] When the identifier indicates that the cache mechanism is enabled, the query device uses the query cache to implement query for the received query statement, and when the identifier indicates that the cache mechanism is not enabled, the query device obtains query result data from the database for any received query statement.
[0031] In some embodiments, the flag is a statement-level flag to indicate whether a cache mechanism is enabled on a specific query statement.
[0032] In some embodiments, the identifier is a global identifier to indicate whether a cache mechanism is enabled on query statements in a global scope.
[0033] In a third aspect, an embodiment of the present invention provides an electronic device, including a memory and a processor, wherein the memory further stores computer instructions executable by the processor, and when the computer instructions are executed, any of the methods described above is implemented.
[0034] In a fourth aspect, an embodiment of the present invention provides a computer-readable medium, wherein the computer-readable medium stores computer instructions executable by a terminal device, and when the computer instructions are executed, any of the methods described above is implemented.
[0035] The data query method provided by the embodiment of the present invention realizes consistency judgment within the database table by comparing the change point information, and decides whether to read data from the query cache based on the judgment result, thereby ensuring the strong consistency of the cached data. Moreover, the amount of data for each update operation of the change point information and each query operation is relatively limited, thereby dispersing the data cache pressure per unit time and making the overall performance of the system relatively stable. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The above and other objects, features and advantages of the present invention will become more apparent through the description of the embodiments of the present invention with reference to the following drawings, in which:
[0037] Figure 1a-1b A comparative example of not using query cache and using query cache in the prior art is shown.
[0038] Figure 2 is an exemplary application scenario diagram of a query operation;
[0039] Figure 3a and 3b is a flowchart of a method for querying a database according to an embodiment of the present invention;
[0040] Figure 4 It is used to illustrate Figure 3a and 3b A schematic diagram of a method for querying a database;
[0041] Figure 5 is a schematic diagram of the data stored in a particular storage unit and query cache;
[0042] Figure 6 is a flow chart of a query operation in a database query method according to another embodiment of the present invention;
[0043] Figure 7 It is a structural diagram of a database query system according to an embodiment of the present invention;
[0044] Figure 8 It is a structural diagram of an electronic device for implementing the database query method according to an embodiment of the present invention. DETAILED DESCRIPTION
[0045] The present invention is described below based on embodiments, but the present invention is not limited to these embodiments. In the detailed description of the present invention below, some specific details are described in detail. It is possible for a person skilled in the art to fully understand the present invention without the description of these details. In order to avoid confusing the essence of the present invention, known methods, processes, and procedures are not described in detail. In addition, the drawings are not necessarily drawn to scale.
[0046] Figure 2 The system 20 may include a plurality of terminal devices 201 and a server 203 coupled via a network 202 .
[0047] Each terminal device 201 can be constructed based on various types of processors currently on the market and include various devices such as memory and input and output devices. Various operating systems and various application software are installed on the terminal device 201. The operating system is such as WINDOWS TM Operating system, UNIX operating system, Linux operating system, RTOS operating system, application software such as browsers, video playback software, communication software, various R&D tools, etc.
[0048] Each terminal device 201 can communicate with the network 202 in various ways. The network 202 can be based on one or a combination of various communication technologies implemented by exchanging signals, including but not limited to wired technology using electrical and / or optical transmission cables, and wireless technology using infrared, radio frequency and / or other forms. In different application scenarios, the network 202 can be the Internet, a wide area network or a local area network, such as a company's proprietary network. The network 202 can also be a wired network or a wireless network.
[0049] Each terminal device 201 is deployed with client software 2011, and the server 203 is deployed with server software 2031. As shown in the figure, the client software 2011 receives the user's input information, generates a query statement according to the input information and provides it to the server software 2031. The server software 2031 can directly retrieve data from the database according to the query statement, and return the query result data to the client software 2011. The server software 2031 can also maintain a query cache. When receiving a query statement, the query cache is first searched. If the corresponding query result data can be retrieved from the query cache, it is returned to the client software. If the corresponding query result data cannot be retrieved from the query cache, the database is searched to obtain the query result data.
[0050] It should be understood that the above application scenarios are only application scenarios of query operations under normal circumstances, but are not limited to this. For example, the query operation and the query operation can be initiated and executed from the same application of the same server. In other words, the embodiment of the present invention can be applied to the scenario shown in Figure 1, and can also be applied to other scenarios.
[0051] Below, before describing the embodiments of the present invention in detail based on the drawings, some aspects of the embodiments of the present invention are first introduced.
[0052] The term "query" is used as a general term including information retrieval such as transaction query and statement level. "Query cache" is a cache unit provided for storing query result data. The query cache has a better response speed than the database due to the small amount of data and fast reading and writing speed. The query cache can be located in various types of caches, various types of memory, and even on a hard disk, and the present invention does not limit this.
[0053] "Change point information" is a custom term used to indicate changes in data within a database. When data within a database table changes, the change point information also changes. For example, when a piece of data is deleted from a database, the current change point information is different from the change point information before the data was deleted. Change point information can be established based on each database table or based on the entire database. "Specific storage unit" is a cache unit that stores change point information for each database table. Specific storage units and query caches are both storage units for data, and they can come from the same or different hardware and / or software systems, which is not limited by the present invention. In the present invention, change point information is also stored in the query cache, and the change point information corresponds to the database table when the query result data is obtained.
[0054] Figure 3a and 3b The present invention is a flowchart of a database query method according to an embodiment of the present invention. The method includes an update operation and a query operation for change point information. The update operation of change point information is used to update the change point information in the query cache according to the database change situation. The query step is used to receive a query statement and obtain query result data from the database table or the query cache according to the query statement.
[0055] The update operation is step S302, which is to update the identification information and the change point information of the database table to a specific storage unit. Since the update operation is triggered by the change of the data in the database table, the figure also shows step S301, that is, at least one of the operations of adding (insert), deleting (delete), and modifying (update) is used to change the data in the database table. Of course, the change of the structure of the database table will also cause the data in the database table to change. However, the change of the structure of the database table will affect the normal execution of the business process, so the structure of the database table is generally not modified when the system is running.
[0056] The identification information of a database table is the unique identifier of the database table, which can be represented by the name of the database and the name of the database table, or only by the name of the database table. Change point information can be represented in a variety of ways. For example, the change point information can be represented by a self-incrementing value. Whenever the data in the database table is added, updated, or deleted, the change point information is incremented once. For another example, the change point information can also be represented by the time point of the addition, update, or deletion operation. If a piece of data is deleted from the database table operations at 18:42:13 on November 25, 2019, the change point information of operations is modified to: 20191125184213.
[0057] The update operation is performed before or after each addition, update or deletion operation. The change point information is updated to a specific storage unit, thereby ensuring that the specific storage unit stores the latest change point information of each database table in real time.
[0058] The query operation includes steps S311 to S315.
[0059] In step S311, the query cache is searched according to the received query statement to obtain identification information and corresponding change point information of each database table contained therein.
[0060] When searching the query cache according to the received query statement, there are two possibilities: the first possibility is that the query cache stores the corresponding data, and the data can be directly read out, and the identification information of each database table contained in the query statement and the corresponding change point information can be obtained from it; the second possibility is that the query cache does not store the corresponding data, and the execution will jump to step S316. The second possibility is not the scenario concerned by the present invention, and therefore, it is not shown in the figure.
[0061] In step S312, a specific storage unit is searched according to the identification information of each database table to obtain the change point information corresponding to each database table.
[0062] Similarly, there are two possibilities for retrieving a specific storage unit based on the identification information of each database table: the first possibility is that the corresponding data is retrieved in the specific storage unit, and the data can be read out to obtain the change point information corresponding to each database table; the second possibility is that the corresponding data is not retrieved in the specific storage unit, and combined with the data that has been read in the query cache, it can only mean that an error has occurred in the system, an error message can be given, and the execution of the query step is terminated. The second possibility is not the scenario concerned by the present invention, so it will not be described in detail in this step.
[0063] In step S313, the change point information of each database table obtained from the query cache is compared one by one with the corresponding change point information obtained from the specific storage unit.
[0064] In step S314, whether they are all consistent. If the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, step S315 is executed, otherwise step S316 is executed.
[0065] In step S315, the query result data is obtained from the query cache.
[0066] In step S316, the query result data is obtained from the database table, and then the corresponding change point information is obtained from the specific storage unit, and the query result data and the corresponding change point information are updated to the query cache.
[0067] In steps S313-S316, the change point information of each database table obtained from the query cache is compared with the change point information obtained from the specific storage unit accordingly. If the change point information of each database table is consistent, it means that the query result data retrieved in the query cache according to the received query statement is the latest data. However, as long as the change point information of one database table is inconsistent, it means that the query result data retrieved in the query cache according to the received query statement is not the latest data. Therefore, the query result data must be re-obtained from the database, and then the corresponding change point information must be obtained from the specific storage unit, and the query result data and the corresponding change point information must be updated to the query cache.
[0068] Combine the following Figure 4 and 5 The above embodiment is further described. Figure 4 As shown, for each change of the data in the database table 401, the update operation will update the change point information in the specific storage unit 10. Initially, the query statement SQL obtains data from the database 101 through operations such as statement analysis 402, optimization 403 and execution 404, and stores the data and the change point information obtained from the specific storage unit 10 in the query cache 30. Each subsequent execution of the query statement SQL first obtains the change point information of the database table 401 from the specific storage unit 10, and then compares it with the change point information obtained from the query cache 30. If they are the same, the data is obtained from the query cache. If they are different, the data is obtained from the database 401, and the change point information in the specific storage unit 10 and the data obtained from the database 401 are updated to the query cache 30.
[0069] Figure 5 This is a diagram of the data stored in a specific storage unit and query cache. Figure 5As shown, the specific storage unit 10 is used to store the corresponding relationship data between the identification information of the database table and the table change point information, and the query cache 30 is used to store the corresponding relationship data between the query statement, the change point information and the query result data. The specific storage unit 10 can be a storage unit in various forms, such as a file, a memory, a database table, etc. The query cache 30 is a cache unit. For example. When the data in the database table with the identification information 11 changes, the change can be caused by operations such as adding, deleting, updating, and changing the structure of the database table. In response to the change, the change point information 21 is modified to the change point information 50. Afterwards, query statement 31 is received. Since query statement 31 involves a query on a database table with identification information 11, change point information 50 is taken out from the specific storage unit 10 and compared with change point information 41 taken out from the query cache 30 (change point information 41 contains change point information corresponding to each database table involved in query statement 31). If the change point information corresponding to identification information 11 stored in change point information 41 is 21, that is, the change point information between the two is inconsistent, it means that after the last query operation of query statement 31, the data in the database table corresponding to identification information 11 has changed. At this time, the query operation will re-obtain new query result data from the database table, and update the new query result data and change point information 50 to the cache of query statement 31.
[0070] In summary, the above embodiment uses change point information for comparison to achieve consistency judgment within the database table, thereby ensuring strong consistency of cached data. Moreover, the update step is used to maintain lightweight change point information, and the query step is used to maintain query result data according to the query statement. Since the data that needs to be maintained each time in the update step and the query step is relatively limited, the data cache pressure per unit time is dispersed, making the overall performance of the system relatively stable.
[0071] It should be noted that the update operation and the query operation can occur simultaneously or at different times. When the update operation and the query operation do not occur at the same time, the update operation will not affect the query operation. However, if the update operation and the query operation occur at the same time, one possible situation is that when the update operation is in the update state, the query operation has read the change point information from the specific storage unit, which causes the query operation to fail to recognize that the current query result data has expired. However, this has little impact on the system and does not need to be processed. Another situation is that the operation of adding, deleting or modifying the data in the database table and the operation of storing the identification information and the corresponding change point information in the specific storage unit are performed in two different transactions, resulting in that when one of the transactions fails, the other transaction succeeds, resulting in the change point information stored in the specific storage unit not reflecting the latest state of the database table. For this situation, a feasible solution is to put the operation that causes the data change in the database table and the operation of storing the identification information and the corresponding change point information in the specific storage unit in the same transaction. The multiple operations contained in the transaction either succeed together or fail together. Thus, through the transaction, it can be ensured that the change point information stored in the specific storage unit reflects the latest state of the corresponding database table.
[0072] When the specific storage unit is a database table, the above solution is to put the operation that causes the data change of the database table and the operation that stores the identification information and the corresponding change point information in the specific storage unit in the same database transaction. Specifically, assuming that the database table is orders, and the database table storing the change point information is versions, and versions stores a record of the change point information of the database table orders, then deleting a piece of data from the database table orders and modifying the corresponding record of orders in versions are executed in the same database transaction. When the deletion operation of orders fails, the corresponding record of orders in versions will not be modified. When the deletion operation of orders succeeds, the corresponding record of orders in versions will be modified.
[0073] When the specific storage unit is a memory unit, various existing coding languages and overall architecture solutions also provide corresponding methods to place operations such as adding, deleting or modifying data in a database table and storing identification information and corresponding change point information in a specific storage unit in the same transaction. For example, local transactions (JDBC transactions) and distributed transactions (Java Transaction API) in transactions in Java can both ensure the execution of transactions. I will not go into details here.
[0074] In some embodiments, the update operation is triggered by a trigger. For example, a trigger is set for each database table. When the database table is updated, deleted, added, etc., the execution of the trigger is triggered, and the trigger stores the identification information and the latest change point information of the database table in a specific storage unit. Let's still take the previous database tables orders and versions as examples. Assuming that triorders is the set trigger, when data is added to the database table orders, triorders is triggered and the corresponding record in versions is modified. Similarly, when the deletion and update operations occur, the trigger will also be triggered, and the corresponding change point information will also be updated.
[0075] Figure 6 FIG. 1 is a flowchart of a query operation in a database query method according to another embodiment of the present invention. Figure 3a The update operation shown is the same.
[0076] The query operation of this embodiment includes the following steps.
[0077] In step S611, a flag is set to indicate whether the cache mechanism is enabled.
[0078] In step S612, a query statement is received.
[0079] In step s613, it is determined whether the cache mechanism is enabled for the query statement. If the cache mechanism is enabled for the query statement, step S614 is executed, otherwise step S619 is executed.
[0080] In step S614, the query cache is searched according to the received query statement to obtain identification information and corresponding change point information of each database table contained therein.
[0081] In step S615, a specific storage unit is searched according to the identification information of each database table to obtain the change point information corresponding to each database table.
[0082] In step S616, the change point information of each database table obtained from the query cache is compared one by one with the corresponding change point information obtained from the specific storage unit.
[0083] In step S617, it is determined whether all the information is consistent. If yes, step S618 is executed, otherwise step S619 is executed.
[0084] In step S618, the query result data is obtained from the query cache.
[0085] In step S619, the query result data is obtained from the database, and the corresponding change point information is obtained from the specific storage unit, and the query result data and the corresponding change point information are updated to the query cache.
[0086] Among them, steps S614-S619 and Figure 3b The corresponding steps are the same as in , so I will not repeat them here.
[0087] In this embodiment, an identifier is set for the query statement to indicate whether the cache mechanism is enabled. Each time a query statement is received, the corresponding identifier is retrieved and judged through the query statement. If the identifier indicates that the cache mechanism is enabled, the query statement is processed using the query cache. If the identifier indicates that the cache mechanism is not enabled, the query result data corresponding to the query statement is directly obtained from the database table. Similarly, when updating, deleting or modifying the data in the database table, the identifier corresponding to the database table can be retrieved first. If the identifier indicates that the cache mechanism is not enabled, there is no need to modify the change point information. If the identifier indicates that the cache mechanism is enabled, the change point information needs to be modified. Setting the identifier at the statement level can flexibly adjust whether to use the cache mechanism according to the situation of each query statement. For example, for some query statements whose data changes frequently, it is necessary to frequently update the data in the database to the query cache. Using the cache mechanism does not improve the query efficiency, so the cache mechanism can be stopped.
[0088] Preferably, the flag indicating whether the cache mechanism is enabled corresponding to the query statement can be stored in the query cache 30, that is, each query statement corresponds to a flag, so that whether the cache mechanism is enabled can be determined based on the flag. However, for the change point information of each database table stored in a specific storage unit, regardless of whether the cache mechanism is enabled, it is best to record it, because if the change point information of each database table is not recorded, even if a flag indicating that the cache mechanism is enabled is set for a certain query statement, strong consistency of the query cache cannot be achieved.
[0089] In the above embodiment, the flag indicating whether the cache mechanism is enabled is set for query statements at the statement level. In another embodiment, a flag indicating whether the global cache mechanism is enabled can also be set. The global flag can be set as a global variable of the application. When the application is started, the global variable is obtained and judged. If the flag indicates that the cache mechanism is enabled, all query statements use the cache mechanism, otherwise all query statements do not use the cache mechanism. Of course, the two can also be used in combination.
[0090] Figure 7 1 is a structural diagram of a system for implementing query based on database cache according to an embodiment of the present invention. The system corresponds to the previous method, so it will be described in a relatively brief manner in the following description.
[0091] The system 70 includes an updating device 701 and a querying device 702. The updating device 701 is used to store the identification information of the database table and the corresponding change point information in a specific storage unit when the data in the database table changes.
[0092] Specifically, the operations that change the data in the database table include adding, deleting, modifying data or changing the database structure, etc. When one of these operations occurs, the identification information of the database table and the corresponding change point information are stored in a specific storage unit.
[0093] The query device 702 is used to retrieve the query cache according to the received query statement to obtain the identification information and corresponding change point information of each database table contained therein; retrieve the specific storage unit according to the identification information of each database table to obtain the change point information corresponding to each database table; if the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, obtain the query result data from the query cache; otherwise, obtain the query result data from the database, obtain the corresponding change point information from the specific storage unit, and update the query result data and the corresponding change point information to the query cache.
[0094] In summary, the system provided by this embodiment uses the change point information for comparison to implement consistency judgment within the database table, and decides whether to read data from the query cache based on the judgment result, thereby ensuring strong consistency of the cached data. Furthermore, the update device is used to maintain lightweight change point information, and the query device is used to maintain the query result data based on the query statement, so that the data that needs to be maintained by the update device and the query device each time is relatively limited, thereby dispersing the data cache pressure per unit time, making the overall performance of the system relatively stable.
[0095] In some embodiments, a trigger is set for each database table. When the data in the database table changes, the trigger is executed. The trigger is used to store the identification information of the database table and the latest change point information in a specific storage unit.
[0096] In some embodiments, the step of storing the identification information of the database table and the corresponding change point information in the specific storage unit and the operation of changing the data in the database table are performed in the same transaction. If the specific storage unit is also a database table, the above operation can be performed in a database transaction.
[0097] In some embodiments, the change point information is a self-incrementing value, and each time the data in the database table changes, the change point information is incremented once.
[0098] In some embodiments, the change point information includes time point information, and whenever the data in the database table changes, the time point of the operation is updated to the change point information.
[0099] In some embodiments, the system further includes: an identification device for setting an identification of whether the cache mechanism is enabled; when the identification indicates that the cache mechanism is enabled, the query device uses the query cache to implement the query for the received query statement, and when the identification indicates that the cache mechanism is not enabled, the query device obtains the query result data from the database for any received query statement. Optionally, the identification is a statement-level identification to indicate whether the cache mechanism is enabled for a specific query statement. Optionally, the identification is a global identification to indicate whether the cache mechanism is enabled for all query statements in the global scope.
[0100] Corresponding to the above embodiment, Figure 8 As shown, the present invention also provides an electronic device 80, which includes a memory 802 and a processor 801 at the hardware level. In addition, in some cases, it also includes input and output devices 803 and other hardware 804. The memory 802 is, for example, a high-speed random access memory (Random-Access Memory, RAM), and may also be a non-volatile memory (non-volatile memory), such as at least one disk storage, etc. The input and output devices 803 are, for example, devices such as a display, a keyboard, a mouse, and a network controller. The processor 801 can be built based on various models of processors currently on the market. The processor 801, the memory 802, the input and output devices 803 and other hardware 804 are interconnected through a bus, which can be an ISA (Industry Standard Architecture) bus, a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 8 The fact that only one line is used does not mean that there is only one bus or only one type of bus.
[0101] The memory 802 is used to store programs. Specifically, the program may include program code, and the program code includes computer instructions. The memory may include internal memory and non-volatile memory, and provide computer instructions and data to the processor 801. The processor 801 reads the corresponding computer program from the memory 802 into the internal memory and then runs it, forming the above-mentioned interactive method applied to the integrated development environment at the logical level, and is specifically used to perform the following operations: retrieve the query cache according to the received query statement to obtain the identification information and corresponding change point information of each database table contained in the query statement; retrieve the specific storage unit according to the identification information of each database table to obtain the change point information corresponding to each database table; if the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, then obtain the query result data from the query cache; otherwise, first obtain the query result data from the database, then obtain the corresponding change point information from the specific storage unit, and update the query result data and the corresponding change point information to the query cache, wherein, when the data in the database table changes, the identification information and the corresponding change point information of the database table are stored in the specific storage unit.
[0102] Those skilled in the art will appreciate that the present invention can be implemented as a system, method, and computer program product. Therefore, the present invention can be specifically implemented in the following forms, i.e., complete hardware, complete software (including firmware, resident software, microcode), and can also be implemented in the form of a combination of software and hardware. In addition, in some embodiments, the present invention can also be implemented in the form of a computer program product in one or more computer-readable media, and the computer-readable medium contains a computer-readable program code.
[0103] Any combination of one or more computer-readable media may be used. A computer-readable medium may be a computer-readable signal medium or a computer-readable storage medium. A computer-readable storage medium may be, for example but not limited to, an electrical, magnetic, optical, electromagnetic, infrared or semiconductor system, device or device, or any combination thereof. More specific examples of computer-readable storage media include: an electrical connection of one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In this article, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by a processing unit, a device or a device, or used in combination with the program.
[0104] Computer readable signal media may include data signals propagated in baseband or as part of a chopped wave, which carry computer readable program code. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any other suitable combination. Computer readable signal media may also be any computer readable medium other than a computer readable storage medium, which may send, propagate, or transmit a program for use by or in conjunction with an instruction system, apparatus, or device.
[0105] Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., and any suitable combination of the foregoing.
[0106] The computer program code for executing the embodiment of the present invention can be written in one or more programming languages or combinations. The programming language includes object-oriented programming languages, such as JAVA, C++, and can also include conventional procedural programming languages, such as C. The program code can be executed completely on the user's computer, partially on the user's computer, as an independent software package, partially on the user's computer, partially on the remote computer, or completely on the remote computer or server. In the case of a remote computer, the remote computer can be connected to the user's computer through any type of network including a local area network (LAN) or a wide area network (WAN), or can be connected to an external computer (e.g., using an Internet service provider to connect through the Internet).
[0107] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for querying a database, the method comprising: Retrieving the query cache according to the received query statement to obtain identification information and corresponding change point information of each database table included in the query statement; Retrieving a specific storage unit according to the identification information of each database table to obtain the change point information corresponding to each database table; wherein the specific storage unit is a cache unit storing the change point information of each database table; If the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, the query result data is obtained from the query cache; otherwise, the query result data is first obtained from the database, and then the corresponding change point information is obtained from the specific storage unit, and the query result data and the corresponding change point information are updated to the query cache, wherein, when the data in the database table changes, the identification information of the database table and the corresponding change point information are stored in the specific storage unit, so that the latest change point information of each database table is stored in the specific storage unit in real time; A trigger is set for each database table. When the data therein changes, the execution of the trigger is triggered. The trigger is used to store the identification information of the database table and the corresponding change point information in a specific storage unit.
2. The method according to claim 1, wherein: The step of storing the identification information of the database table and the corresponding change point information in the specific storage unit and the operation of changing the data in the database table are performed in the same transaction.
3. The method according to claim 2, wherein: The specific storage unit is a database table, so the step of storing the identification information of the database table and the corresponding change point information in the database table and the operation of changing the data in the database table are performed in the same database transaction.
4. The method according to claim 1, wherein: The change point information is a self-increasing value, and each time the data in the database table changes, the change point information is automatically incremented.
5. The method according to claim 1, wherein: The change point information includes time point information. Whenever the data in the database table changes, the change point information is updated according to the operation time point.
6. The method according to claim 1, further comprising: Set the flag to indicate whether the cache mechanism is enabled; When the identifier indicates that the cache mechanism is enabled, the query method is used to implement the query operation for the received query statement, and when the identifier indicates that the cache mechanism is not enabled, the query result data is obtained from the database for any received query statement.
7. The method according to claim 6, wherein: Sets a statement-level flag to indicate whether caching is enabled for a particular query statement.
8. The method according to claim 6, wherein: Sets the global flag to indicate whether to enable caching for query statements globally.
9. A database query system, comprising: An updating device, used for storing identification information of the database table and corresponding change point information in a specific storage unit when data in the database table changes; A query device, used to retrieve the query cache according to the received query statement to obtain the identification information of each database table contained therein and the corresponding change point information; Retrieving a specific storage unit according to identification information of each database table to obtain change point information corresponding to each database table; If the change point information of each database table obtained from the query cache is consistent with the corresponding change point information obtained from the specific storage unit, then obtaining the query result data from the query cache; Otherwise, the query result data is obtained from the database, and the corresponding change point information is obtained from the specific storage unit, and the query result data and the corresponding change point information are updated to the query cache; wherein the specific storage unit is a cache unit that stores the change point information of each database table, so that the latest change point information of each database table is stored in the specific storage unit in real time; wherein, A trigger is set for each database table. When the data in the database table changes, the execution of the trigger is triggered. The trigger is used to store the identification information and the latest change point information of the database table in a specific storage unit.
10. The system according to claim 9, wherein: The step of storing the identification information of the database table and the corresponding change point information in the specific storage unit and the operation of changing the data in the database table are performed in the same transaction.
11. The system according to claim 9, wherein: The change point information is a self-incrementing value, and each time the data in the database table is changed, the change point information is self-incremented once.
12. The system according to claim 9, wherein: The change point information includes time point information. Whenever the data in the database table changes, the change point information is updated according to the operation time point.
13. The system according to claim 9, wherein: Also includes: An identification device, used to set an identification indicating whether the cache mechanism is enabled; When the identifier indicates that the cache mechanism is enabled, the query system is used to implement the query for the received query statement, and when the identifier indicates that the cache mechanism is not enabled, the query result data is obtained from the database for any received query statement.
14. The system according to claim 13, wherein: The flag is a statement-level flag, which indicates whether to enable the cache mechanism for a specific query statement.
15. The system of claim 13, wherein: The flag is a global flag, which indicates whether the cache mechanism is enabled for query statements in the global scope.
16. An electronic device comprising a memory and a processor, wherein the memory further stores computer instructions executable by the processor, and when the computer instructions are executed, the method according to any one of claims 1 to 8 is implemented.
17. A computer-readable medium storing computer instructions executable by an electronic device, wherein when the computer instructions are executed, the method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Data query method and device
CN108647357A
Cache management apparatus, cache management program and recording medium
JP2012043338A