在matlab中使用并行池来运行多个ODBC连接

时间:2016-09-02 19:37:02

标签: matlab parallel-processing odbc

我需要从数据库中检索2次非常相似的数据,我想在matlab中使用并行池并行运行2查询。这是我的代码:

parpool('local',12)

parfor i=1:2

%Set preferences with setdbprefs.
setdbprefs('DataReturnFormat', 'cellarray');
setdbprefs('NullNumberRead', 'NaN');
setdbprefs('NullStringRead', 'null');

    if i == 1  
        %Make connection to database.  
        %Using ODBC driver.
        conn1 = database('qad', 'ngc', 'ngc');

        %%%%%%%%%%%%%%%%%%%%%%%%%%
        %Read CLOSING PRICE DATA%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%
        curs1 = exec(conn1, ['SELECT    Russell1000_Prices_Close.date_'...
            ' , Russell1000_Prices_Close.RUSSELL1000_mcapW'...
            ' , Russell1000_Prices_Close.ID'...
            ' , Russell1000_Prices_Close.Close_'...
            ' FROM  qai.dbo.Russell1000_Prices_Close']);

        curs1 = fetch(curs1);
        close(curs1);

        %Assign data to output variable
        temp = curs1.Data;
        pricesPivot = pivottable(temp, [1,2], 3, 4, @sum);
        dates_ = datetime(pricesPivot(2:end,1),'inputFormat','yyyy-MM-dd');
        pricesClose_ = cell2mat(pricesPivot(2:end,3:end));
        pricesMarketmcapW_ = cell2mat(pricesPivot(2:end,2));
        compIDs_ = pricesPivot(1,3:end);

        %Close database connection.
        close(conn1);
    else
        %Make connection to database.  
        %Using ODBC driver.
        conn2 = database('qad', 'ngc', 'ngc');

        %%%%%%%%%%%%%%%%%%%%%%%%%%
        %Read OPENING PRICE DATA%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%
        curs2 = exec(conn2, ['SELECT    Russell1000_Prices_Open.date_'...
            ' , Russell1000_Prices_Open.RUSSELL1000_mcapW'...
            ' , Russell1000_Prices_Open.ID'...
            ' , Russell1000_Prices_Open.Open_'...
            ' FROM  qai.dbo.Russell1000_Prices_Open']);

        curs2 = fetch(curs2);
        close(curs2);

        %Assign data to output variable
        temp = curs2.Data;
        pricesPivot = pivottable(temp, [1,2], 3, 4, @sum);
        dates1_ = datetime(pricesPivot(2:end,1),'inputFormat','yyyy-MM-dd');
        pricesOpen_ = cell2mat(pricesPivot(2:end,3:end));
        compIDs_ = pricesPivot(1,3:end);

        %Close database connection.
        close(conn2);
    end
end

首先,我是否正确使用并行池以加快查询速度而不是迭代执行?第二,这是最好的方式还是有其他选择同时运行这两个查询?

0 个答案:

没有答案