In this blog post, we’ll look at MySQL 8.0 general tablespaces.
MySQL 8.0 (the DMR version is available now) has two great features (among others):
With those two options, we can use MySQL for creating multi-tenant environments with a “schema per customer” approach.
Using schema per customer with older MySQL versions presents issues … namely the number of files. (I’ve described schema per customer approach in MySQL in an older blog post.) Let’s say you are hosting a Drupal-based site for your customers, and you create a new database (AKA “schema”) per each customer. You do not want to create one schema for all because each customer wants to extend Drupal and use plugins that will create their own unique set of tables. With tablespace per table and an FRM file, 10K customers will end up with:
. . . or a grand total of 1.3 million files!
With MySQL 8.0, we can create a tablespace file per each schema and place those tablespace files in a specific set of directories. For example, if we have demo, test and production accounts, we can create a set of directories (outside of the MySQL datadir) and place tablespaces inside them. With no FRM files, we will only have 10 thousands of files, evenly split across multiple locations.
Example:
|
1 |
mysql> create database if not exists drupal_customer_name;<br>Query OK, 1 row affected (0.00 sec)<br>mysql> CREATE TABLESPACE drupal_customer_name <br>ADD DATAFILE '/var/lib/mysql_datafiles/drupal_demo/drupal_customer_name.ibd'<br>Engine=InnoDB;<br>Query OK, 0 rows affected (0.16 sec)<br>mysql> use drupal_customer_name;<br>Database changed<br>mysql> create table t(i int) ENGINE=InnoDB <br>TABLESPACE drupal_customer_name;<br>Query OK, 0 rows affected (0.13 sec)<br>mysql> create table t1(i int) ENGINE=InnoDB <br>TABLESPACE drupal_customer_name;<br>Query OK, 0 rows affected (0.00 sec) |
Now let’s look at the directory:
|
1 |
ls -lah /var/lib/mysql_datafiles/drupal_demo/<br>-rw-r----- 1 mysql mysql 144K Sep 26 00:58 drupal_customer_name.ibd<br> |
The downside of this approach is that the “create tables” command should have the tablespace name in it. I’ve created a sample “deploy” script to create a new schema for a customer:
|
1 |
customer_name="my_drupal_customer"<br>mysql -f -vvv -e "create database if not exists $customer_name; <br>CREATE TABLESPACE $customer_name ADD DATAFILE '/var/lib/mysql_datafiles/drupal_demo/${customer_name}.ibd' <br>engine=InnoDB;"<br>cat drupal.sql | sed -e "s/ENGINE=InnoDB/ENGINE=InnoDB TABLESPACE $customer_name/g"|mysql $customer_name |
Size and Timing
In the next post, I plan to benchmark the performance of millions of tables with MySQL 8.0 and tablespace file per database. Here, I’ve compared the create table performance between MySQL 5.7 (with FRM and file per table), MySQL 8.0 with a file per table (FRMs are gone) and MySQL 8.0 with a file per database. Time to create 1000 databases for Drupal (no data), 65 tables in each database:
What about the size on disk? It did not change much. Actually, the size on disk for the blank tables (no data) is more in MySQL 8.0:
With 10K schemas, it is 100G just to store tablespaces (schema overhead). At the same time, it is not 100% overhead: InnoDB creates 112K+ the tablespace file right away (it depends upon the table structure). When the data is loaded it will use this reserved space.
Tablespace supports compression as well: CREATE TABLESPACE … ADD DATAFILE ‘…’ FILE_BLOCK_SIZE = 8192 Engine=InnoDB; CREATE TABLE … ENGINE=InnoDB TABLESPACE … ROW_FORMAT=COMPRESSED;
MySQL 8.0 uses a new transactional data dictionary.
MySQL Server 8.0 now incorporates a global data dictionary containing information about database objects in transactional tables. In previous MySQL releases, dictionary data was stored in metadata files and nontransactional system tables.
That also means that all old metadata files are gone: no .frm, .par, .trn, .trg files. In addition tables inside a MySQL database are not using MyISAM tables anymore. The new installation has no single MyISAM table, although the MyISAM engine is supported:
|
1 |
Welcome to the MySQL monitor. Commands end with ; or g.<br>Your MySQL connection id is 2299<br>Server version: 8.0.0-dmr MySQL Community Server (GPL)<br><br>Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.<br><br>Oracle is a registered trademark of Oracle Corporation and/or its<br>affiliates. Other names may be trademarks of their respective<br>owners.<br><br>Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.<br><br>mysql> select count(*) from information_schema.tables where engine = 'MyISAM';<br>+----------+<br>| count(*) |<br>+----------+<br>| 0 |<br>+----------+<br>1 row in set (0.03 sec)<br><br>mysql> select count(*), engine from information_schema.tables where table_schema = 'mysql' group by engine;<br>+----------+--------+<br>| count(*) | ENGINE |<br>+----------+--------+<br>| 2 | CSV |<br>| 30 | InnoDB |<br>+----------+--------+<br>2 rows in set (0.00 sec)<br> |
FRM free installation looks great (performance testing of it is my next step). I would also love to see some additional features in MySQL 8.0:
Please note: MySQL 8.0 is not production ready and only available for preview.
Learn more about Percona Server for MySQL