update
This commit is contained in:
		
							
								
								
									
										44
									
								
								netease_music/sql/export_sql_generator.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								netease_music/sql/export_sql_generator.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
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)};`);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								netease_music/sql/replace_insert_table_name.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								netease_music/sql/replace_insert_table_name.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
const fs = require('fs');
 | 
			
		||||
const absPath = `D:/sql_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 begin = 115;
 | 
			
		||||
let end = 116;
 | 
			
		||||
for (let i = begin; i < end; i++) {
 | 
			
		||||
    console.log(`读取文件 comment_export_${fill(i, '0', 4)}.sql`);
 | 
			
		||||
    let sqlFile = fs.readFileSync(`${absPath}/comment_export_${fill(i, '0', 4)}.sql`, "utf-8");
 | 
			
		||||
    console.log(`处理文件`);
 | 
			
		||||
    // console.log(sqlFile);
 | 
			
		||||
    let sqls = sqlFile.split("\r\n");
 | 
			
		||||
    sqls = sqls.map(sql => sql.replace(`INSERT INTO \`comment_export_${fill(i, '0', 4)}\` VALUES`, "INSERT INTO `comment` VALUES"))
 | 
			
		||||
    // console.log(sqls);
 | 
			
		||||
    console.log(`拼接文件`);
 | 
			
		||||
    let newSqlFile = sqls.join('\n');
 | 
			
		||||
    console.log(`写入文件`);
 | 
			
		||||
    fs.writeFileSync(`${absPath}/output/comment_${fill(i, '0', 4)}.sql`, newSqlFile, "utf-8");
 | 
			
		||||
    console.log(`完成`);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user