在ndb上可以建立两种类型的表:
1、内存表:所有数据(包括index)都在内存中。同时会在磁盘上保存数据,因此不用担心数据会丢失,datanode会在启动的时候把数据加载到内存。
2、磁盘表:仅主键、索引字段保存在内存中,其他字段保存在磁盘文件里。
-- MySQL 5.7 CLUSTER NDB 7.5
-- 创建undo logfile group
create logfile group lg_1
ADD UNDOFILE 'undo_1.log'
INITIAL_SIZE 1024M
undo_buffer_size 8M
ENGINE NDBCLUSTER;
-- 增加 logfile
ALTER logfile group lg_1
ADD UNDOFILE 'undo_2.log'
initial_size 1024M
ENGINE NDBCLUSTER;
-- 创建tablespace
CREATE TABLESPACE ts1
ADD DATAFILE 'data_1.dat'
USE LOGFILE GROUP lg_1
INITIAL_SIZE 1024M
ENGINE NDBCLUSTER;
-- 扩张tablespace
ALTER TABLESPACE ts_1
ADD DATAFILE 'data_2.dat'
INITIAL_SIZE 1024M
ENGINE NDBCLUSTER;
-- 创建disk table
CREATE TABLE dt_1 (
member_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
last_name VARCHAR(50) NOT NULL,
first_name VARCHAR(50) NOT NULL,
dob DATE NOT NULL,
joined DATE NOT NULL,
INDEX(last_name, first_name)
)
TABLESPACE ts_1 STORAGE DISK
ENGINE NDBCLUSTER;
-- 删除table
mysql> DROP TABLE dt_1;
mysql> ALTER TABLESPACE ts_1
-> DROP DATAFILE 'data_2.dat'
-> ENGINE NDBCLUSTER;
mysql> ALTER TABLESPACE ts_1
-> DROP DATAFILE 'data_1.dat'
-> ENGINE NDBCLUSTER;
mysql> DROP TABLESPACE ts_1
-> ENGINE NDBCLUSTER;
mysql> DROP LOGFILE GROUP lg_1
-> ENGINE NDBCLUSTER;
-- 修改内存表为磁盘表
alter table tablename tablespace
ts_1 STORAGE DISK ENGINE=NDB;
-- 查看表空间大小
SELECT TABLESPACE_NAME, FILE_NAME, EXTENT_SIZE*TOTAL_EXTENTS/1024/1024 AS TOTAL_MB, EXTENT_SIZE*FREE_EXTENTS/1024/1024 AS FREE_MB, EXTRA FROM information_schema.FILES WHERE FILE_TYPE="DATAFILE";