mysql中关于批量插入数据(1万、10万、100万、1000万、1亿级别的数据)二

版权声明:转载请标明出处,否则会依法追究其责任 https://blog.csdn.net/qq_41204714/article/details/85804176

硬件:windows7+8G内存+i3-4170处理器+4核CPU

关于前天写的批量插入数据,还有一种方式,就是通过预先写入文本文件,然后通过mysql的load in file命令导入到数据库,今天把这种方式也说一下,首先是main方法调用:

解释一下为什么要两个参数:

第一个参数是写入几次

第二个参数是一次写入多少条,直观来说就是StringBuffer中存储多少条记录(目的为了测试StringBuffer达到多少会内存溢出)

第三个是文件的存储位置(文件以W为单位)

注:其实按照正常思维,第一个和第二个参数应该换一下位置,应该是先一次多少条,然后在说明几次的,但当时写的时候直接给写上去了,所以就懒得改了,见谅大家~

public static void main(String[] args) throws Exception {
    	createDate(100, 100000, "D:/10000.txt");
		createDateByBufferedWriter(1000, 100000, "F:/10000.txt");
	}

第一种:

通过PrintStream流进行输出:

     * 生成1万条数据共花费193毫秒 
     * 生成10万条数据共花费552毫秒 
     * 生成100万条数据共花费2667毫秒 
     * 生成1000万条数据共花费19539毫秒
     * 生成1亿条数据共花费180303毫秒

private static void createDate(int amount, int time, String filePath) throws Exception {
		File file = new File(filePath);
		PrintStream ps = new PrintStream(file);

		long start = System.currentTimeMillis();
		long row = 1;
		StringBuffer bf = new StringBuffer();
		for (int j = 0; j < amount; j++) {
			for (int i = 0; i < time; i++) {
				String uuid = UUID.randomUUID().toString();
				String name = uuid.substring(0, 4);
				int sex = -1;
				if (Math.random() < 0.51) {
					sex = 1;
				} else {
					sex = 0;
				}
				String phone = (String) RandomValue.getAddress().get("tel");
				bf.append(row + "\t" + name + "\t" + sex + "\t" + phone + "\t" + uuid + "\n");
				row++;
			}
			ps.println(bf.toString());
			bf.setLength(0);
		}
		ps.close();
		long end = System.currentTimeMillis();
		System.out.println("生成10000万条数据共花费" + (end - start) + "毫秒");

	}

第二种:

通过BufferedWriter流进行输出:

     * 生成1万条数据共花费155毫秒 
     * 生成10万条数据共花费599毫秒 
     * 生成100万条数据共花费2364毫秒 
     * 生成1000万条数据共花费18197毫秒
     * 生成1亿条数据共花费174275毫秒

private static void createDateByBufferedWriter(int amount, int time, String filePath) throws Exception {
		File file = new File(filePath);
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));

		long start = System.currentTimeMillis();
		long row = 1;
		StringBuffer bf = new StringBuffer();
		for (int j = 0; j < amount; j++) {
			for (int i = 0; i < time; i++) {
				String uuid = UUID.randomUUID().toString();
				String name = uuid.substring(0, 4);
				int sex = -1;
				if (Math.random() < 0.51) {
					sex = 1;
				} else {
					sex = 0;
				}
				String phone = (String) RandomValue.getAddress().get("tel");
				bf.append(row + "\t" + name + "\t" + sex + "\t" + phone + "\t" + uuid + "\n");
				row++;
			}
			bw.write(bf.substring(0, bf.length()-1));
			bf.setLength(0);
		}
		bw.close();
		long end = System.currentTimeMillis();
		System.out.println("生成10000万条数据共花费" + (end - start) + "毫秒");
	}

总结:

为了数据的快速生成,我这里利用了StringBuffer作为缓冲区,并且通过实例验证得出StringBuffer在10W条的时候运行没有问题,但100W条会发生内存溢出,具体界限在哪没有深究。

关于文件导入到数据库就不说了,感兴趣的可以自己动手测试测试,按照上面这两种方式,生成数据会非常快,但是文件大了之后利用load in file导入到数据库中就会变慢很多,因此这两种方式和前天写的批量添加的方式各有优劣吧,具体情况可以根据具体的业务需求来制定选用哪种方式。

在上一次的博客里,有位同志评论说上亿条数据可以考虑换方案,但就目前我的水平来说,没有想到用什么技术来实现这种数据迁移,希望有大佬知道的话可以评论区分享分享~

猜你喜欢

转载自blog.csdn.net/qq_41204714/article/details/85804176
今日推荐