Database table fragmentation rate detection method and device, equipment and storage medium
By backing up the database table to the second database and performing defragmentation on a different machine, the problem of inaccurate assessment of database table fragmentation rate in the prior art is solved, and the accuracy of assessment and resource utilization efficiency are improved.
Patent Information
- Application Number
- CN202410613143.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-16
- Publication Date
- 2024-08-20
AI Technical Summary
When the existing technology evaluates the fragmentation rate of a database table, high sampling frequency leads to high resource consumption, and low sampling frequency results in untimely and inaccurate statistical information, making it difficult to accurately evaluate the fragmentation rate of the database table.
By backing up the database table to the second database, defragmenting it on a different machine, and calculating the fragmentation rate, we avoid relying on the sampling frequency and sample number, and improve the accuracy of the evaluation.
It achieves the accuracy of database table fragmentation rate assessment, reduces resource consumption, and avoids the problem of untimely update of statistical information.
Smart Images

Figure CN118519829A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of data processing technology, and in particular to a method, device, equipment and storage medium for detecting the fragmentation rate of a database table. Background Art
[0002] Common databases such as MySQL and Oracle store data in different tables. Deleting table data will leave blanks on the page. Adding and modifying data will also cause random additions and modifications to the corresponding secondary index values, resulting in blanks on the data pages in the index structure. Although these blanks may be reused, they will eventually cause part of the database's physical space to be unused. These unused physical spaces in the database are fragments. Fragments not only affect the utilization of data storage space, but also affect business continuity and lead to low access efficiency. Therefore, it is very important to evaluate the fragmentation rate before defragmentation.
[0003] In the prior art, by sampling the tables in the database and counting the relevant information of the tables and indexes, such as the number of records in the table, the number of clustered index pages, the Cardinality of the field, etc., the average fragment size of each page in the table is calculated by querying the relevant information of the table and index, thereby obtaining the fragmentation rate of the entire table. However, high sampling frequency and large sample size may lead to large resource consumption and affect database performance, while low sampling frequency and small sample size may lead to untimely and inaccurate statistical information updates, reduce the effect of database fragmentation rate evaluation, and make it difficult to accurately evaluate the fragmentation rate of database tables. Summary of the invention
[0004] The embodiments of the present application provide a method, apparatus, device and storage medium for detecting the fragmentation rate of a database table, which effectively improves the accuracy of evaluating the fragmentation rate of the database table.
[0005] In a first aspect, an embodiment of the present application provides a method for detecting a fragmentation rate of a database table, comprising:
[0006] Obtaining a first file size of each of a plurality of database tables included in the first database;
[0007] Backing up the multiple database tables included in the first database to the second database;
[0008] For the multiple database tables included in the second database, perform the following operations respectively:
[0009] Defragmenting a database table and obtaining a second file size of the defragmented database table;
[0010] A fragmentation rate of the database table is obtained based on the first file size and the second file size of the database table.
[0011] In an embodiment of the present application, firstly, a first file size of each of the multiple database tables included in the first database is obtained, and the multiple database tables included in the first database are backed up to the second database, and the multiple database tables included in the first database are backed up on a different machine and restored to the second database, and then the multiple database tables are defragmented on the different machine to obtain the second file size corresponding to each database table, and finally, the fragmentation rate of each of the multiple database tables is obtained by the first file size of each of the multiple database tables before defragmentation and the second file size after defragmentation. The entire evaluation process does not need to pay attention to the sampling frequency and the number of samples, and is not performed on the source database, which effectively improves the accuracy of the fragmentation rate evaluation of the database tables.
[0012] In an optional implementation, before backing up the multiple database tables included in the first database to the second database, it is monitored that the first database and the second database are in normal status, and the backup images of the first database and the second database are respectively in available status.
[0013] In an optional implementation, a full database backup is performed locally on the first database to obtain local backup data;
[0014] The multiple database tables are backed up from the local backup data to a second database.
[0015] In an optional implementation, obtaining a backup type between the first database and the second database;
[0016] Acquire the multiple database tables from the local backup data according to the backup type;
[0017] The plurality of database tables are backed up to a second database.
[0018] In an optional implementation, the one database table includes multiple physical files, the first file size is the sum of the file sizes of the multiple physical files before defragmentation; the second file size is the sum of the file sizes of the multiple physical files after defragmentation.
[0019] In an optional implementation, determining a difference between the first file size and the second file size;
[0020] The ratio of the difference to the first file size is used as the fragmentation rate of the database table.
[0021] In an optional implementation, after obtaining the fragmentation rate of the one database table, if the second file size is greater than a first preset threshold and the fragmentation rate is greater than a second preset threshold, an alarm is triggered for the one database table.
[0022] In a second aspect, an embodiment of the present application provides a database table fragmentation rate detection device, including:
[0023] An acquiring unit, configured to acquire a first file size of each of a plurality of database tables included in the first database;
[0024] A backup unit, used for backing up a plurality of database tables included in the first database to a second database;
[0025] The fragmentation assessment unit is configured to respectively perform the following operations on the multiple database tables included in the second database:
[0026] Defragmenting a database table and obtaining a second file size of the defragmented database table;
[0027] A fragmentation rate of the database table is obtained based on the first file size and the second file size of the database table.
[0028] In an optional implementation, it further includes a status monitoring unit;
[0029] The state monitoring unit is specifically used for:
[0030] Before backing up the multiple database tables included in the first database to the second database, it is monitored that the first database and the second database are in a normal state, and the backup images of the first database and the second database are respectively in an available state.
[0031] In an optional implementation manner, the backup unit is further used for:
[0032] Performing a full database backup locally on the first database to obtain local backup data;
[0033] The multiple database tables are backed up from the local backup data to a second database.
[0034] In an optional implementation manner, the backup unit is further used for:
[0035] Obtaining a backup type between the first database and the second database;
[0036] Acquire the multiple database tables from the local backup data according to the backup type;
[0037] The plurality of database tables are backed up to a second database.
[0038] In an optional implementation manner, the fragment assessment unit is specifically used to:
[0039] Determining a difference between the first file size and the second file size;
[0040] The ratio of the difference to the first file size is used as the fragmentation rate of the database table.
[0041] In an optional implementation, it further includes an alarm unit;
[0042] The alarm unit is specifically used for:
[0043] After obtaining the fragmentation rate of the one database table, if the second file size is greater than a first preset threshold and the fragmentation rate is greater than a second preset threshold, an alarm is triggered for the one database table.
[0044] In a third aspect, an embodiment of the present application provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above-mentioned database table fragmentation rate detection method when executing the program.
[0045] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium storing a computer program executable by a computer device. When the program runs on the computer device, the computer device executes the steps of the above-mentioned database table fragmentation rate detection method.
[0046] In a fifth aspect, an embodiment of the present application provides a computer program product, which includes a computer program stored on a computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer device, the computer device executes the steps of the above-mentioned database table fragmentation rate detection method. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0048] Figure 1 A schematic diagram of a system architecture provided in an embodiment of the present application;
[0049] Figure 2 A schematic diagram of a flow chart of a method for detecting the fragmentation rate of a database table provided in an embodiment of the present application;
[0050] Figure 3 A schematic diagram of a method flow of a scheduling process provided in an embodiment of the present application;
[0051] Figure 4 A schematic diagram of a method flow of a heterogeneous machine recovery process provided in an embodiment of the present application;
[0052] Figure 5 A schematic diagram of a method flow chart of a fragmentation rate evaluation process provided in an embodiment of the present application;
[0053] Figure 6 A schematic diagram of a method flow of an alarm process provided in an embodiment of the present application;
[0054] Figure 7 A schematic diagram of the structure of a database table fragmentation rate detection device provided in an embodiment of the present application;
[0055] Figure 8 A schematic diagram of the structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0056] In order to make the purpose, technical solution and beneficial effects of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0057] For ease of understanding, the terms involved in the embodiments of the present invention are explained below.
[0058] Fragmentation: Unused physical space in a database.
[0059] Defragmentation: Move all database tables and index data in the current database file to the new database file one by one, rearranging and compacting the data in the process.
[0060] Database as a Service: Database as a Service is a service capability based on traditional database technology, which provides database resources to one or more tenants in the form of standard services. The Database as a Service platform provides users with a unified database access service.
[0061] Kubernetes: (abbreviated as k8s, application software) is an open source containerized application for managing multiple hosts in a cloud platform. The goal of Kubernetes is to make the deployment of containerized applications simple and efficient.
[0062] Swarm: It is a container cluster management system developed by Docker. In the early days, Swarm existed as an independent service. The cluster management and orchestration functions of Swarm were integrated into Docker Engine v1.12.
[0063] Xtrabackup: is a tool for backing up InnoDB data, supporting online hot backup (backup does not affect data reading and writing).
[0064] See also Figure 1 , which is a system architecture diagram applicable to an embodiment of the present application, and the system architecture at least includes a detection system 101, a first database 102 and a second database 103.
[0065] The number of detection systems 101 may be one or more, and the number of first databases 102 and second databases 103 may also be one or more. This application does not specifically limit the number of detection systems 101, first databases 102 and second databases 103.
[0066] The detection system 101 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms. The detection system 101 can be directly or indirectly connected to the first database 102 and the second database 103 by wired or wireless communication, and this application does not limit this.
[0067] Database as a Service (DBaaS) is a service that pools database resources and delivers them to users through the network in a cloud service model. Different companies use different resource pooling and isolation methods when implementing DBaaS. Some use the physical resource isolation mechanism provided by the Linux kernel (full name is Control Groups, Cgroups for short) to achieve this, while others use virtual machine technology to achieve this. The effects of database cloud platforms built based on different resource isolation mechanisms are also different.
[0068] It should be noted that the embodiment of the present application adopts a database cloud platform based on container technology to complete database container deployment and management. The container technology can be: kubernetes (abbreviated as k8s, application software) or swarm, etc. Each database in the platform is an independent container deployed on the host machine, and the container is configured with a dedicated IP address, central processing unit (abbreviated as CPU), memory, table space and log space.
[0069] based on Figure 1 The system architecture diagram shown in the embodiment of the present application provides a schematic diagram of the process of detecting the fragmentation rate of a database table. The process of the method is executed by the detection system 101, such as Figure 2 As shown, the specific steps include:
[0070] Step 201: Obtain a first file size of each of a plurality of database tables included in a first database.
[0071] Specifically, a database table contains multiple physical files, and the first file size is the sum of the file sizes of multiple physical files before defragmentation. For example, the first file size of the tb1 database table in the database db1 is db1_tb1_size1, and the first file size is obtained by adding the file sizes of all physical files in the data directory of the database db1. For example, the database table chk has a table structure file chk.frm and a data file chk.ibd in the data directory, and the file sizes are 8586 and 98304 bytes respectively, then the first file size of the database table chk is 106620, in bytes.
[0072] In an optional implementation, before backing up the multiple database tables included in the first database to the second database, it is monitored that the first database and the second database are in a normal state, and the backup images of the first database and the second database are in an available state.
[0073] Specifically, before backing up the multiple database tables contained in the first database to the second database, it is necessary to first check the status of the first database on the database platform. If the status of the first database is abnormal, perform exception handling and end; if the first database is in a normal state, obtain the backup files of the multiple database tables of the first database. Then check whether the backup image of the first database is available. If it is not available, perform exception handling and end; if the backup image of the first database is available, check the status of the second database on the database platform. If the status of the second database is abnormal, perform exception handling and end; if the second database is in a normal state, check whether the backup image of the second database is available. If it is available, back up the multiple database tables contained in the first database to the second database. Among them, the exception handling process mainly records the relevant logs of the exception handling, including the exception handling time, the operation steps of the exception handling, etc.
[0074] Step 202: Back up multiple database tables included in the first database to the second database.
[0075] In an optional implementation, a full database backup is performed locally on the first database to obtain local backup data;
[0076] Back up multiple database tables from the local backup data to the second database.
[0077] Specifically, a full database backup is performed locally on the first database, and a backup file list of the first database is obtained, including: the name of the first database, the backup time, the full database backup status, the verification status and the mirror status. The verification status of the first database is checked. If the verification status is successful, the operation is terminated. If it is not verified or the verification status is failed, the full database backup status of the first database is checked. If the full database backup status is a successful backup, the latest local backup data is obtained, and multiple database tables are backed up to the second database from the local backup data.
[0078] In an optional implementation, a backup type between the first database and the second database is obtained;
[0079] Obtain multiple database tables from local backup data according to the backup type;
[0080] Back up multiple database tables to a secondary database.
[0081] Specifically, the backup type between the first database and the second database is obtained. If the backup type is a full database physical backup, multiple database tables are obtained from the local backup data, and the multiple database tables are backed up to the second database. The status of the database tables is checked. If the database tables are in a normal state, the database tables are successfully backed up to the second database. If the backup type is a table guide backup or a table result backup, an exception is handled and the process ends.
[0082] Step 203: for the multiple database tables included in the second database, perform the following operations respectively:
[0083] A database table is defragmented and a second file size of the defragmented database table is obtained; and a fragmentation rate of the database table is obtained based on the first file size and the second file size of the database table.
[0084] Specifically, the second file size is the sum of file sizes of multiple physical files after defragmentation, the difference between the first file size and the second file size is determined, and the ratio of the difference to the first file size is used as the fragmentation rate of a database table. If the first file size of the tb1 database table in the database db1 is db1_tb1_size1, and the second file size is db1_tb1_size2, then the calculation method of the fragmentation rate of the tb1 database table in the database db1 is: fragmentation_rat = (db1_tb1_size1-db1_tb1_size2) / db1_tb1_size2*100. For example, in the database db1, the first file size of the tb1 database table is 7893013, and the second file size is 6414410, then the fragmentation rate of the tb1 database table is (7893013-6414410) / 7893013*100=18.7%.
[0085] In an embodiment of the present application, firstly, a first file size of each of the multiple database tables included in the first database is obtained, and the multiple database tables included in the first database are backed up to the second database, and the multiple database tables included in the first database are backed up on a different machine and restored to the second database, and then the multiple database tables are defragmented on the different machine to obtain the second file size corresponding to each database table, and finally, the fragmentation rate of each of the multiple database tables is obtained by the first file size of each of the multiple database tables before defragmentation and the second file size after defragmentation. The entire evaluation process does not need to pay attention to the sampling frequency and the number of samples, and is not performed on the source database, which effectively improves the accuracy of the fragmentation rate evaluation of the database tables.
[0086] In an optional implementation, after obtaining the fragmentation rate of a database table, if the second file size is greater than a first preset threshold and the fragmentation rate is greater than a second preset threshold, an alarm is triggered for the database table.
[0087] Specifically, after obtaining the fragmentation rate of a database table, if the second file size is greater than the first preset threshold and the fragmentation rate is greater than the second preset threshold, for example, the second file size exceeds 10G and the fragmentation rate exceeds 30%, then the relevant information of the database table is recorded in the exception record table, and the relevant information includes the database name, the database table name, the second file size of the database table, and the fragmentation rate, etc. The exception record table is periodically checked to see if it is empty. If the exception record table is empty, the process ends; if the exception record table is not empty, an alarm is sent to the user via email, and the alarm content includes the exception record time, the database name, the database table name, the second file size of the database table, and the fragmentation rate.
[0088] In addition, in order to more clearly explain the embodiments of the present application, the fragmentation rate detection method of the database table provided in the embodiments of the present application mainly includes: a scheduling process, a heterogeneous machine recovery process, a fragmentation rate evaluation process and an alarm process, wherein the scheduling process mainly sets a heterogeneous machine recovery strategy for the heterogeneous machine recovery process, monitors the full database backup status of the first database, and schedules the backup of multiple database tables contained in the first database to the second database; the heterogeneous machine recovery process is mainly to back up the multiple database tables contained in the first database to the second database; the fragmentation rate evaluation process mainly evaluates the fragmentation rate of each of the multiple database tables in the first database based on the first file size before defragmentation and the second file size after defragmentation; the alarm process mainly periodically checks the fragmentation rate record results of the fragmentation rate evaluation process, and according to the pre-set threshold, notifies the user of the relevant information of the database table that does not meet the preset conditions in the form of an alarm.
[0089] The following introduces the scheduling process, heterogeneous machine recovery process, fragmentation rate evaluation process and alarm process in turn.
[0090] See also Figure 3 , exemplarily shows a schematic diagram of the method flow of the scheduling process, which specifically includes the following steps:
[0091] Step 301, start.
[0092] Step 302: Check the status of the first database on the database platform.
[0093] Step 303, determine whether the first database is in a normal state, if so, execute step 304, otherwise execute step 311.
[0094] Specifically, if the state of the first database is abnormal, the exception processing is performed and the process ends; if the first database is in a normal state, the process proceeds to step 304 to obtain backup files of multiple database tables of the first database.
[0095] Step 304: Obtain local backup data of multiple database tables of the first database.
[0096] Specifically, the local backup data of multiple database tables of the first database include: the name of the first database, the name of the database table, the backup time, the full database backup status, the verification status and the mirror status. If the execution fails, jump to step 311 to handle the exception and end. If the execution succeeds, go to step 304. In addition, in order to reduce the time and resource costs required for the regular cross-machine recovery process, this step can set different levels of cross-machine recovery strategies according to the database table name, security level and business system. For example, the frequency of cross-machine recovery is set according to the security level: the first level: the local backup data is retained for 10 days, and the cross-machine recovery is performed every 3 days; the second level: the local backup data is retained for 7 days, and the cross-machine recovery is performed every 5 days; the third level: the local backup data is retained for 5 days, and the cross-machine recovery is performed every 7 days; the fourth to sixth levels: the local backup data is retained for 3 days, and the cross-machine recovery is performed every 10 days.
[0097] Step 305: Check the verification status of the first database.
[0098] Step 306, determine whether the first database is successfully verified, if so, execute step 312, otherwise execute step 307.
[0099] Specifically, if verification is not performed or verification fails at the heterogeneous machine recovery frequency specified by the security level, step 307 is performed; if verification succeeds, the process ends.
[0100] Step 307, checking the latest full database backup status of the first database.
[0101] Step 308 , determining whether the full database backup status of the first database is successful, if so, executing step 309 , otherwise executing step 311 .
[0102] Specifically, if the full database backup state fails, the process jumps to step 311 to perform exception handling and ends. If the full database backup state succeeds, the process proceeds to step 309.
[0103] Step 309: Acquire the latest local backup data of the first database, and back up the latest local backup data of the first database to the recovery path of the second database.
[0104] Step 310, determine whether step 309 is executed successfully, if so, execute step 312, otherwise execute step 311.
[0105] Specifically, if the execution fails, the process jumps to step 311 to handle the exception and then ends; if the execution succeeds, the entire scheduling process ends.
[0106] Step 311, exception handling.
[0107] Specifically, the exception handling process mainly records the relevant logs of the exception handling, including the exception handling time, the operation steps of the exception handling, etc.
[0108] Step 312, end.
[0109] See also Figure 4 , exemplarily shows a method flow chart of a heterogeneous machine recovery process, which specifically includes the following steps:
[0110] Step 401, start.
[0111] Step 402 , determine whether the recovery path of the second database has a backup image of the first database, if yes, execute step 403 , if no, execute step 414 .
[0112] Specifically, the backup image of the first database includes local backup data of the first database, wherein the local backup data of the first database includes multiple database tables. If the recovery path of the second database contains the backup image of the first database, proceed to step 403; if not, perform exception processing and end.
[0113] Step 403, determine whether the status of the second database on the database platform is normal, if so, execute step 404, if not, execute step 414.
[0114] Specifically, if the second database is in a normal state, the second database is stopped and the process proceeds to step 404; if the second database is in an abnormal state, an exception is handled and the process ends.
[0115] Step 404 , determining whether the backup type of the backup image of the first database is a full database physical backup, if so, executing step 405 , if not, executing step 414 .
[0116] Specifically, if the backup type is a full database physical backup, the process proceeds to step 405 ; if the backup type is a table guide backup or a table result backup, the process performs an exception process and ends.
[0117] Step 405, heterogeneous machine recovery operation.
[0118] Step 406, determine whether the heterogeneous machine recovery operation is successfully executed, if so, execute step 407, if not, execute step 414.
[0119] Specifically, the heterogeneous machine recovery method may be Xtrabackup, Disksync, FavBackup, etc. If the heterogeneous machine recovery operation fails, the exception processing is performed and the operation ends.
[0120] Step 407: Start the second database and detect the startup status of the second database.
[0121] Step 408 , determining whether the second database is started successfully, if so, executing step 409 , if not, executing step 414 .
[0122] Specifically, if the second database is successfully started, the process proceeds to step 409 ; if the second database fails to be started, an exception is handled and the process ends.
[0123] Step 409: Update the identification information and import it into the database table.
[0124] Step 410 , determining whether the database table is imported successfully, if so, executing step 411 , if not, executing step 414 .
[0125] Specifically, if identification information backup_gtid_executed.sql exists in multiple database tables, the multiple database tables are imported into the second database. If the import is successful, the process proceeds to step 411. If the import fails, an exception is handled and the process ends.
[0126] Step 411 , determining whether the status of the plurality of database tables in the second database is normal, if so, executing step 412 , if not, executing step 414 .
[0127] Specifically, if the state of the database table is abnormal, the exception processing is performed and the process ends; if the state of the database table is normal, the database table backup is successful and the process proceeds to step 412 .
[0128] Step 412, updating the heterogeneous machine recovery status flag.
[0129] Step 413, determine whether the update in step 412 is successful, if so, execute step 415, if not, execute step 414.
[0130] Specifically, if the update is successful, the backup images of the first database and the second database are marked as being available, and the process of backing up multiple database tables from the local backup data to the second database is completed.
[0131] Step 414, exception handling.
[0132] Specifically, the exception handling process mainly records the relevant logs of the exception handling, including the exception handling time, the operation steps of the exception handling, etc.
[0133] Step 415, end.
[0134] Third, see Figure 5 , exemplarily shows a schematic flow chart of a fragmentation rate evaluation process, which specifically includes the following steps:
[0135] Step 501, start.
[0136] Step 502, periodically check the task status of the heterogeneous machine recovery process.
[0137] Step 503, determine whether the heterogeneous machine recovery process is detected to be completed, if so, execute step 504, if not, execute step 518.
[0138] Specifically, if it is detected that the heterogeneous machine recovery process is completed, the fragmentation rate evaluation process is started, otherwise, it is ended.
[0139] Step 504: record the specific start time of the fragmentation rate evaluation process.
[0140] Step 505, determine whether step 504 is executed successfully, if so, execute step 506, if not, execute step 518.
[0141] Specifically, if the execution fails, the exception handling process is jumped and ends; if the execution succeeds, the process proceeds to step 506 .
[0142] Step 506, determine whether the second database is connected successfully, if so, execute step 507, if not, execute step 518.
[0143] Specifically, if the second database connection fails, the exception handling process is jumped and ends; if the connection is successful, the process proceeds to step 507 .
[0144] Step 507: Obtain the database table names of other databases in the second database except the system database, and generate database table lists corresponding to the respective databases.
[0145] Step 508, determine whether step 507 is executed successfully, if so, execute step 509, if not, execute step 518.
[0146] Specifically, if step 507 fails to execute, the exception handling process is jumped and ends. If the execution is successful, step 509 is entered, wherein the system database includes: mysql database, information_schema database, performance_schema database, etc.
[0147] Step 509, obtain the data file size of all database tables in the second database, and record it as the first file size. For example, the first file size of the tb1 database table in the database db1 is db1_tb1_size1. The method for obtaining the first file size is to add up the file sizes of all physical files in the data directory of the database db1. For example, the database table chk has a table structure file chk.frm and a data file chk.ibd in the data directory, and the file sizes are 8586 and 98304 bytes respectively. Then, the first file size of the database table chk is 106620, in bytes. According to the above calculation method, the first file size, database name and database table name of all database tables contained in all databases in the second database are obtained.
[0148] Step 510: close the database record and defragment the multiple database tables of the second database, for example, by defragmenting the database tables through SQL statements.
[0149] Step 511, determine whether step 510 is executed successfully, if so, execute step 512, if not, execute step 518.
[0150] Specifically, if the execution fails, the exception handling process is jumped and ends; if the execution succeeds, the process proceeds to step 512 .
[0151] Step 512, obtain the data file size of all database tables in the second database, and record it as the second file size. The second file size is the sum of the file sizes of multiple physical files after the database table is defragmented. For example, the second data file size of the tb1 table in the database db1 is db1_tb1_size2. According to the above calculation method, the second file size, database name and database table name of all database tables contained in all databases in the second database are obtained.
[0152] Step 513, determine whether step 512 is executed successfully, if so, execute step 514, if not, execute step 518.
[0153] Specifically, if the execution fails, the exception handling process is jumped and ends; if the execution succeeds, the process proceeds to step 514 .
[0154] Step 514: Evaluate the fragmentation rate of the database table.
[0155] Specifically, the difference between the first file size and the second file size is determined, and the ratio of the difference to the first file size is used as the fragmentation rate of a database table. If the first file size of the tb1 database table in the database db1 is db1_tb1_size1, and the second file size is db1_tb1_size2, then the calculation method of the fragmentation rate of the tb1 database table in the database db1 is: fragmentation_rat = (db1_tb1_size1-db1_tb1_size2) / db1_tb1_size2*100. For example, in the database db1, the first file size of the tb1 database table is 7893013, and the second file size is 6414410, then the fragmentation rate of the tb1 database table is (7893013-6414410) / 7893013*100=18.7%.
[0156] Step 515, determine whether step 514 is executed successfully, if so, execute step 516, if not, execute step 518.
[0157] Specifically, if the execution fails, the exception handling process is jumped and ends; if the execution succeeds, the process proceeds to step 516 .
[0158] Step 516, information storage.
[0159] Specifically, relevant information of a plurality of database tables in the first database is stored, and the relevant information includes: the name of the first database, the name of the database table, and the fragmentation rate of the database table.
[0160] Step 517, determine whether step 516 is executed successfully, if so, execute step 519, if not, execute step 518.
[0161] Specifically, if the execution fails, the process jumps to the exception handling process and ends; if the execution succeeds, the process proceeds to step 519 to end the entire fragment evaluation process.
[0162] Step 518, exception handling.
[0163] Specifically, the exception handling process mainly records the relevant logs of the exception handling, including the exception handling time, the operation steps of the exception handling, etc.
[0164] Step 519, end.
[0165] Fourth, see Figure 6 , exemplarily shows a method flow chart of the alarm process, which specifically includes the following steps:
[0166] Step 601, start.
[0167] Step 602 , periodically checking whether the relevant information of the plurality of database tables in the second database meets the preset conditions, if so, executing step 609 , if not, executing step 603 .
[0168] Specifically, if the relevant information of the database table does not meet the preset conditions, step 603 is entered to record the relevant information of the database table in the exception record table. For example, if the second file size of a database table exceeds 10G and the fragmentation rate exceeds 30%, the relevant information of the database table is recorded in the exception record table; if the preset conditions are met, the relevant information of the database table that meets the preset conditions is not recorded and the database table is skipped, and it is checked whether the next database table meets the preset conditions.
[0169] Step 603, record the abnormal record table.
[0170] Specifically, relevant information of the database table that does not meet the preset conditions is recorded in the abnormal record table, and the relevant information includes the database name, the database table name, the second file size of the database table, the fragmentation rate, etc.
[0171] Step 604, determine whether the abnormal record table is recorded successfully, if so, execute step 605, if not, execute step 608.
[0172] Step 605, periodically check whether the abnormal record table is empty, if so, execute step 609, if not, execute step 606.
[0173] Specifically, if the abnormal record table is empty, the alarm process ends; if the abnormal record table is not empty, the process proceeds to step 606 .
[0174] Step 606: Email alert.
[0175] Specifically, an alarm is sent to the user via email, and the alarm content includes the abnormal record time, database name, database table name, second file size of the database table, and fragmentation rate.
[0176] Step 607, determine whether step 606 is executed successfully, if so, execute step 609, if not, execute step 608.
[0177] Step 608, exception handling.
[0178] Specifically, the exception handling process mainly records the relevant logs of the exception handling, including the exception handling time, the operation steps of the exception handling, etc.
[0179] Step 609, end.
[0180] Based on the same technical concept, see Figure 7, an embodiment of the present application provides a database table fragmentation rate detection device, comprising:
[0181] An acquiring unit 701 is used to acquire a first file size of each of a plurality of database tables included in a first database;
[0182] A backup unit 703, configured to back up a plurality of database tables included in the first database to a second database;
[0183] The fragmentation assessment unit 704 is configured to respectively perform the following operations on the multiple database tables included in the second database:
[0184] Defragment a database table and obtain a second file size of the defragmented database table;
[0185] A fragmentation rate of a database table is obtained based on a first file size and a second file size of a database table.
[0186] In an optional implementation, a state monitoring unit 702 is further included;
[0187] The status monitoring unit 702 is specifically used for:
[0188] Before backing up the multiple database tables included in the first database to the second database, it is monitored that the first database and the second database are in a normal state, and the backup images of the first database and the second database are respectively in an available state.
[0189] In an optional implementation manner, the backup unit 703 is further configured to:
[0190] Performing a full database backup locally on the first database to obtain local backup data;
[0191] Back up multiple database tables from the local backup data to the second database.
[0192] In an optional implementation manner, the backup unit 703 is further configured to:
[0193] Obtaining a backup type between the first database and the second database;
[0194] Obtain multiple database tables from local backup data according to the backup type;
[0195] Back up multiple database tables to a secondary database.
[0196] In an optional implementation manner, the fragment assessment unit 704 is specifically configured to:
[0197] determining a difference between a first file size and a second file size;
[0198] The ratio of the difference to the size of the first file is taken as the fragmentation rate of a database table.
[0199] In an optional implementation, an alarm unit 705 is also included;
[0200] The alarm unit 705 is specifically used for:
[0201] After obtaining the fragmentation rate of a database table, if the second file size is greater than a first preset threshold and the fragmentation rate is greater than a second preset threshold, an alarm is triggered for the database table.
[0202] In an embodiment of the present application, firstly, a first file size of each of the multiple database tables included in the first database is obtained, and the multiple database tables included in the first database are backed up to the second database, and the multiple database tables included in the first database are backed up on a different machine and restored to the second database, and then the multiple database tables are defragmented on the different machine to obtain the second file size corresponding to each database table, and finally, the fragmentation rate of each of the multiple database tables is obtained by the first file size of each of the multiple database tables before defragmentation and the second file size after defragmentation. The entire evaluation process does not need to pay attention to the sampling frequency and the number of samples, and is not performed on the source database, which effectively improves the accuracy of the fragmentation rate evaluation of the database tables.
[0203] Based on the same technical concept, the embodiment of the present application provides a computer device, which can be Figure 1 The detection system, the first database and / or the second database shown, as Figure 8 As shown, it includes at least one processor 801 and a memory 802 connected to the at least one processor. The specific connection medium between the processor 801 and the memory 802 is not limited in the embodiment of the present application. Figure 8 For example, the processor 801 and the memory 802 are connected via a bus. The bus can be divided into an address bus, a data bus, a control bus, etc.
[0204] In the embodiment of the present application, the memory 802 stores instructions that can be executed by at least one processor 801. The at least one processor 801 can perform the steps of the above-mentioned database table fragmentation rate detection method by executing the instructions stored in the memory 802.
[0205] Among them, the processor 801 is the control center of the computer device, and can use various interfaces and lines to connect various parts of the computer device, and realize the fragmentation rate detection of the database table by running or executing the instructions stored in the memory 802 and calling the data stored in the memory 802. Optionally, the processor 801 may include one or more processing units, and the processor 801 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 801. In some embodiments, the processor 801 and the memory 802 may be implemented on the same chip, and in some embodiments, they may also be implemented separately on independent chips.
[0206] Processor 801 can be a general-purpose processor, such as a central processing unit (CPU), a digital signal processor, an application-specific integrated circuit (ASIC), a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, and can implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present application. A general-purpose processor can be a microprocessor or any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application can be directly embodied as a hardware processor for execution, or can be executed by a combination of hardware and software modules in the processor.
[0207] The memory 802 is a non-volatile computer-readable storage medium that can be used to store non-volatile software programs, non-volatile computer executable programs and modules. The memory 802 may include at least one type of storage medium, such as a flash memory, a hard disk, a multimedia card, a card-type memory, a random access memory (Random Access Memory, RAM), a static random access memory (Static Random Access Memory, SRAM), a programmable read-only memory (Programmable Read Only Memory, PROM), a read-only memory (Read Only Memory, ROM), an electrically erasable programmable read-only memory (Electrically Erasable Programmable Read-Only Memory, EEPROM), a magnetic memory, a disk, an optical disk, etc. The memory 802 is any other medium that can be used to carry or store a desired program code in the form of an instruction or data structure and can be accessed by a computer device, but is not limited thereto. The memory 802 in the embodiment of the present application can also be a circuit or any other device that can realize a storage function, for storing program instructions and / or data.
[0208] Based on the same inventive concept, an embodiment of the present application provides a computer-readable storage medium storing a computer program executable by a computer device. When the program runs on the computer device, the computer device executes the steps of the above-mentioned database table fragmentation rate detection method.
[0209] Based on the same inventive concept, an embodiment of the present application provides a computer program product, which includes a computer program stored on a computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer device, the computer device executes the steps of the above-mentioned database table fragmentation rate detection method.
[0210] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0211] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer device or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0212] These computer program instructions may also be stored in a computer readable memory capable of directing a computer device or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0213] These computer program instructions can also be loaded onto a computer device or other programmable data processing device so that a series of operation steps are executed on the computer device or other programmable device to produce a process implemented by the computer device, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0214] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0215] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A method for detecting the fragmentation rate of a database table, characterized in that: include: Obtaining a first file size of each of a plurality of database tables included in the first database; Backing up the multiple database tables included in the first database to the second database; For the multiple database tables included in the second database, perform the following operations respectively: Defragmenting a database table and obtaining a second file size of the defragmented database table; A fragmentation rate of the database table is obtained based on the first file size and the second file size of the database table.
2. The method according to claim 1, characterized in that Before backing up the multiple database tables included in the first database to the second database, the method further includes: It is monitored that the first database and the second database are in a normal state, and the backup images of the first database and the second database are respectively in an available state.
3. The method according to claim 1, characterized in that Backing up multiple database tables included in the first database to a second database includes: Performing a full database backup locally on the first database to obtain local backup data; The multiple database tables are backed up from the local backup data to a second database.
4. The method according to claim 3, characterized in that The step of backing up the multiple database tables from the local backup data to a second database further includes: Obtaining a backup type between the first database and the second database; Acquire the multiple database tables from the local backup data according to the backup type; The plurality of database tables are backed up to a second database.
5. The method according to claim 1, characterized in that The database table includes multiple physical files, the first file size is the sum of the file sizes of the multiple physical files before defragmentation; the second file size is the sum of the file sizes of the multiple physical files after defragmentation.
6. The method according to claim 1, characterized in that The obtaining the fragmentation rate of the database table based on the first file size and the second file size of the database table includes: Determining a difference between the first file size and the second file size; The ratio of the difference to the first file size is used as the fragmentation rate of the database table.
7. The method according to claim 1, characterized in that After obtaining the fragmentation rate of the database table, the method further includes: If the second file size is greater than a first preset threshold, and the fragmentation rate is greater than a second preset threshold, an alarm is triggered for the one database table.
8. A database table fragmentation rate detection device, characterized in that: include: An acquiring unit, configured to acquire a first file size of each of a plurality of database tables included in the first database; A backup unit, used for backing up a plurality of database tables included in the first database to a second database; The fragmentation assessment unit is configured to respectively perform the following operations on the multiple database tables included in the second database: Defragmenting a database table and obtaining a second file size of the defragmented database table; A fragmentation rate of the database table is obtained based on the first file size and the second file size of the database table.
9. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of any one of the methods of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that: It stores a computer program executable by a computer device. When the program is run on the computer device, the computer device executes the steps of any method described in claims 1 to 7.
11. A computer program product, characterized in that The computer program product comprises a computer program stored on a computer-readable storage medium, wherein the computer program comprises program instructions, and when the program instructions are executed by a computer device, the computer device executes the steps of the method according to any one of claims 1 to 7.
