• 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;

  • 相关阅读:
    C# 如何生成CHM帮助文件
    Excel导出问题
    JS一些类实现方式的性能研究
    Date对象的一些相关函数
    ECMASCRIPT5新特性(转载)
    javascript apo
    $linq A Javascript LINQ library
    javascript 编程规范
    flash 工程师的标准
    flash 弹出 网页
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16290929.html
Copyright © 2020-2023  润新知