Database structure comparison method and device, storage medium and program product
By uniformly formatting the table structure information of different types of databases in a preset storage space and adopting a hash value hierarchical comparison mechanism, the compatibility issue of database structure comparison tools is solved, efficient comparison across database types is achieved, and the comparison efficiency and versatility of the tool are improved.
Patent Information
- Application Number
- CN202510894448.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-30
- Publication Date
- 2025-10-14
AI Technical Summary
Existing database structure comparison tools are not compatible with different types of databases, resulting in serious compatibility issues, which limits their versatility and comparison efficiency.
By storing the table structure information of different types of databases in the preset storage space and formatting them uniformly into the same data format, and adopting a hash value hierarchical comparison mechanism, it supports structural comparison of multiple database types, including MySQL, TDSQL, Oracle, GuassDB, etc.
It achieves efficient and accurate structural comparison across database types, breaks through the limitations of a single database type, improves the versatility and applicability of the comparison tool, reduces development and maintenance costs, and supports diverse comparison needs.
Smart Images

Figure CN120780707A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, and particularly relates to a database structure comparison method, device, storage medium and program product. BACKGROUND
[0002] At present, there are many types of databases on the market, such as MySQL, TDSQL, Oracle, SQL Server and the like.
[0003] However, due to the differences in structure definition syntax, metadata storage mode and the like of different types of databases, the current database structure comparison tools mostly only support a single type of database, and the compatibility problem caused by such heterogeneity seriously limits the universality and application range of the database structure comparison tools; in actual use, the efficiency of database structure comparison is seriously reduced.
[0004] Therefore, there is a problem of low efficiency of database structure comparison in the related art. SUMMARY
[0005] The main purpose of the present application is to provide a database structure comparison method, device, storage medium and program product, aiming at solving the technical problem of low efficiency of database structure comparison.
[0006] To achieve the above purpose, the present application provides a database structure comparison method, which comprises:
[0007] In response to a database structure comparison request, target table structure information about a to-be-compared database is obtained from a preset storage space, wherein the preset storage space stores table structure information of different types of databases, and the table structure information stored in the preset storage space has the same data format;
[0008] The target table structure information is compared to obtain a comparison result.
[0009] In an embodiment, the to-be-compared database comprises at least one type, and when the to-be-compared database is a database of the same type, the step of obtaining target table structure information about the to-be-compared database from the preset storage space comprises:
[0010] Target table structure information about the to-be-compared database corresponding to the to-be-compared time is obtained from the preset storage space;
[0011] When the to-be-compared database is a database of different types, the step of obtaining target table structure information about the to-be-compared database from the preset storage space comprises:
[0012] Target table structure information of different types of to-be-compared databases is obtained from the preset storage space.
[0013] In one embodiment, before the step of obtaining the table structure information about the database to be compared from the preset storage space, the method further includes:
[0014] In response to the data configuration request, obtaining a database type and access parameters of the database to be managed;
[0015] Establishing a link with the database to be managed according to the access parameters, and obtaining metadata of the database to be managed through metadata query methods corresponding to different types of databases, wherein the metadata stores original table structure information;
[0016] According to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format;
[0017] The table structure information after the unified data format is stored in the preset storage space.
[0018] In one embodiment, the step of storing the table structure information in a unified data format into a preset storage space includes:
[0019] Encode the table structure information in the unified data format into a string, and store the encoded string in a preset storage space;
[0020] After the step of storing the table structure information in a unified data format into a preset storage space, the method further includes:
[0021] The table structure information after the unified data format is hashed according to the preset levels of the database to obtain the corresponding hash values of each level;
[0022] The hash values corresponding to each level are stored in the preset storage space respectively.
[0023] In one embodiment, after the step of storing the table structure information in a unified data format in a preset storage space, the method further includes:
[0024] At every preset time period, the step of obtaining the metadata of the database to be managed is re-executed, and according to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format, and the table structure information in the unified data format is calculated with a hash value according to the preset hierarchy of the database to obtain the hash value corresponding to each hierarchy, and the recalculated hash value corresponding to each hierarchy is stored in the preset storage space.
[0025] In one embodiment, the preset levels include field level, table level, and library level, and the step of comparing the target comparison data to obtain a comparison result includes:
[0026] According to the preset hierarchy priorities, the newly calculated hash values corresponding to each hierarchy are compared with the previously calculated hash values corresponding to each hierarchy to obtain a comparison result, wherein the priorities are sorted from largest to smallest as library level, table level, and field level;
[0027] The step of comparing the latest calculated hash value corresponding to each level with the previously calculated hash value corresponding to each level according to the preset level priority to obtain a comparison result includes:
[0028] Compare the latest calculated library-level hash value with the last calculated library-level hash value;
[0029] If the latest calculated database-level hash value is different from the last calculated database-level hash value, the latest calculated table-level hash value is compared with the last calculated table-level hash value.
[0030] If the latest calculated table-level hash value is different from the last calculated table-level hash value, the latest calculated field-level hash value is compared with the last calculated field-level hash value.
[0031] In one embodiment, after the step of storing the table structure information in a unified data format in a preset storage space, the method further includes:
[0032] Receiving a structure modification event notification sent by an event trigger corresponding to the database to be managed, wherein the structure modification event notification is sent to a local device when the event trigger detects a structure modification operation of the database;
[0033] The structure modification event notification is parsed to obtain and store modified structure information.
[0034] In addition, to achieve the above-mentioned purpose, the present application also proposes a database structure comparison device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the database structure comparison method as described above.
[0035] In addition, to achieve the above-mentioned purpose, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by the processor, the steps of the database structure comparison method as described above are implemented.
[0036] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, it implements the steps of the database structure comparison method as described above.
[0037] One or more technical solutions proposed in this application have at least the following technical effects:
[0038] In response to a database structure comparison request, the present application can obtain target table structure information about the database to be compared from a preset storage space. Since the preset storage space stores table structure information of different types of databases, the data format of the table structure information stored in the preset storage space is the same. This method can eliminate compatibility issues caused by differences in database structure definition syntax and metadata storage methods. Regardless of the type of database to be compared, its corresponding table structure information can be obtained from the preset storage space, thereby comparing the target table structure information to obtain a comparison result. In actual use, the efficiency of database structure comparison can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0040] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0041] Figure 1 A flowchart of the first embodiment of the database structure comparison method of the present application is provided;
[0042] Figure 2 A schematic diagram of a scenario provided for the first embodiment of the database structure comparison method of this application;
[0043] Figure 3 A flowchart of the second embodiment of the database structure comparison method of this application is provided;
[0044] Figure 4 A schematic diagram of a scenario provided for Example 2 of the database structure comparison method of this application;
[0045] Figure 5 Schematic diagram of the device structure of the hardware operating environment involved in the database structure comparison method in the embodiment of the present application.
[0046] The purpose, features and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0047] It should be understood that the specific embodiments described herein are merely used to explain the technical solutions of the present application and are not intended to limit the present application.
[0048] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0049] Based on this, the embodiment of the present application provides a database structure comparison method, referring to Figure 1 , Figure 1 This is a flowchart of the first embodiment of the database structure comparison method of this application.
[0050] In this embodiment, the database structure comparison method includes steps S10 to S20:
[0051] Step S10, in response to the database structure comparison request, obtaining target table structure information about the database to be compared from a preset storage space, wherein the preset storage space stores table structure information of different types of databases, and the data format of the table structure information stored in the preset storage space is the same;
[0052] It should be noted that the execution subject of this embodiment can be a database structure comparison device, which can be a tablet computer, personal computer, mobile phone, etc., and the database structure comparison tool (software) can be installed in the database structure comparison device.
[0053] The system architecture of the database structure comparison tool includes target database layer, adaptation layer, unified model, snapshot management layer, comparison engine, and user layer. Figure 2 In response to a database structure comparison request, the comparison engine obtains target table structure information about the database to be compared from a preset storage space. The snapshot management layer is used to store table structure information of different types of databases in the preset storage space. The unified model is used to convert table structure information of different types of databases into the same data format.
[0054] It should be noted that the preset storage space can be the local storage space of the database structure comparison device, the database directory of the server corresponding to the database structure comparison tool, or a cloud storage space, etc.; the table structure of the database refers to the organization and definition of the table in the database. The table structure determines how the data is stored, associated and accessed. The main components of the table structure include: Table Name: the unique identifier of the table, Fields (Columns / Fields): field name, data type (such as integer, string, date, etc.), constraints (such as primary key, foreign key, not empty, etc.), default value, Primary Key: the field or field combination that uniquely identifies each record in the table, Foreign Key: the field that establishes the relationship between tables, Indexes: the data structure that improves query performance, Constraints: the rules that ensure data integrity.
[0055] Specifically, since the preset storage space stores table structure information of different types of databases, the data format of the table structure information stored in the preset storage space is the same. This method can eliminate compatibility issues caused by differences in database structure definition syntax and metadata storage methods. Regardless of the type of database to be compared, its corresponding table structure information can be obtained from the preset storage space; the same data format can be JSON, XML or a custom structure, etc.
[0056] In which, the database to be compared includes at least one type, specifically, the database to be compared includes at least one of MYSQL, TDSQL, Oracle, and GuassDB; when the databases to be compared are databases of the same type, the implementation method of obtaining the target table structure information about the database to be compared from the preset storage space can be: obtaining the target table structure information about the database to be compared corresponding to the time to be compared from the preset storage space.
[0057] It can be understood that by "obtaining the target table structure information about the database to be compared corresponding to the time to be compared", structural comparison can be performed between different historical moments of the same type of database, thereby detecting changes in the structure of a database before and after it goes online, comparing historical versions of the same database structure in the test environment and the production environment, or auditing database structure change records, etc.
[0058] In addition, when the databases to be compared are of different types, the implementation of obtaining the target table structure information about the databases to be compared from the preset storage space may be: obtaining target table structure information of different types of databases to be compared from the preset storage space.
[0059] It is understandable that many tools currently on the market can only support table structure comparison between databases of the same type (such as MySQL and MySQL), but cannot handle table structure comparison between databases of different types (such as MySQL and Oracle); this embodiment uniformly extracts and stores table structure information of different database types (MySQL, TDSQL, Oracle, GuassDB, etc.), so that table structure comparison can be performed even if the underlying database syntax and metadata format are different.
[0060] Step S20: Compare the target table structure information to obtain a comparison result.
[0061] Since the table structure information of different types of databases in this embodiment is stored in the preset storage space in the same data format, when performing structure comparison, there is no need to develop specific comparison logic for each database type, which reduces the implementation complexity and makes the comparison process more efficient.
[0062] Specifically, the target table structure information is compared, and a method for obtaining the comparison result may be as follows: the system compares multiple structural dimensions item by item according to preset comparison rules. For example, the table name, field name, data type, constraint condition, default value, primary key, foreign key, index, constraint, etc. may be compared item by item respectively; the comparison result obtained may be the specific dimension that has changed, or it may identify whether the structure has been added, modified, deleted, or the like.
[0063] Furthermore, the user layer can display the comparison results for users to view.
[0064] This embodiment provides a database structure comparison method that supports multiple database types (such as MySQL, TDSQL, Oracle, and GuassDB), and can record and compare snapshots of table structures of different types of databases on a unified platform. By converting the table structure information of different types of databases into a unified format and storing it in a standardized manner, it can complete efficient and accurate structure comparison without relying on the specific syntax and metadata organization of the original database. It breaks through the limitation that traditional database structure comparison tools can only support a single database type, effectively solves the compatibility problems caused by the heterogeneity of different databases in structure definition syntax, metadata storage mechanism, etc., and improves the versatility and applicability of database structure comparison tools. At the same time, by introducing a unified data processing process and comparison logic, the system does not need to develop an adaptation module separately for each database type, reducing development and maintenance costs and improving comparison efficiency.
[0065] In addition, this embodiment supports structural comparison within the same database type (such as different time points of the same MySQL database) and across database types (such as MySQL and Oracle), meeting the diverse comparison needs in practical applications, such as database version management, online change auditing, heterogeneous database migration verification and other scenarios, and has good practical value and promotion prospects.
[0066] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above embodiment 1 can be referred to the above introduction and will not be described in detail later. Figure 3 Before step S10, the database structure comparison method further includes steps S01 to S04:
[0067] Step S01, in response to a data configuration request, obtaining a database type and access parameters of a database to be managed;
[0068] The target database layer can be used to receive data configuration requests initiated by users or external services (for example, adding a new database for management). The target database layer parses the database type to be managed and the access parameters required to connect to the database from the data configuration request. The access parameters include the host address (Host), port number (Port), database name (Database Name), user name (Username), password (Password), connection protocol, etc.; thereby realizing dynamic access and configuration of the database and supporting flexible expansion of multiple database types.
[0069] Step S02: establishing a link with the database to be managed according to the access parameters, and obtaining metadata of the database to be managed through metadata query methods corresponding to different types of databases, wherein the metadata stores original table structure information;
[0070] After obtaining the database type and access parameters of the database to be managed, the target database layer can establish a link with the database to be managed according to the access parameters. The database to be managed may include at least one type of database and at least one database.
[0071] The adaptation layer obtains the metadata of the database to be managed through the metadata query methods corresponding to different types of databases, and thus obtains the original table structure information from the metadata; specifically, the adaptation layer can design a separate "adapter" for each type of database. The "adapter" encapsulates the metadata query logic specific to different types of databases, executes the metadata query statement or calls the interface of the corresponding type of database after connecting to the database, parses the returned results and extracts the required original table structure information.
[0072] Specifically, a unified metadata collection specification has been designed for collecting table structure information from four types of databases: MySQL, Oracle, TDSQL, and GaussDB. Specifically:
[0073] Different types of core metadata collection items include: Basic field information: Accurately obtain the field name, data type (adapting to the specific type of each database), data length / precision, whether NULL values are allowed, default value, auto-increment attribute, and field comments of each table; Primary key identification: Identify the primary key field name, composite primary key composition, and primary key constraint name of the table; Index analysis: Collect all index information (normal / unique / full-text index), including index name, included fields, sorting method, and index type; Constraint management: Obtain specific condition expressions for CHECK constraints and UNIQUE constraints; Foreign key relationships: Analyze the associated tables, associated fields, and update / delete rules (CASCADE / SET NULL, etc.) of foreign key constraints; Triggers: Record the trigger name, trigger event (INSERT / UPDATE / DELETE), trigger timing (BEFORE / AFTER), and trigger status; Table attribute supplement: Collect additional information such as storage engine (MySQL), tablespace (Oracle), table comments, and row count estimation.
[0074] Furthermore, different types of metadata are collected in the following order of priority: infrastructure (table name, field definition, primary key), relational objects (foreign keys, constraints), and code objects (triggers, stored procedures). Metadata of different types is organized into three levels: library, schema (Schema refers to the organizational structure and definition of data in a database. Generally speaking, it is a collection of database objects (such as tables, views, indexes, functions, etc.) that describes the logical structure and relationships between these objects), and table. Each record contains metadata such as timestamps and version numbers for subsequent table structure comparisons.
[0075] The above approach can control the complexity of collection, avoid performance issues caused by loading too many objects at one time, and facilitate subsequent incremental updates or structural change detection.
[0076] Specifically, for MySQL databases, metadata can be queried through the information_schema+SHOW command; for Oracle databases, metadata can be queried through data dictionary views such as ALL_TAB_COLS, ALL_CONSTRAINTS, ALL_CONS_COLUMNS; for TDSQL / GaussDB databases, metadata can be queried through the pg_catalog system catalog + psql meta command.
[0077] Step S03: converting the original table structure information of the database to be managed into the target data format according to the preset format mapping rule to obtain the table structure information in a unified data format;
[0078] It should be noted that the preset format mapping rules are used to normalize the field types of different types of databases into a standard format, for example: Oracle's NUMBER (10,0) is mapped to INT, MySQL's TINYINT (1) is mapped to BOOLEAN, PostgreSQL's SERIAL is mapped to INT, and GaussDB's BIGSERIAL is mapped to BIGINT; this solves the problem of inconsistent data types between different types of databases and improves the accuracy of structure comparison (avoiding misjudgment of type differences).
[0079] At the same time, different databases use different syntaxes to define field comments. For example, MySQL uses COMMENT'xxx' to define field comments, Oracle uses COMMENT ON COLUMN table.field IS'xxx' to define field comments, and PostgreSQL / GaussDB uses COMMENT ON COLUMN to define field comments. These syntaxes will be recognized during collection, and the comment content will be extracted. Finally, it will be saved in a unified standard field format to retain semantic information and improve structural documentation capabilities. During the comparison process, the situation of "only comment changes" can be identified.
[0080] In addition, different databases use different mechanisms to implement auto-increment fields. For example, MySQL uses AUTO_INCREMENT, Oracle uses SEQUENCE+TRIGGER, and PostgreSQL / GaussDB uses SERIAL / BIGSERIAL. The adaptation layer parses these features and marks whether the field is an "auto-increment field," thereby uniformly representing the logical "primary key auto-increment" attribute and avoiding deviations in comparison results due to different implementation mechanisms.
[0081] Based on the above rules, according to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format; specifically, the present invention defines a target data storage structure for recording the table structure of each table under the database, wherein the target data storage structure includes the field name, field type, data length, whether it is allowed to be empty, whether it is auto-incremented, field comments, the name of the primary key in the table, the constraint type, the foreign key in the table, the trigger and the index, etc. of each table, with reference to Figure 4 .
[0082] Step S04: storing the table structure information in a unified data format into a preset storage space.
[0083] Furthermore, these structural information in a unified format can be written into a centrally managed storage space so that the table structure information can be quickly read when needed later; specifically, the preset storage space can also be a document-based database (for example, MongoDB, Elasticsearch), which facilitates subsequent full-text retrieval and fast access.
[0084] The above method in this embodiment uses a factory pattern to encapsulate the connection operations of four databases: MySQL, Oracle, TDSQL, and GaussDB. Users only need to pass in the database type, as well as access parameters such as the connection address, account number, and password, to automatically select the corresponding driver to establish a connection and provide a standardized query interface and result return format. By abstracting the database connection logic, the differences between different databases in connection parameter configuration, SQL syntax support, transaction control mechanism, etc. are effectively shielded, so that the business code does not need to pay attention to the specific implementation details of the underlying database, thereby significantly improving the maintainability and scalability of the system.
[0085] At the same time, the system has a built-in complete connection pool management mechanism to support connection reuse and resource optimization in high-concurrency scenarios; an integrated transaction control module to ensure the consistency and integrity of multi-operation transactions; and a unified exception handling layer to capture and convert error information unique to each database, thereby improving the robustness and fault tolerance of the system.
[0086] Specifically, the implementation method of storing the table structure information in a unified data format into a preset storage space may be:
[0087] The table structure information in the unified data format is encoded into a string, and the encoded string is stored in a preset storage space.
[0088] After the table structure information is standardized into a unified format such as JSON or XML, in order to save space, it can be stored directly in a structured form (such as fields or objects). Instead, the table structure information in the unified data format can be converted into a string format (such as a JSON string) and then processed using Base64 encoding. Finally, the Base64-encoded string is stored in a preset storage space such as a database or file system, thereby improving data storage efficiency.
[0089] This method achieves consistent data storage across platforms and environments through unified encoding of structural information, and has certain security and compatibility advantages.
[0090] Furthermore, after the step of storing the table structure information in the unified data format in the preset storage space, you can also: calculate the hash value of the table structure information in the unified data format according to the preset level of the database to obtain the hash value corresponding to each level; and store the hash value corresponding to each level in the preset storage space respectively.
[0091] Specifically, the hash value can be calculated through SHA-256 (Secure Hash Algorithm 256-bit, a cryptographic hash function). The snapshot management layer can store the original database result in plain text in the snapshot initial table of the preset storage space, and record the hash value of the data in segments.
[0092] Specifically, SHA-256 calculation can be performed on the definition content of each field (such as field name, type, length, whether it is empty, etc.), the hash values of all fields are sorted and spliced into a string, and SHA-256 is used to calculate again to generate a table-level fingerprint; the table-level hash values of all tables are sorted and spliced, and SHA-256 is used again to calculate to generate a global fingerprint of the entire database.
[0093] The above method can quickly determine whether there are any changes to the entire database structure; it supports full structure consistency verification.
[0094] It is understandable that current database structure comparison tools typically generate full snapshots and record all database structure information. This requires a full comparison during comparison, which consumes a lot of data storage space and is inefficient. The snapshot management layer of this embodiment collects and stores all the table structure information of the entire database once it is first run (a full snapshot). If the database table results subsequently change, only the changed structure can be recorded (incremental updates). The structure information is converted into a hash value and stored for quick identification of changes and table structure comparison.
[0095] Specifically, after the step of storing the table structure information in a unified data format in a preset storage space, the following steps may be performed:
[0096] Receive a structure modification event notification sent by an event trigger corresponding to the database to be managed, wherein the structure modification event notification is sent to the local when the event trigger detects a structure modification operation of the database; parse the structure modification event notification, obtain and store the modified structure information.
[0097] This embodiment uses event triggers configured on the managed database itself to monitor all DDL (Data Definition Language) operations involving structural changes (e.g., CREATE, ALTER, DROP, and other structural modification operations). Since the managed database is linked to the local system, when a structural modification operation is detected, the event trigger automatically sends relevant information (e.g., SQL statement, operation time, user, etc.) to the local system via a structural modification event notification.
[0098] After receiving an event notification, this embodiment parses it to extract the specific structural changes. A structural modification event notification may include: which table was modified? Which field was added / deleted? Was the field type or length modified? Did the primary key, index, or foreign key be affected? Furthermore, the parsed structural change information is incrementally written to a pre-set storage space. Several storage strategies are supported: incremental logging (recording an operation log for each change); structural versioning (maintaining multiple versions of structural snapshots for each table); and hash fingerprint updates (updating only the hash value at the level where the change occurred, such as the field level, table level, or library level).
[0099] This embodiment introduces an event monitoring mechanism and utilizes the DDL trigger or event subscription function provided by the managed database to obtain structural change information in real time, avoiding the problems of storage space waste and low comparison efficiency caused by traditional tools using a full snapshot method for structural collection.
[0100] The implementation methods for different database types are as follows: Oracle enables DDL triggers and pushes events through DBMS_ALERT; MySQL parses DDL events in the binlog (binlog_rows_query_log_events needs to be enabled) or uses performance_schema to capture DDL statements; TDSQL / GaussDB creates event triggers to listen to the pg_ddl_command_end event; thereby achieving real-time perception, efficient collection, and accurate recording of database structure changes, significantly improving the response speed, resource utilization, and auditability of the structure management system.
[0101] In addition, after the step of storing the table structure information in the unified data format into the preset storage space, the following steps may be performed:
[0102] At every preset time period, the step of obtaining the metadata of the database to be managed is re-executed, and according to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format, and the table structure information in the unified data format is calculated with a hash value according to the preset hierarchy of the database to obtain the hash value corresponding to each hierarchy, and the recalculated hash value corresponding to each hierarchy is stored in the preset storage space.
[0103] Since some types of databases are incompatible with event triggers, in scenarios where a monitoring mechanism or subscription mechanism cannot be used, this embodiment uses a full table scan to scan the database structure and only records the incremental part. Specifically, a structure collection task (which can be configured as a scheduled task (Cron Job, Quartz scheduling, etc.)) can be automatically triggered every preset time period (such as every day or every hour), and the above-mentioned process of obtaining the metadata of the database to be managed is re-executed. According to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format. The hash value of the table structure information in the unified data format is calculated according to the preset hierarchy of the database to obtain the corresponding hash value of each hierarchy.
[0104] The recalculated corresponding hash values of each level are stored in the preset storage space to support versioning records, and whether changes have occurred is determined by comparing the old and new hash fingerprints; if no changes have occurred, storage is skipped; if changes have occurred, the change details are recorded as "incremental updates"; thereby achieving efficient structural change tracking and storage management, ensuring that in the absence of a real-time event notification mechanism, the collection, comparison and version control of the database structure can still be effectively completed, thereby improving the applicability and stability of the system.
[0105] In addition, in scenarios where a monitoring mechanism or a subscription mechanism is not possible, relying on a polling scanning method is inefficient and has high latency. In this embodiment, a lightweight eBPF (Extended Berkeley Packet Filter) probe can be deployed in the operating system kernel layer to dynamically intercept the system call chain of the database process (such as writev, fsync), and identify DDL operations through instruction features; bypassing the database's own log mechanism, directly analyzing the system call context (such as Redis's CONFIG SET will trigger the characteristic byte order of writev writing the configuration file), and using a JIT (just-in-time compilation) compiled eBPF (Extended Berkeley Packet Filter, virtual machine program) program to filter non-DDL operations in real time.
[0106] Specifically, the preset levels include field level, table level, and library level. The comparison of the target comparison data to obtain the comparison result may be implemented as follows:
[0107] According to the priority of the preset levels, the latest calculated hash value corresponding to each level is compared with the last calculated hash value corresponding to each level to obtain a comparison result, wherein the priority is sorted from large to small as library level, table level and field level.
[0108] When comparing the target comparison data, this embodiment can perform a progressive comparison layer by layer according to priority: for example, first compare the global level (library level), then refine it to a single table (table level), and finally go deep into the field (field level); only continue to the next level of comparison when changes are found, avoiding unnecessary full comparison.
[0109] Among them, the hierarchical priority design can ensure the comparison logic from macro to micro, thereby improving efficiency.
[0110] Specifically, the implementation method of comparing the latest calculated hash value corresponding to each level with the last calculated hash value corresponding to each level according to the priority of the preset levels to obtain the comparison result may be:
[0111] Compare the latest calculated database-level hash value with the last calculated database-level hash value. If the latest calculated database-level hash value is different from the last calculated database-level hash value, continue to compare the latest calculated table-level hash value with the last calculated table-level hash value. If the latest calculated table-level hash value is different from the last calculated table-level hash value, continue to compare the latest calculated field-level hash value with the last calculated field-level hash value.
[0112] For example, the sum of the structural fingerprints of all tables in the entire database is calculated (such as sorting all table-level hashes, concatenating them, and then hashing them); the current database-level hash is compared with the historical records; if they are consistent, the subsequent comparison is skipped directly; if they are inconsistent, the next stage is entered; this allows for quick determination of whether the entire database has changed, saving resources and avoiding meaningless table-by-table comparisons.
[0113] Further traverse all tables in the current database, obtain the table-level hash of each table (hash after field hash aggregation), and compare it with the hash value of the table in the historical record; determine the result type: newly added table (does not exist in history), deleted table (does not exist currently but existed in history), modified table (hash inconsistent); use thread pool or asynchronous task scheduler to process the comparison of multiple tables in parallel, and improve the response speed in large-scale database environments.
[0114] Furthermore, you can traverse the field list of the changed table and compare the definition hash of each field to determine whether the field has been: added (the field does not exist in the original structure), deleted (the field is missing in the new structure), or modified (the field definition hash is inconsistent).
[0115] In this embodiment, efficient management and accurate comparison of multi-type database structures are achieved through mechanisms such as unified data format conversion, hierarchical hash calculation and storage, scheduled task collection, and event-driven monitoring. Progressive structural comparison is performed through a three-level hashing system at the field, table, and database levels, significantly improving comparison efficiency and reducing resource consumption. Support for both scheduled full-volume scans and real-time event monitoring ensures comprehensive capture and rapid response to structural changes. The hash fingerprint mechanism not only supports structural consistency verification but also facilitates version management and audit tracking.
[0116] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the database structure comparison method of the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.
[0117] The present application provides a database structure comparison device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the database structure comparison method in the above-mentioned embodiment one.
[0118] Reference below Figure 5 , which shows a schematic diagram of a database structure comparison device suitable for implementing the embodiments of the present application. The database structure comparison device in the embodiments of the present application may include, but is not limited to, mobile terminals such as mobile phones, tablet computers, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), and fixed terminals such as digital televisions and desktop computers. Figure 5 The database structure comparison device shown is merely an example and should not limit the functions and scope of use of the embodiments of the present application.
[0119] like Figure 5As shown, the database structure comparison device may include a processing device 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes based on programs stored in a read-only memory (ROM) 1002 or programs loaded from a storage device 1003 into a random access memory (RAM) 1004. RAM 1004 also stores various programs and data required for the operation of the database structure comparison device. Processing device 1001, ROM 1002, and RAM 1004 are interconnected via a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Typically, the following systems can be connected to I / O interface 1006: input device 1007 including, for example, a touch screen, touchpad, keyboard, mouse, image sensor, microphone, accelerometer, gyroscope, etc.; output device 1008 including, for example, a liquid crystal display (LCD), speaker, vibrator, etc.; storage device 1003 including, for example, a magnetic tape, hard disk, etc.; and communication device 1009. Communication device 1009 can allow the database structure comparison device to communicate with other devices wirelessly or by wire to exchange data. Although the figure shows a database structure comparison device with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or provided instead.
[0120] In particular, according to the embodiments disclosed in the present application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program comprising program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0121] The database structure comparison device provided in this application utilizes the database structure comparison method of the above-described embodiment to address the technical issue of inefficient database structure comparison. Compared to the prior art, the beneficial effects of the database structure comparison device provided in this application are the same as those of the database structure comparison method provided in the above-described embodiment. Other technical features of the database structure comparison device are the same as those disclosed in the above-described embodiment and are not further elaborated upon here.
[0122] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0123] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
[0124] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer program) stored thereon, and the computer-readable program instructions are used to execute the database structure comparison method in the above embodiment.
[0125] The computer-readable storage medium provided in this application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with 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 embodiment, the computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.
[0126] The computer-readable storage medium may be included in the database structure comparison device; or it may exist independently without being assembled into the database structure comparison device.
[0127] The computer-readable storage medium carries one or more programs. When the one or more programs are executed by the database structure comparison device, the database structure comparison device is enabled to execute the database structure comparison method.
[0128] Computer program code for carrying out operations of the present application can be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code can execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, 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 the connection can be made to an external computer (for example, through the Internet using an Internet Service Provider).
[0129] The flow diagrams and the block diagrams in the drawings are meant as methodological and functional description of implementations of systems, methods, and computer program products according to the embodiments of the present application. In this regard, each block in the flow diagrams or block diagrams can represent a module, a segment, or a portion of code, which comprises one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions noted in the blocks can occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently or the blocks may
[0130] The modules involved in the embodiments of the present application can be implemented in software or in hardware. In some cases, the name of the module does not constitute a limitation on the module itself.
[0131] The readable storage medium provided by the present application is a computer readable storage medium, which stores computer readable program instructions (i.e. computer program) for executing the database structure comparison method described above, and can solve the technical problem of low efficiency of database structure comparison. Compared with the prior art, the computer readable storage medium provided by the present application has the same beneficial effects as the database structure comparison method provided by the above embodiments, which will not be described here.
[0132] The present application also provides a computer program product, comprising a computer program, which implements the steps of the above-mentioned database structure comparison method when executed by a processor.
[0133] The computer program product provided in this application can solve the technical problem of low efficiency in database structure comparison. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as those of the database structure comparison method provided in the above embodiment, and will not be repeated here.
[0134] The above descriptions are only some of the embodiments of this application and do not limit the scope of the patent application. All equivalent structural transformations made by utilizing the contents of this application specification and drawings, or direct or indirect application in other related technical fields under the technical concept of this application are included in the scope of patent protection of this application. All actions of obtaining signals, information or data in this application are carried out in compliance with the relevant data protection laws and policies of the country where they are located and with the authorization of the corresponding device owner.
Claims
1. A database structure comparison method, characterized in that: The method includes: In response to a database structure comparison request, obtaining target table structure information about a database to be compared from a preset storage space, wherein the preset storage space stores table structure information of different types of databases, and the table structure information stored in the preset storage space has the same data format; The target table structure information is compared to obtain a comparison result.
2. The method according to claim 1, wherein The databases to be compared include at least one type. When the databases to be compared are of the same type, the step of obtaining target table structure information about the databases to be compared from the preset storage space includes: Obtaining target table structure information of the database to be compared corresponding to the time to be compared from a preset storage space; When the databases to be compared are databases of different types, the step of obtaining target table structure information about the databases to be compared from the preset storage space includes: Obtain target table structure information of different types of databases to be compared from the preset storage space.
3. The method according to claim 1, wherein Before the step of obtaining the table structure information of the database to be compared from the preset storage space, the method further includes: In response to the data configuration request, obtaining a database type and access parameters of the database to be managed; Establishing a link with the database to be managed according to the access parameters, and obtaining metadata of the database to be managed through metadata query methods corresponding to different types of databases, wherein the metadata stores original table structure information; According to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format; The table structure information after the unified data format is stored in the preset storage space.
4. The method according to claim 3, wherein The step of storing the table structure information in a unified data format into a preset storage space includes: Encode the table structure information in the unified data format into a string, and store the encoded string in a preset storage space; After the step of storing the table structure information in a unified data format into a preset storage space, the method further includes: The table structure information after the unified data format is hashed according to the preset levels of the database to obtain the corresponding hash values of each level; The hash values corresponding to each level are stored in the preset storage space respectively.
5. The method according to claim 4, wherein After the step of storing the table structure information in a unified data format into a preset storage space, the method further includes: At every preset time period, the step of obtaining the metadata of the database to be managed is re-executed, and according to the preset format mapping rules, the original table structure information of the database to be managed is converted into the target data format to obtain the table structure information in a unified data format, and the table structure information in the unified data format is calculated with a hash value according to the preset hierarchy of the database to obtain the hash value corresponding to each hierarchy, and the recalculated hash value corresponding to each hierarchy is stored in the preset storage space.
6. The method according to claim 4, wherein The preset levels include field level, table level and library level. The step of comparing the target comparison data to obtain the comparison result includes: According to the preset hierarchy priorities, the newly calculated hash values corresponding to each hierarchy are compared with the previously calculated hash values corresponding to each hierarchy to obtain a comparison result, wherein the priorities are sorted from largest to smallest as library level, table level, and field level; The step of comparing the latest calculated hash value corresponding to each level with the previously calculated hash value corresponding to each level according to the preset level priority to obtain a comparison result includes: Compare the latest calculated library-level hash value with the last calculated library-level hash value; If the latest calculated database-level hash value is different from the last calculated database-level hash value, the latest calculated table-level hash value is compared with the last calculated table-level hash value. If the latest calculated table-level hash value is different from the last calculated table-level hash value, the latest calculated field-level hash value is compared with the last calculated field-level hash value.
7. The method according to claim 3, wherein After the step of storing the table structure information in a unified data format into a preset storage space, the method further includes: Receiving a structure modification event notification sent by an event trigger corresponding to the database to be managed, wherein the structure modification event notification is sent to a local device when the event trigger detects a structure modification operation of the database; The structure modification event notification is parsed to obtain and store modified structure information.
8. A database structure comparison device, characterized in that: The device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the database structure comparison method according to any one of claims 1 to 7.
9. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the database structure comparison method according to any one of claims 1 to 7 are implemented.
10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the database structure comparison method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Database integrity protection method and device, equipment and storage medium
CN121365430A