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;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现