Recently the company has a tens of millions of-row large table that needs to be split into different CSV files according to the city's ID field.
Wrote an automated shell script
Underneath the/HOME/HDH.
LINUX-XUD0:/HOME/HDH # LL
Total 16
-rwxrwxrwx 1 root root 902 Dec 07:47 cf.sh
-RWXRWXRWX 1 root root 6 Dec 07:47 id_1.txt
-RWXRWXRWX 1 root root 6 Dec 20:00 log
Vim cf.sh
#!/bin/bash
date=$ (date + '%y%m%d ')
Cd/home/hdh
Rm-r/home/hdh/file #删除上次执行shell generated file folder
Mkdir/home/hdh/file #创建一个file文件夹用于存放生成的csv File
chmod 777/home/hdh/file-r
echo ' MYSQL-UROOT-PROOT-E ' select DISTINCT ID from xizang.test; "|grep-evi" id "' >/home/hdh/id_1.txt #把该表的id field The value is saved to/home/hdh/id_1.txt.
#cat Id_1.txt |while Read line
For line in $ (<id_1.txt);
Do
# echo ${line} >> id_2.txt
# echo ' mysql-uroot-proot-e ' select DISTINCT ID from xizang.test where id= ' ${line} '; " ' >>id_2.txt
# MYSQL-UROOT-PROOT-E "Select DISTINCT ID from xizang.test where id= ' ${line} ' to outfile '/home/hdh/c.csv ' fields TER minated by ', ' enclosed by ' "' LINES TERMINATED by ' \ r \ n ';"
Mysql-uroot-proot <<eof
SELECT * from xizang.test where id= ' ${line} ' to outfile '/home/hdh/file/${line}_c.csv ' fields TERMINATED by ', ' enclosed By ' "' LINES TERMINATED by ' \ r \ n '; #以id is the where condition, generated into the corresponding CSV.
Exit
Eof
Done
MySQL large table split into CSV export