Robin Law
Robin Law

Reputation: 29

C++ how to assign data queryed from postgresql to a struct and store in std::vector fast

I have large data in postgresql, more than 1, 000, 000 rows.The data structure in postgresql is like this:

CREATE TABLE tickdata
(
    datetime character varying COLLATE pg_catalog."default" NOT NULL,
    date character varying COLLATE pg_catalog."default" NOT NULL,
    "time" character varying COLLATE pg_catalog."default" NOT NULL,
    instrumentid character varying COLLATE pg_catalog."default" NOT NULL,
    presettlementprice double precision NOT NULL,
    precloseprice double precision NOT NULL,
    preopeninterest bigint NOT NULL,
    openprice double precision NOT NULL,
    lastprice double precision NOT NULL,
    highprice double precision NOT NULL,
    lowprice double precision NOT NULL,
    averageprice double precision NOT NULL,
    upperlimit double precision NOT NULL,
    lowerlimit double precision NOT NULL,
    bidprice1 double precision NOT NULL,
    bidvolume1 bigint NOT NULL,
    askprice1 double precision NOT NULL,
    askvolume1 bigint NOT NULL,
    volume bigint NOT NULL,
    turnover double precision NOT NULL,
    openinterest bigint NOT NULL,
    lastvolume bigint NOT NULL,
    lastopeninterest bigint NOT NULL,
    gatewayname character varying COLLATE pg_catalog."default" NOT NULL,
    exchangeid character varying COLLATE pg_catalog."default" NOT NULL,
    CONSTRAINT hc1605_pkey PRIMARY KEY(datetime)
)

And the data struct in C++ is like this :

struct TICK {

    std::string datetime;
    std::string date;
    std::string time;
    std::string instrumentID;
    double preSettlementPrice;
    double preClosePrice;
    int preOpenInterest;
    double openPrice;
    double lastPrice;
    double highPrice;
    double lowPrice;
    double averagePrice;
    double upperLimit;
    double lowerLimit;
    double bidPrice1;
    int bidVolume1;
    double askPrice1;
    int askVolume1;
    int volume;
    double turnover;
    int openInterest;
    int lastVolume;
    int lastOpeninterest;
    std::string ExchangeID;

};

I mean, the column names in the table of postgresql is same as the members of the structure.

Now I want to query 1, 000, 000 row of data from postgresql table and assign the data to a vector with TICK structure.

My code is as following :

std::vector<TICK> tickMap;
std::string sqlQuery = "SELECT * FROM tickdata where datetime between 'XXXXX' and 'XXXXX'";
pqxx::connection conn(testTickConn);
try
{
    start = clock();
    pqxx::nontransaction dbQuery(conn);
    pqxx::result rs = (dbQuery.exec(sqlQuery));
    dbQuery.commit();
    finish = clock();
    double duration = (double)(finish - start) / CLOCKS_PER_SEC;
    emit sendLogData(QString::fromStdString("datamanager load " + instrumentID + " " + std::to_string(rs.size()) + "takes " + std::to_string(duration) + " seconds"));
    start = clock();
    for (auto& row : rs) {

        tick.datetime = QDateTime::fromString(QString::fromStdString(row[1].as<std::string>()), "yyyyMMdd hh:mm:ss.z");
        tick.date = row[2].as<std::string>();
        tick.time = row[3].as<std::string>();
        tick.instrumentID = row[4].as<std::string>();
        tick.preSettlementPrice = row[5].as<double>();
        tick.preClosePrice = row[6].as<double>();

        tick.preOpenInterest = row[7].as<int>();
        tick.openPrice = row[8].as<double>();
        tick.lastPrice = row[9].as<double>();
        tick.highPrice = row[10].as<double>();
        tick.lowPrice = row[11].as<double>();
        tick.averagePrice = row[12].as<double>();
        tick.upperLimit = row[13].as<double>();
        tick.lowerLimit = row[14].as<double>();
        tick.bidPrice1 = row[15].as<double>();
        tick.bidVolume1 = row[16].as<int>();
        tick.askPrice1 = row[17].as<double>();
        tick.askVolume1 = row[18].as<int>();
        tick.volume = row[19].as<int>();
        tick.turnover = row[20].as<double>();
        tick.openInterest = row[21].as<int>();
        tick.lastVolume = row[22].as<int>();
        tick.lastOpeninterest = row[23].as<int>();
        tick.ExchangeID = row[24].as<std::string>();


        //then I want to push back the tick to a map. Maybe I can do something else like put an tick envent....
        tickMap['tickdata'].push_back(tick);
        //emit sendTickData(tick);
    }

    conn.close();

    return true;



}
catch (const std::exception& ex)
{
    emit sendLogData(QString::fromStdString(ex.what()));
    return false;

}

It takes me 4 minutes to finish this job. query data from postgresql takes 10 seconds, but the for loop takes me almost 4 minutes.

I can find out there are 2 problems but I don't know how to fix it:

  1. It take long time to change the format from character varying to QDateTime format.

    tick.datetime = QDateTime::fromString(QString::fromStdString(row[1].asstd::string()), "yyyyMMdd hh:mm:ss.z");

two columns need to change format like this. But I need to compare the date time exactly, any other format for time in C++?

  1. assign each row of data to a TICK struct is very time consuming. Can I assign the query result to a vector in one time?

Any solutions? I want to make it fast. Thank you.

Upvotes: 1

Views: 754

Answers (1)

Robin Law
Robin Law

Reputation: 29

thanks for @Sam Varshavchik and @Jesper Juhl kindly help.

I set optimizations enabled, reserve the vector in advance, and make a new structure which only keep the members of int and double for test.

struct TEMPTICK {
    double preSettlementPrice;
    double preClosePrice;
    int preOpenInterest;
    double openPrice;
    double lastPrice;
    double highPrice;
    double lowPrice;
    double averagePrice;
    double upperLimit;
    double lowerLimit;
    double bidPrice1;
    int bidVolume1;
    double askPrice1;
    int askVolume1;
    int volume;
    double turnover;
    int openInterest;
    int lastVolume;
    int lastOpeninterest;

};

The new code is as following:

std::vector<TEMPTICK> tickMap;
std::string sqlQuery = "SELECT * FROM tickdata where datetime between 'XXXXX' and 'XXXXX'";
pqxx::connection conn(testTickConn);
try
{
    start = clock();
    pqxx::nontransaction dbQuery(conn);
    pqxx::result rs = (dbQuery.exec(sqlQuery));
    dbQuery.commit();
    finish = clock();
    double duration = (double)(finish - start) / CLOCKS_PER_SEC;
    emit sendLogData(QString::fromStdString("datamanager load " + instrumentID + " " + std::to_string(rs.size()) + "takes " + std::to_string(duration) + " seconds"));
    start = clock();
    tickMap.reserve(re.size());
    for (auto& row : rs) {
        tick.preSettlementPrice = row[5].as<double>();
        tick.preClosePrice = row[6].as<double>();

        tick.preOpenInterest = row[7].as<int>();
        tick.openPrice = row[8].as<double>();
        tick.lastPrice = row[9].as<double>();
        tick.highPrice = row[10].as<double>();
        tick.lowPrice = row[11].as<double>();
        tick.averagePrice = row[12].as<double>();
        tick.upperLimit = row[13].as<double>();
        tick.lowerLimit = row[14].as<double>();
        tick.bidPrice1 = row[15].as<double>();
        tick.bidVolume1 = row[16].as<int>();
        tick.askPrice1 = row[17].as<double>();
        tick.askVolume1 = row[18].as<int>();
        tick.volume = row[19].as<int>();
        tick.turnover = row[20].as<double>();
        tick.openInterest = row[21].as<int>();
        tick.lastVolume = row[22].as<int>();
        tick.lastOpeninterest = row[23].as<int>();


        //then I want to push back the tick to a map. Maybe I can do something else like put an tick envent....
        tickMap.push_back(tick);
        //emit sendTickData(tick);
    }

    conn.close();

    return true;



}
catch (const std::exception& ex)
{
    emit sendLogData(QString::fromStdString(ex.what()));
    return false;

}

So now the structure members are only int and double. It takes 40 seconds for the loop with 1,000,000 rows. Is it normal speed? Before I used pandas with python to load 1,000,000 rows from postgresql to a dataframe, it took less than 20 seconds. I think c++ should be much faster than pandas.

Upvotes: 1

Related Questions