const export_gap = 50000000; const partition_gap = 50000000; const table = "comment_1024"; const index = "comment_id"; const exportTablePrefix = "comment_export_"; // 数字转成字符串,同时在前面填充 function fill(num, fillers, length) { var result = `${num}`; if (result.length < length) result = new Array(length - result.length + 1).join(fillers) + result; return result; } let sqlArr1 = []; let sqlArr2 = []; let sqlArr3 = []; for (let i = 200; i < 300; i++) { // for (let i = 100; i < 200; i++) { let where = `${index} >= ${fill(i * export_gap, ' ', 12)} and ${index} < ${fill((i + 1) * export_gap, ' ', 12)}`; let sql_create_table = `create table ${exportTablePrefix}${fill(i, '0', 4)} select * from ${table} where ${where};`; sqlArr1.push(sql_create_table); let sql_delete_rows = `DELETE FROM ${table} WHERE ${where};`; sqlArr2.push(sql_delete_rows); let sql_partition = ` PARTITION p${fill(i, '0', 4)} VALUES LESS THAN (${fill((i + 1) * partition_gap, ' ', 12)})`; sqlArr3.push(sql_partition); } sqlArr3.push(` PARTITION p_max VALUES LESS THAN MAXVALUE`); // 建表 // console.log(sqlArr1.join('\n')); // 删除原表数据 // console.log(sqlArr2.join('\n')); // 新创建表的分区 // console.log(`partition (\n${sqlArr3.join(',\n')}\n)`); for (let i = 107; i < 200; i++) { console.log(`DROP TABLE IF EXISTS comment_export_${fill(i, '0', 4)};`); }