Oracle 快速load資料的方法--SQL*Loader

來源:互聯網
上載者:User

Oracle可以快速load資料,相比之前測試過的批量插入方法(1KW條記錄耗時近2分鐘)也要快很多

準備資料檔案

在記憶體的vector中產生1KW條記錄,然後寫到指定的資料檔案中

#include <string.h>
#include <stdio.h>

#include <unistd.h>
#include <vector>
using namespace std;

#include "clocktool.h"

typedef struct User_Record
{
    char id[16];
    char name[32];
    char passwd[16];
}USER_RECORD;


int main(int argc, char** argv)
{
    int iTimes = 10000000;

    vector<User_Record*> vec;
    TimeTool cktool;

    User_Record* pRecord = NULL;
    cktool.begin();
    for (int i = 0; i < iTimes; ++i)
    {
        pRecord = new User_Record;
        memset(pRecord, 0, sizeof(struct User_Record));
        sprintf(pRecord->id, "%d", i);
        sprintf(pRecord->name, "%ld", 18600000000+i);
        strcpy(pRecord->passwd, "111111");
        vec.push_back(pRecord);
    }
    cktool.end();
    printf("cost %f\n", cktool.getInterval());

    cktool.reset();

    cktool.begin();
    FILE* pf = fopen("./load.data", "w");
    if (pf)
    {
        char buff[256];
        int iResult = 0;
        for (int i = 0; i < iTimes; ++i)
        {
            memset(buff, 0, sizeof(buff));
            iResult = sprintf(buff, "%s, %s, %s\n", vec[i]->id, vec[i]->name, vec[i]->passwd);
            fwrite(buff, 1, iResult, pf);
        }
        fclose(pf);
    }
    cktool.end();
    printf("cost %f\n", cktool.getInterval());

    vector<User_Record*>::iterator it = vec.begin();
    for (; it != vec.end(); ++it)
    {
        delete (*it);
    }
    return 0;
}

執行結果:

cost 2.796853
cost 3.715381

通過SQL*Loader載入資料

編寫load.ctl檔案

load data
infile 'load.data'
into table base_users
fields terminated by ","
(userid, username, passwd)

清除原有資料

SQL> truncate table base_users;

對於大資料量的載入,追求效率的時候建議關閉log

SQL> alter table base_users nologging;

運行load載入命令

sqlldr userid=cat/cat control=load.ctl log=load.log direct=y


通過測試發現其中direct=y非常重要,大幅提升了資料載入的效率。檢查load.log日誌可以發現執行時間大概就是在12秒左右

Total logical records skipped:          0
Total logical records read:      10000000
Total logical records rejected:         0
Total logical records discarded:        0
Total stream buffers loaded by SQL*Loader main thread:     2204
Total stream buffers loaded by SQL*Loader load thread:        0


Run began on Sat Mar 31 22:47:07 2012
Run ended on Sat Mar 31 22:47:19 2012


Elapsed time was:     00:00:11.73
CPU time was:         00:00:05.31

更多Oracle相關資訊見Oracle 專題頁面 http://www.bkjia.com/topicnews.aspx?tid=12

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.