一个C ++程序,将超过10亿条记录加载到内存中?

时间:2013-09-06 19:04:01

标签: c++ database boost stl shared-memory

我在数据库中有一整年的期权价格(以及其他一些信息,如执行价格,波动率等),并希望将其加载到内存中以供我的程序处理(哈希映射,程序已编写)在C ++)。问题是在启动时(从本地数据库)加载到内存需要10个多小时。

有人可以建议我如何克服这个问题吗?我有解决方法,我只加载我需要的部分,但如果人们可以分享问题的想法,那将是很好的。是否会在每次启动后从共享内存加载数据?

到目前为止,这是代码,简而言之,它查询数据库并将数据加载到容器中

    const std::string m_url = "mysql://localhost/test2?user=root&password=";
    URL_T optionURL = URL_new(m_url.c_str());
    ConnectionPool_T optionPool = ConnectionPool_new(optionURL);
    ConnectionPool_setInitialConnections(optionPool,1);
    ConnectionPool_setMaxConnections(optionPool,1);
    ConnectionPool_start(optionPool);
    Connection_T con = ConnectionPool_getConnection(optionPool);
    ResultSet_T result = Connection_executeQuery(con,
                                                 "SELECT DISTINCT(underlying) FROM  Options");
    PreparedStatement_T prepareStatement = Connection_prepareStatement(con,"SELECT  underlying,underlyingPrice,expiry,strike,issueDate,type,delta,volatility FROM Options  o,RiskFreeRate r WHERE underlying = ? AND o.issueDate = r.date");

    while(ResultSet_next(result))
    {
        const std::string symbol = ResultSet_getString(result,1);
        PreparedStatement_setString(prepareStatement,1,symbol.c_str());
        ResultSet_T resultDetail  = PreparedStatement_executeQuery(prepareStatement);
        while(ResultSet_next(resultDetail))
        {
            float strike = ResultSet_getDouble(resultDetail,4);
            date expiry = from_string(ResultSet_getString(resultDetail,3));
            std::string issueDate = ResultSet_getString(resultDetail,5);
            float underlyingPrice = ResultSet_getDouble(resultDetail,2);
            float riskFreeRate = 4; //tmp hack
            float volatility = ResultSet_getDouble(resultDetail,8);
            OptionDateMap optionMap = m_optionMap[symbol];
            OptionVec optionVec = optionMap[issueDate];
            optionVec.push_back(boost::shared_ptr<WallStreet::FixedIncome::Option::Option> (new WallStreet::FixedIncome::Option::Option(strike,expiry,underlyingPrice, riskFreeRate,volatility)));
            optionMap[issueDate] = optionVec;
            m_optionMap[symbol] = optionMap;

    }
}

0 个答案:

没有答案