C++ mysql mysqlclient split one big table to multiple small tables averagely
作者:互联网
#include <chrono> #include <fstream> #include <iostream> #include <mysql/mysql.h> #include <sstream> #include <string.h> #include <uuid/uuid.h> #include "Model/Util.h" using namespace std; static char *uuidValue = (char *)malloc(40); static char *dtValue = (char *)malloc(20); char *getUuid(); char *getTimeNow(); void splitTables(); int main(int args, char **argv) { splitTables(); } void splitTables() { try { MYSQL *conn, mysql; int state; mysql_init(&mysql); conn = mysql_real_connect(&mysql, "localhost", "root", "Password", "db", 0, 0, 0); if (conn == NULL) { cout << mysql_error(&mysql) << endl; return; } int loops = 100; stringstream ss; int interval=1000000; string str; for (int i = 1; i <= loops; i++) { ss = stringstream(); ss << "create table mt" << i << " (BookIndex int NOT NULL AUTO_INCREMENT,BookId bigint NOT NULL, BookName varchar(100) NOT NULL, BookTitle varchar(100) NOT NULL, PRIMARY KEY (BookIndex));\n"; str=ss.str(); state = mysql_query(conn, str.c_str()); if(state==0) { cout<<"Create table "<<i<<" successfully!"<<endl; } ss=stringstream(); ss << "insert into mt" << i << " select * from mt where BookIndex between " << (i - 1) * interval+1 << " and " << i * interval << ";\n"; // ss<<"delete from mt where BookIndex between "<<(i-1)*1000000<<" and "<<i*1000000<<";\n"; str=ss.str(); state = mysql_query(conn, str.c_str()); if(state==0) { cout<<"Insert into table "<<i<<" successfully! and now is "<<getTimeNow()<<endl; } } } catch (exception &e) { cout << "# ERR: SQLException in " << __FILE__; cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl; cout << "# ERR: " << e.what(); return; } } char *getUuid() { uuid_t newUUID; uuid_generate(newUUID); uuid_unparse(newUUID, uuidValue); return uuidValue; } char *getTimeNow() { time_t rawTime = time(NULL); struct tm tmInfo = *localtime(&rawTime); strftime(dtValue, 20, "%Y%m%d%H%M%S", &tmInfo); return dtValue; }
Compile
g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -luuid -lmysqlclient;
Execute
./h1 0
SELECT GROUP_CONCAT(table_name) FROM information_schema.tables where table_schema='db';
select * from mt41;
select * from mt81;
select * from mt100;
标签:tables,multiple,int,big,char,mysql,table,include,select 来源: https://www.cnblogs.com/Fred1987/p/16290929.html