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

C++ mysql  mysqlclient split one big table to multiple small tables averagely

 

 

SELECT GROUP_CONCAT(table_name) FROM information_schema.tables where table_schema='db';

C++ mysql  mysqlclient split one big table to multiple small tables averagely

 

 

select * from mt41;

 

C++ mysql  mysqlclient split one big table to multiple small tables averagely

 

 

select * from mt81;

C++ mysql  mysqlclient split one big table to multiple small tables averagely

 

 

select * from mt100;

C++ mysql  mysqlclient split one big table to multiple small tables averagely

 

原文链接: https://www.cnblogs.com/Fred1987/p/16290929.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ mysql  mysqlclient split one big table to multiple small tables averagely

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401446

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:15
下一篇 2023年4月19日 上午9:15

相关推荐