Managing storage and performance efficiently in your MySQL database is crucial, and general tablespaces offer flexibility in achieving this. This blog discusses general tablespaces and explores their functionalities, benefits, and practical usage, along with illustrative examples.

What are MySQL general tablespaces?

In contrast to the single system tablespace that holds system tables by default, general tablespaces are user-defined storage containers for multiple InnoDB tables. They provide flexibility in data organization and performance optimization compared to the default setup.

Key features

  • Multi-table storage: Unlike file-per-table tablespaces, which store each table in a separate file, general tablespaces can house numerous tables, enhancing storage efficiency.
  • Flexible location: Data files can reside within the MySQL data directory or an independent location, enabling finer control over storage management and performance tuning.
  • Support for all table formats: General tablespaces accommodate all InnoDB table formats, including Redundant, Compact, Dynamic, and Compressed Row formats, offering flexibility for specific needs.
  • Memory optimization: Shared tablespace metadata reduces memory consumption compared to multiple file-per-table tablespaces.

Benefits of using general tablespaces

  • Improved performance: Strategically placing data files on faster disks or spreading tables across multiple disks can significantly enhance 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 encryption for General Tablespaces, enhancing the security of your data.
  • Convenient table management: General tablespaces allow you to group multiple tables together, making it easier to manage and organize your database objects.

Creating and managing general tablespaces

You can create general tablespaces using the CREATE TABLESPACE statement, specifying data file locations and engine options. 

Creating a general tablespace involves a few simple steps. The CREATE TABLESPACE statement below creates a new tablespace named my_general_tablespace with a specified data file ‘general_tablespace.ibd’. Additionally, it enables encryption for the tablespace with the option ENCRYPTION=’Y’ and sets the file block size with the FILE_BLOCK_SIZE = 16384 option.

 Let’s create a general tablespace named my_general_tablespace:

Now, let us see how to create a general tablespace outside the data directory.

Error 3121 (HY000): The DATAFILE location must be in a known directory. Indicates that MySQL cannot create the tablespace in the specified directory because it’s not configured as a valid location for data files.

To address this error, follow these steps: Check the configured directories using SHOW VARIABLES LIKE ‘innodb_directories’; If /var/lib/mysql_user_defined is not listed, proceed to add the directory.

Assigning tables to general tablespaces

Once a MySQL general tablespace is created, you can assign tables to it during the table creation process or by altering existing tables. Here’s an example of creating a table within the my_general_tablespace:

The user_defined_general_tablespace we created is unencrypted, allowing us to create unencrypted tables within it.

Migrating tables to general tablespaces

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

To transfer a table from the general tablespace to a file-per-table tablespace, specify “innodb_file_per_table” as the target tablespace name.

Monitoring

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

The following query helps to find information about InnoDB tables that belong to the specified tablespace.

To retrieve TABLESPACE information for a particular InnoDB table, utilize the following query.

Examples of practical usage:

  • Separating frequently accessed and rarely used tables: Place frequently accessed tables in general tablespaces residing on SSDs for superior performance while keeping rarely used tables in HDD-based general tablespaces to optimize storage costs.
  • Balancing I/O load: Distribute tables across multiple general tablespaces located on different disks to avoid I/O bottlenecks and improve query execution speed.
  • Dedicated storage for critical data: Create a separate general tablespace with RAID or DRBD configuration for critical tables, ensuring maximum redundancy and protection against hardware failures.

Conclusion

MySQL general tablespaces offer a powerful and flexible storage solution for optimizing data organization and performance, and understanding their functionalities and effectively deploying them can significantly improve your database management efforts. In order to maximize their benefits, remember to carefully consider your specific needs and workload characteristics before implementing general tablespaces.

Percona Distribution for MySQL is the most complete, stable, scalable, and secure open source MySQL solution available, delivering enterprise-grade database environments for your most critical business applications… and it’s free to use!

 

Try Percona Distribution for MySQL today!

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mike

I have moved a deployment to general tablespaces per schema, where there is a db-per-client pattern, resulting in lots of disk space savings and much faster xtrabackup.

Watch out for upstream bug 111709