Skip to content

Instantly share code, notes, and snippets.

@imiskolee
Last active March 20, 2019 03:09
Show Gist options
  • Save imiskolee/44e04556187de005acd68de193e9f946 to your computer and use it in GitHub Desktop.
Save imiskolee/44e04556187de005acd68de193e9f946 to your computer and use it in GitHub Desktop.
MySQL Merge Two Same Struct Tables
#!/bin/bash
database=""
from_table=""
dest_table=""
batch_size=5000
last_id=""
user=""
pwd=""
i=0
while [ true ]
do
t=$(( i*batch_size ))
echo "last_id:$last_id $i $t"
sql="REPLACE INTO ${database}.$dest_table SELECT * FROM ${database}.${from_table} where id > $last_id LIMIT ${batch_size}"
echo $sql
date
mysql -u${user} -p${pwd} -e "$sql"
new_id=`mysql -u${user} -p${pwd} -s -N -e "SELECT id FROM ${database}.${from_table} where id > $last_id LIMIT ${batch_size},1"`
if [ -z $new_id ]
then
echo "done."
break
fi
last_id=$new_id
i=$(( i+1 ))
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment