Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the technical solutions of the present application will be described in detail and completely with reference to the following specific embodiments of the present application and the accompanying drawings. It should be apparent that the described embodiments are only some of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present application.
The technical solutions provided by the embodiments of the present application are described in detail below with reference to the accompanying drawings.
Fig. 1 is a schematic diagram of an application scenario of a multi-table association query method in an embodiment of the present specification. As shown in fig. 1, the workstation 10 is communicably connected to the databases 11, 12, and 13. The databases 11, 12, and 13 store a large number of data tables, respectively. The databases 11, 12 and 13 may be located at different locations, respectively. The databases 11, 12 and 13 may be different types of databases. When the workstation 10 acquires the multi-table association query script and the tables involved in the script are located in the database 11, the database 12 and the database 13, respectively, it is required to acquire relevant data information from the database 11, the database 12 and the database 13, respectively.
Fig. 2 is a flowchart illustrating a multi-table association query method according to an embodiment of the present disclosure. From the viewpoint of a program, the main body of execution of the flow may be a program or an application client mounted on an application server or a workstation computer.
As shown in fig. 2, the process may include the following steps:
s201: obtaining a first query statement for a first table; the first query statement is obtained by analyzing a multi-table association query script;
the multi-table association query script can be a script adopting SQL statements.
The first table in the embodiment of the present specification may be a data table stored in a database.
Open source software can be adopted to analyze the multi-table association query script to obtain query statements for each table.
S202: obtaining a second query statement for a second table; the second query statement is obtained by analyzing the multi-table association query script; the query conditions in the first query statement and the second query statement have the same fields;
for example, the following is a simple multi-table association query script:
in the above paragraph of script, tables a and b are referred to. The script can be parsed by open source software to obtain a query statement for table a and a query statement for table b.
The query statement for table a may be
The query statement for table a may be
It can be seen that the query statement for table a has the same field id as the query condition in the query statement for table b.
S203: estimating a first query cost of the first query statement;
the query cost may refer to the time consumed by executing the query or the amount of occupied hardware resources. In the embodiment of the present specification, the query cost may be estimated from the perspective of the data amount of the query result, or from the perspective of the data amount of the metadata in the data table, or may be estimated in a manner of comprehensively considering the data amount of the query result, the occupancy rate of the memory, and the occupancy rate of the CPU.
The query cost can be estimated by adopting a dynamic programming algorithm and a genetic algorithm in combination with database metadata.
S204: estimating a second query cost for the second query statement;
s205: comparing the first query cost with the second query cost to determine a minimum query cost;
the result of the query cost may be a specific numerical value. Comparing the size of the specific numerical value can determine the minimum query cost.
S206: inquiring according to the inquiry statement corresponding to the minimum inquiry cost to obtain a first inquiry result set;
for the query statement with the minimum query cost, the query can be performed first.
S207: rewriting additional query statements according to the first query result set;
additional query statements may be augmented with qualifiers using the first set of query results as newly augmented qualifiers.
When the query cost of the first query statement is minimal, the further query statement is a second query statement; the further query statement is the first query statement when the query cost of the second query statement is minimal.
S208: inquiring according to the rewritten inquiry statement to obtain a second inquiry result set;
since the restriction condition of the query statement after rewriting is increased, the data amount of the second query result set is smaller than the query result of the query statement before rewriting.
S209: and obtaining an associated query result set according to the first query result set and the second query result set.
Specifically, the first query result set and the second query result set may be connected through a connection algorithm to obtain a final associated query result set.
In the example of query statements given above, assuming that the query cost for the query statement for table a is 100 (indicating that 100 pieces of query results are expected) and the query cost for the query statement for table b is 1000 (indicating that 1000 pieces of query results are expected), the query statement for table a may be executed first. Assuming that the result set obtained after execution is d (a) { id } ═ 1,2,3,. 100}, the query statement for table b can be rewritten as select b.age, b.sex from b where b.aid in (1,2,3,. 100) and b.age >20and b.sex ═ male'. After rewriting, it means that the query statement for table b only needs to query from data with id between 1 and 100, the query scope is reduced, and the query result for table b is within 100. The query cost of the rewritten query statement is smaller than the query cost before rewriting. More specifically, before rewriting, when executing the query statement for the table b, the device of the workstation needs to read about 1000 pieces of data from the database into its own memory, and then filter the data related to the table a from the about 1000 pieces of data, so as to obtain the final 100 pieces of data. After rewriting, the device of the workstation only needs to read the data within 100 items from the database into its own memory, and then connect the data within 100 items with the query result set of the table a.
In summary, in the method in fig. 2, by estimating the query cost of each table, the query statement with the smaller query cost is executed first, and then the other query statement is rewritten according to the obtained query result set, so that the query range of the other query statement can be narrowed, the query cost of the other query statement can be reduced, and the query efficiency of the multi-table association query can be improved.
Based on the method of fig. 2, the present specification also provides some specific embodiments of the method, which are described below.
In the embodiment of the present specification, step S207: rewriting additional query statements according to the first query result set may specifically include:
defining the same field in the further query statement with the value of the first query result set.
In practical applications, when the data amount of a certain data table is large, the estimation of the query cost of the table also consumes a large amount of resources. When the amount of data exceeds a certain value, the cost of evaluating the query against the table may cause a crash of the database. In order to avoid such a situation, in the embodiment of the present specification, step S203: before estimating the first query cost of the first query statement, the method may further include the following steps:
judging whether the data volume of the first table is larger than a preset threshold value or not to obtain a first judgment result;
the estimating the first query cost of the first query statement specifically includes:
and when the first judgment result shows that the data volume of the first table is not larger than the preset threshold, estimating a first query cost of the first query statement.
When the first determination result indicates that the data amount of the first table is greater than the preset threshold, the step of estimating the first query cost of the first query statement may no longer be performed.
In the above step, the specific value of the preset threshold may be set according to the performance of the database.
And when the first judgment result shows that the data volume of the first table is not larger than the preset threshold, estimating the first query cost of the first query statement, and not causing the crash of the database.
When the first judgment result indicates that the data size of the first table is greater than the preset threshold, the following steps may be directly performed without estimating the first query cost of the first query statement:
inquiring according to the first inquiry statement to obtain a third inquiry result set;
inquiring according to the second inquiry statement to obtain a fourth inquiry result set;
and obtaining an associated query result set according to the third query result set and the fourth query result set.
In the steps, the query statement is not rewritten any more, and the original first query statement and the original second query statement are directly adopted for query, so that the crash of the database is avoided.
In the above example, a multi-table associative query is directed to two tables. In practical application, a multi-table association query can be performed on three tables or more tables.
If multi-table association query is performed on three tables, the following steps can be adopted:
obtaining a third query statement for a third table; the query conditions in the third query statement have the same field;
calculating a third query cost of the third query statement;
after the step S205 determines the minimum query cost, and before the step S206 performs query according to the query statement corresponding to the minimum query cost, the method may further include:
comparing the third query cost to the minimum query cost;
if the third query cost is less than the minimum query cost, performing query according to the third query statement to obtain a fifth query result set;
rewriting the query statement corresponding to the minimum query cost according to the fifth query result set;
step S206, performing query according to the query statement corresponding to the minimum query cost, which may specifically include:
and inquiring according to the inquiry statement corresponding to the rewritten minimum inquiry cost.
In the above step, when three tables are involved, the query costs of the three tables can be estimated respectively, and the table with the minimum query cost is selected from the three estimation results and is queried first. And after the query result is obtained, rewriting the query statement of the table with the intermediate query cost according to the query result. And after the query result of the second table is obtained, rewriting the query statement of the third table according to the query result of the second table, and then querying, thereby realizing statement optimization of multi-table association query of the three tables.
Furthermore, according to the query mode of the three tables in the above example, it can be known that, when there are more tables to be queried in association, the query costs of the tables can be estimated, the tables with the minimum query costs are queried according to the query costs obtained through estimation, the query statements with the minimum query costs are rewritten according to the query results, and the query and the rewriting are sequentially performed until the query results of the tables with the rewritten query statements are obtained.
In practical application, the execution process of the query statement is performed circularly. Sometimes, after the data in the data table has been completely queried, the query process is still in circulation, which may cause the system to still perform circulation query, resulting in wasted query time.
In order to avoid the above situation, in this embodiment of the present specification, after performing query according to the query statement corresponding to the minimum query cost, the method may further include the step of:
and marking the state of the table aimed at by the query statement corresponding to the minimum query cost as queried.
Similarly, in this embodiment of the present specification, after performing a query according to the rewritten query statement, the method may further include:
and marking the state of the table aimed at by the rewritten query statement as queried.
The state of the table is queried, which means that the table is queried completely, and after the subsequent process identifies the state mark, the circular query on the table can not be performed any more.
In practical application, each query result set may include a plurality of pieces of query data, and each query result set is associated to obtain a final associated query result set. The specific association method may be performed by external connection. The external connection comprises a left connection and a right connection. The specific external connection mode is determined by the initial multi-table association query script. In the original multi-table associative query script, left join can be used to represent left join and right join can be used to represent right join.
It should be noted that, in the embodiments of the present specification, the first query statement, the second query statement, and the like are obtained by analyzing the initial multi-table related query script. That is, before the first query statement for the first table is obtained, the following steps may be further included:
acquiring a multi-table association query script;
and analyzing the script to obtain the first query statement and the second query statement.
The analysis of the multi-table association query script can be realized by adopting open source software such as Jsqlparser or druid.
Based on the same idea, the embodiment of the present specification further provides a device corresponding to the above method. Fig. 3 is a schematic structural diagram of a multi-table association query apparatus corresponding to fig. 2 according to an embodiment of the present disclosure. As shown in fig. 3, the apparatus may include:
a first query statement obtaining module 301, configured to obtain a first query statement for a first table; the first query statement is obtained by analyzing a multi-table association query script;
a second query statement obtaining module 302, configured to obtain a second query statement for a second table; the second query statement is obtained by analyzing the multi-table association query script; the query conditions in the first query statement and the second query statement have the same fields;
a first query cost estimation module 303, configured to estimate a first query cost of the first query statement;
a second query cost evaluation module 304, configured to evaluate a second query cost of the second query statement;
a query cost comparing module 305, configured to compare the first query cost with the second query cost, and determine a minimum query cost;
a first query module 306, configured to perform query according to the query statement corresponding to the minimum query cost to obtain a first query result set;
a query statement rewriting module 307, configured to rewrite another query statement according to the first query result set;
a second query module 308, configured to perform a query according to the rewritten query statement to obtain a second query result set;
and an association result generating module 309, configured to obtain an association query result set according to the first query result set and the second query result set.
Optionally, the query statement rewriting module 307 may specifically include:
a query statement rewrite unit configured to define the same field in the other query statement by a value of the first query result set.
Optionally, the apparatus may further include:
the first judging module is used for judging whether the data volume of the first table is larger than a preset threshold value or not before estimating the first query cost of the first query statement to obtain a first judging result;
the first query cost estimation module 303 may be specifically configured to:
and when the first judgment result shows that the data volume of the first table is not larger than the preset threshold, estimating a first query cost of the first query statement.
Optionally, the apparatus may further include:
the third query module is used for querying according to the first query statement to obtain a third query result set when the first judgment result indicates that the data volume of the first table is larger than the preset threshold;
the fourth query module is used for querying according to the second query statement to obtain a fourth query result set;
the association result generating module 309 is further configured to obtain an association query result set according to the third query result set and the fourth query result set.
Optionally, the apparatus may further include:
a third query statement acquisition module configured to acquire a third query statement for a third table; the query conditions in the third query statement have the same field;
a third query cost estimation module for estimating a third query cost of the third query statement;
the query cost comparing module 305 is further configured to, after determining a minimum query cost and before performing query according to the query statement corresponding to the minimum query cost, compare the third query cost with the minimum query cost;
the fourth query module is configured to query according to the third query statement to obtain a fifth query result set if the third query cost is smaller than the minimum query cost;
the query statement rewriting module 307 is further configured to rewrite the query statement corresponding to the minimum query cost according to the fifth query result set;
the first query module 306 is further configured to:
and inquiring according to the inquiry statement corresponding to the rewritten minimum inquiry cost.
Optionally, the apparatus may further include:
and the first marking module is used for marking the state of the table aimed at by the query statement corresponding to the minimum query cost as queried after querying according to the query statement corresponding to the minimum query cost.
Optionally, the apparatus may further include:
and the second marking module is used for marking the state of the table aimed at by the rewritten query statement as queried after querying according to the rewritten query statement.
Optionally, the correlation result generating module 309 is specifically configured to:
and externally connecting the first query result set and the second query result set to obtain a connected query result set.
Optionally, the apparatus may further include:
the query script acquisition module is used for acquiring a multi-table association query script before acquiring a first query statement aiming at a first table;
and the script analysis module is used for analyzing the script to obtain the first query statement and the second query statement.
Based on the same idea, the embodiment of the present specification further provides a device corresponding to the above method.
Fig. 4 is a schematic structural diagram of a multi-table association query device corresponding to fig. 2 provided in an embodiment of this specification. As shown in fig. 4, the apparatus 400 may include:
at least one processor 410; and the number of the first and second groups,
a memory 430 communicatively coupled to the at least one processor; wherein,
the memory 430 stores instructions 420 executable by the at least one processor 410 to enable the at least one processor 410 to:
obtaining a first query statement for a first table; the first query statement is obtained by analyzing a multi-table association query script;
obtaining a second query statement for a second table; the second query statement is obtained by analyzing the multi-table association query script; the query conditions in the first query statement and the second query statement have the same fields;
estimating a first query cost of the first query statement;
estimating a second query cost for the second query statement;
comparing the first query cost with the second query cost to determine a minimum query cost;
inquiring according to the inquiry statement corresponding to the minimum inquiry cost to obtain a first inquiry result set;
rewriting additional query statements according to the first query result set;
inquiring according to the rewritten inquiry statement to obtain a second inquiry result set;
and obtaining an associated query result set according to the first query result set and the second query result set.
In the 90 s of the 20 th century, improvements in a technology could clearly distinguish between improvements in hardware (e.g., improvements in circuit structures such as diodes, transistors, switches, etc.) and improvements in software (improvements in process flow). However, as technology advances, many of today's process flow improvements have been seen as direct improvements in hardware circuit architecture. Designers almost always obtain the corresponding hardware circuit structure by programming an improved method flow into the hardware circuit. Thus, it cannot be said that an improvement in the process flow cannot be realized by hardware physical modules. For example, a Programmable Logic Device (PLD), such as a Field Programmable Gate Array (FPGA), is an integrated circuit whose Logic functions are determined by programming the Device by a user. A digital system is "integrated" on a PLD by the designer's own programming without requiring the chip manufacturer to design and fabricate application-specific integrated circuit chips. Furthermore, nowadays, instead of manually making an integrated Circuit chip, such Programming is often implemented by "logic compiler" software, which is similar to a software compiler used in program development and writing, but the original code before compiling is also written by a specific Programming Language, which is called Hardware Description Language (HDL), and HDL is not only one but many, such as abel (advanced Boolean Expression Language), ahdl (alternate Language Description Language), traffic, pl (core unified Programming Language), HDCal, JHDL (Java Hardware Description Language), langue, Lola, HDL, laspam, hardsradware (Hardware Description Language), vhjhd (Hardware Description Language), and vhigh-Language, which are currently used in most common. It will also be apparent to those skilled in the art that hardware circuitry that implements the logical method flows can be readily obtained by merely slightly programming the method flows into an integrated circuit using the hardware description languages described above.
The controller may be implemented in any suitable manner, for example, the controller may take the form of, for example, a microprocessor or processor and a computer-readable medium storing computer-readable program code (e.g., software or firmware) executable by the (micro) processor, logic gates, switches, an Application Specific Integrated Circuit (ASIC), a programmable logic controller, and an embedded microcontroller, examples of which include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320, the memory controller may also be implemented as part of the control logic for the memory. Those skilled in the art will also appreciate that, in addition to implementing the controller as pure computer readable program code, the same functionality can be implemented by logically programming method steps such that the controller is in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers and the like. Such a controller may thus be considered a hardware component, and the means included therein for performing the various functions may also be considered as a structure within the hardware component. Or even means for performing the functions may be regarded as being both a software module for performing the method and a structure within a hardware component.
The systems, devices, modules or units illustrated in the above embodiments may be implemented by a computer chip or an entity, or by a product with certain functions. One typical implementation device is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
For convenience of description, the above devices are described as being divided into various units by function, and are described separately. Of course, the functionality of the units may be implemented in one or more software and/or hardware when implementing the present application.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
The present invention is described with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flowchart flow or flows and/or block diagram block or blocks.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), other types of Random Access Memory (RAM), Read Only Memory (ROM), Electrically Erasable Programmable Read Only Memory (EEPROM), flash memory or other memory technology, compact disc read only memory (CD-ROM), Digital Versatile Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium such as a modulated data signal and a carrier wave.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
The application may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The application may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media including memory storage devices.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only an example of the present application and is not intended to limit the present application. Various modifications and changes may occur to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.