日期:2014-05-17  浏览次数:20919 次

OCI BLOB批量操作
我下面的代码是批量更新数据库里BLOB字段,测试之后发现更新1000条50KB的图片要17秒,而C#只需要13秒,理论上我使用OCI应该比C#快,但现在却要慢得多,不知道我的方法正不正确,有没有其他好的方法?

C/C++ code

/*
 * 根据ID批量更新BLOB数据
 * @param    ID和图片路径组成的集合
 * @param    表名称
 * @param    主键名称
 * @param    BLOB字段名称
 * @param    失败的记录
 */
bool OracleStatementImpl::CopyBlobIn(map<string,string>& fileNames, const string& tableName, const string& primaryKey, const string& blobColumnName, map<string,string>& failure)
{
    failure.clear();

    reset();
    string sql = "update "+ tableName + " set "+ blobColumnName + " = :1"
                 +" where " + primaryKey +" = :2";

    char buffer[255];

    CHECK(_sessImpl->errhp, OCIDescriptorAlloc((dvoid *) _sessImpl->envhp, (dvoid **) &lobl,
                         (ub4)OCI_DTYPE_LOB, (size_t) 0, (dvoid **) 0));

    CHECK(_sessImpl->errhp, OCILobCreateTemporary(_sessImpl->svchp,_sessImpl->errhp,lobl,0,SQLCS_IMPLICIT, 
        OCI_TEMP_BLOB, OCI_ATTR_NOCACHE, OCI_DURATION_SESSION));

    CHECK(_sessImpl->errhp, OCIStmtPrepare(stmthp, _sessImpl->errhp, (ub1*)sql.c_str(), strlen(sql.c_str()), OCI_NTV_SYNTAX, OCI_DEFAULT));
    
    CHECK(_sessImpl->errhp, OCIBindByPos(stmthp, &m_pBind, _sessImpl->errhp, 1, (dvoid*)&lobl,
    sizeof(OCILobLocator *), SQLT_BLOB, 0,0,0,0,0, OCI_DEFAULT));

    CHECK(_sessImpl->errhp, OCIBindByPos(stmthp, &m_pBind, _sessImpl->errhp, 
            (ub4)2, (dvoid*)buffer, (sb4)sizeof(buffer), (ub2)SQLT_AVC, 
            0,0,0,0,0, OCI_DEFAULT));

    _sessImpl->begin(); 

    for(map<string,string>::iterator file = fileNames.begin(); file != fileNames.end(); file++)
    {
        strcpy(buffer,file->first.c_str());

        sb4 status = OCIStmtExecute(_sessImpl->svchp, stmthp, _sessImpl->errhp, 
        (ub4)1, (ub4) 0, (CONST OCISnapshot *) NULL, (OCISnapshot *) NULL, 
         OCI_DEFAULT);

        if(status == OCI_NO_DATA)
        {
            failure.insert(*file);
            continue;
        }

        FILE* fp = fopen(file->second.c_str(), (const char *) "rb");
        if(!fp)
        {
            failure.insert(*file);
            continue;
        }

        stream_write_lob(lobl, fp, get_file_length(fp));

        fclose(fp);

    }
    _sessImpl->commit();

    CHECK(_sessImpl->errhp,OCIDescriptorFree((dvoid *) lobl, (ub4) OCI_DTYPE_LOB));

    return (failure.size() == 0);
}



------解决方案--------------------
汗,遇到了类似的问题,不过我的是批量获取blob字段慢~~~