Do you know how to use these options of MySQL general table space?

Efficiently managing storage and performance in a MySQL database is critical, and general tablespaces provide the flexibility to achieve this goal. This article discusses general table spaces and explores their features, benefits, and practical uses, along with illustrative examples.

What is a general tablespace?

Unlike a single system tablespace that holds system tables by default, a general tablespace is a user-defined storage container for multiple InnoDB tables. They provide flexibility in data organization and performance optimization compared to the default settings.

Main features

  • Multi-table storage: Unlike independent table spaces that store each table in a separate file, a general table space can accommodate a large number of tables, thereby improving storage efficiency.
  • Flexible location: Data files can reside in MySQL's data directory or in a separate location, allowing greater control over storage management and performance tuning.
  • Support for all table formats: Universal table spaces can accommodate all InnoDB table formats, including redundant, compact, dynamic, and compressed row formats, providing flexibility for specific needs.
  • Memory Optimization: Shared tablespace metadata reduces memory consumption compared to tablespaces with multiple files per table.

Benefits of using a general tablespace

  • Improved performance: Strategically placing data files on faster disks or spreading tables across multiple disks can significantly improve performance.
  • RAID and DRBD integration: Data files can be placed on RAID or DRBD volumes for enhanced data redundancy and disaster recovery.
  • Encryption support: MySQL supports general table space encryption to enhance data security.
  • Convenient table management: Universal tablespaces allow you to group multiple tables together, making it easier to manage and organize database objects.

Create and manage common tablespaces

You can use the CREATE TABLESPACE statement to create a general tablespace and specify the data file location and engine options.

Creating a general tablespace involves a few simple steps. The following CREATE TABLESPACEgeneral_tablespace.ibd statement creates a my_general_tablespacenew tablespace named using the specified data file . Additionally, it ENCRYPTION='Y'enables tablespace encryption using the option and FILE_BLOCK_SIZE = 16384sets the file block size using the option.

Let's create a my_general_tablespacegeneral tablespace called :

mysql> CREATE TABLESPACE my_general_tablespace
   -> ADD DATAFILE 'general_tablespace.ibd'
   -> ENCRYPTION='Y'
   -> FILE_BLOCK_SIZE = 16384;
ERROR 3185 (HY000): Can't find master key from keyring, please check in the server log if a keyring is loaded and initialized successfully.
mysql>


mysql> pager grep -i keyring_file;
PAGER set to 'grep -i keyring_file'

mysql> SHOW PLUGINS;
50 rows in set (0.00 sec)

mysql> INSTALL PLUGIN keyring_file SONAME 'keyring_file.so';
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW PLUGINS;
| keyring_file                     | ACTIVE   | KEYRING            | keyring_file.so | GPL     |
50 rows in set (0.00 sec)

mysql> CREATE TABLESPACE my_general_tablespace
   -> ADD DATAFILE 'general_tablespace.ibd'
   -> ENCRYPTION='Y'
   -> FILE_BLOCK_SIZE = 16384;
Query OK, 0 rows affected (0.01 sec)

mysql>

Now, let's see how to create a general tablespace outside of the data directory.

root@mysql8:/var/lib# mkdir mysql_user_defined
root@mysql8:/var/lib# chown -R mysql.mysql mysql_user_defined
root@mysql8:/var/lib#

mysql> CREATE TABLESPACE user_defined_general_tablespace
    -> ADD DATAFILE '/var/lib/var/lib/mysql_user_defined/user_defined_general_tablespace.ibd'
    -> Engine=InnoDB;
ERROR 3121 (HY000): The DATAFILE location must be in a known directory.

Error 3121 (HY000): The data file location must be in a known directory. Tip: MySQL cannot create the tablespace in the specified directory because the directory is not configured as a valid location for data files.

To resolve this error, follow these steps: Check the configured directories using SHOW VARIABLES LIKE 'innodb_directories'/var/lib/mysql_user_define ; if not listed, proceed to add the directory.

mysql> SHOW VARIABLES LIKE 'innodb_directories';
+--------------------+-------+
| Variable_name      | Value |
+--------------------+-------+
| innodb_directories |       |
+--------------------+-------+
1 row in set (0.00 sec)

root@mysql8:/etc/mysql/mysql.conf.d# grep -i innodb_directories mysqld.cnf
innodb_directories=/var/lib/mysql_user_defined
root@mysql8:/etc/mysql/mysql.conf.d# service mysql restart
root@mysql8:/etc/mysql/mysql.conf.d

mysql> CREATE TABLESPACE user_defined_general_tablespace
    -> ADD DATAFILE '/var/lib/mysql_user_defined/user_defined_general_tablespace.ibd'
    -> Engine=InnoDB;
Query OK, 0 rows affected (0.02 sec)

Allocate table to general tablespace

After you create a MySQL general tablespace, you can allocate tables to it during table creation or by altering existing tables. The following is my_general_tablespacean example of creating a table in :

mysql> CREATE TABLE my_table (
    ->     id INT PRIMARY KEY,
    ->     name VARCHAR(50)
    -> ) TABLESPACE = my_general_tablespace;
ERROR 3825 (HY000): Request to create 'unencrypted' table while using an 'encrypted' tablespace.
mysql>

mysql> CREATE TABLE my_table (
    ->     id INT PRIMARY KEY,
    ->     name VARCHAR(50)
    -> ) TABLESPACE = my_general_tablespace
    ->   ENCRYPTION='Y';
Query OK, 0 rows affected (0.02 sec)

We created user_define_general_tablespaceunencrypted, allowing us to create unencrypted tables within it.

mysql> CREATE TABLE my_unencrypted_table(
    -> id INT PRIMARY KEY,
    -> name VARCHAR(50)
    -> ) TABLESPACE = user_defined_general_tablespace;
Query OK, 0 rows affected (0.01 sec)

Migrate a table to a general tablespace

If you have existing tables and want to move them to a general tablespace, you can use the ALTER TABLE statement. For example:

mysql> show create table authorsG
*************************** 1. row ***************************
       Table: authors
Create Table: CREATE TABLE `authors` (
  `id` int DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `age` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

mysql> ALTER TABLE authors
    -> TABLESPACE = my_general_tablespace;
ERROR 3825 (HY000): Request to create 'unencrypted' table while using an 'encrypted' tablespace.

mysql> ALTER TABLE authors ENCRYPTION='Y';
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE authors
    -> TABLESPACE = my_general_tablespace;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql>

To move a table from a general tablespace to a standalone tablespace, specify “innodb_file_per_table”as the target tablespace name.

mysql> ALTER TABLE authors
    -> TABLESPACE = innodb_file_per_table ENCRYPTION = 'Y';
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

monitor

This query retrieves information about the specified MySQL tablespace, including tablespace name, file name, storage engine, status, and available free data space.

mysql> SELECT TABLESPACE_NAME, FILE_NAME, ENGINE, STATUS, DATA_FREE FROM INFORMATION_SCHEMA.FILES WHERE TABLESPACE_NAME IN ('my_general_tablespace',
'user_defined_general_tablespace')G
*************************** 1. row ***************************
TABLESPACE_NAME: my_general_tablespace
      FILE_NAME: ./general_tablespace.ibd
         ENGINE: InnoDB
         STATUS: NORMAL
      DATA_FREE: 0
*************************** 2. row ***************************
TABLESPACE_NAME: user_defined_general_tablespace
      FILE_NAME: /var/lib/mysql_user_defined/user_defined_general_tablespace.ibd
         ENGINE: InnoDB
         STATUS: NORMAL
      DATA_FREE: 0
2 rows in set (0.00 sec)

The following query helps find information about InnoDB tables belonging to the specified table space.

mysql> SELECT NAME, SPACE_TYPE, TABLESPACE_NAME from INFORMATION_SCHEMA.INNODB_TABLES JOIN INFORMATION_SCHEMA.FILES ON FILE_ID=SPACE WHERE TABLESPACE_NAME='my_general_tablespace'G
*************************** 1. row ***************************
           NAME: mytestdb/my_table
     SPACE_TYPE: General
TABLESPACE_NAME: my_general_tablespace
*************************** 2. row ***************************
           NAME: mytestdb/books
     SPACE_TYPE: General
TABLESPACE_NAME: my_general_tablespace
2 rows in set (0.01 sec)

To retrieve TABLESPACE information for a specific InnoDB table, use the following query.

mysql> SELECT NAME, SPACE_TYPE, TABLESPACE_NAME from INFORMATION_SCHEMA.INNODB_TABLES JOIN INFORMATION_SCHEMA.FILES ON FILE_ID=SPACE WHERE NAME='mytestdb/my_table'G
*************************** 1. row ***************************
           NAME: mytestdb/my_table
     SPACE_TYPE: General
TABLESPACE_NAME: my_general_tablespace
1 row in set (0.00 sec)

Practical usage example:

  • Separate frequently accessed and rarely used tables: Place frequently accessed tables in a general table space on SSD for superior performance, while placing rarely used tables in a general table space based on HDD, to optimize storage costs.
  • Balance I/O load: Distribute tables across multiple common table spaces located on different disks to avoid I/O bottlenecks and increase query execution speed.
  • Dedicated storage for critical data: Create separate general-purpose tablespaces with RAID or DRBD configuration for critical tables to ensure maximum redundancy and protect against hardware failures.

in conclusion

MySQL universal tablespaces provide a powerful and flexible storage solution for optimizing data organization and performance. Understanding their capabilities and deploying them effectively can significantly improve your database management efforts. To maximize its benefits, remember to carefully consider your specific needs and workload characteristics before implementing a universal tablespace.

For more technical articles, please visit: https://opensource.actionsky.com/

About SQLE

SQLE is a comprehensive SQL quality management platform that covers SQL auditing and management from development to production environments. It supports mainstream open source, commercial, and domestic databases, provides process automation capabilities for development and operation and maintenance, improves online efficiency, and improves data quality.

SQLE get

type address
Repository https://github.com/actiontech/sqle
document https://actiontech.github.io/sqle-docs/
release news https://github.com/actiontech/sqle/releases
Data audit plug-in development documentation https://actiontech.github.io/sqle-docs/docs/dev-manual/plugins/howtouse
MySQL 5.7, Moqu, Li Tiaotiao... Taking stock of the (open source) projects and websites that will be "suspended" in 2023. Kingsoft WPS crashed . Linux's Rust experiment was successful. Can Firefox seize the opportunity... 10 predictions about open source The middle school purchased an "intelligent interactive catharsis device" - which is actually a shell for the Nintendo Wii. "Ruiping", the father of Redis, LLM programming: omniscient and omnipotent&& Stupid The "post-open source" era has arrived: the license has expired and cannot serve the general public. Vim 9.1 is released , dedicated to Bram Moolenaar 2024 "New Year's Battle" in the front-end circle: React digs holes but does not fill them, must it rely on documentation to fill them? China Unicom Broadband suddenly limited the upload speed, and a large number of users complained. Niklaus Wirth, the father of Pascal, passed away.
{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/actiontechoss/blog/10678832