2016-08-03 16 views
0

JSONの結果を以下の形式で返すOracle DBをクエリするWeb APIを作成しました。このAPIは入力パラメータの配列を取得します。クエリ結果が非常に大きいため、以下のクエリのように、SQL Developerで試すと313568が返されます。 STPR_STUDY.STD_REF IN( "BL001、TM002")は、以下 タイプ 'System.OutOfMemoryException'の例外がスローされました。WEB API

は私がURL https://bhbl.abc.org/api/Sample?id= 'BL001' & IDを使用していたとき、我々は
public HttpResponseMessage Getdetails([FromUri] string[] id) 
{ 
    string connStr = ConfigurationManager.ConnectionStrings["ProDataConnection"].ConnectionString; 
    using (OracleConnection dbconn = new OracleConnection(connStr)) 
    { 
     var inconditions = id.Distinct().ToArray(); 
     var srtcon = string.Join(",", inconditions); 
     DataSet userDataset = new DataSet(); 
     var strQuery = @"SELECT * from STCD_PRIO_CATEGORY where STPR_STUDY.STD_REF IN(" + srtcon + ")"; 
     using (OracleCommand selectCommand = new OracleCommand(strQuery, dbconn)) 
     { 
      using (OracleDataAdapter adapter = new OracleDataAdapter(selectCommand)) 
      { 
       DataTable selectResults = new DataTable(); 
       adapter.Fill(selectResults); 
       var returnObject = new { data = selectResults }; 
       var response = Request.CreateResponse(HttpStatusCode.OK, returnObject, MediaTypeHeaderValue.Parse("application/json")); 
       ContentDispositionHeaderValue contentDisposition = null; 
       if (ContentDispositionHeaderValue.TryParse("inline; filename=ProvantisStudyData.json", out contentDisposition)) 
       { 
        response.Content.Headers.ContentDisposition = contentDisposition; 
       } 
       return response; 

を使用しているコードですSTCD_PRIO_CATEGORYから

SELECT * =「TM002」 enter image description here

としてDBを照会しかし、私はURL https://bhbl.abc.org/api/Sample?id=「BL001」を使用していたときのために、それはJで結果を返します。 ust 41552 records

大量のデータを返すAPIを作成する方法。任意の助けが大いに評価されます

答えて

2

これはAPIが返すデータが大きすぎるため、プロセスがメモリオーバーフローを生成するためです。ページングSQLを使用する必要があります:

SELECT * 
    FROM (SELECT a.*, ROWNUM rn 
      FROM (SELECT * 
        FROM table_name) a 
     WHERE ROWNUM <= 40) 
WHERE rn >= 21 

クライアントプロキシ一度、複数の呼び出しapi、すべてのデータの取得を完了します。以下

クライアントのコアコード:

// ids: 'BL001' 'TM002', next_key: paging record index 
while (next_key != null) 
{ 
    next_key = GetDetails(ids, next_key); 
} 

private string GetDetails(stirng[] ids, string next_key) 
{ 
    // call api 
    var result = ...; 

    // parse api reponse result 
    object key = result.next_key; 
    if (key == null) return null; 
    string s = key.ToString(); 
    return string.IsNullOrEmpty(s) ? null : s; 
} 

以下のサーバーコアコード:ページングなし

public HttpResponseMessage Getdetails([FromUri] string[] id, [FromUri] string next_key) 
{ 
    // use paging sql 
    // excute sql return record count 
    var count = ... 

    // return next_key 
    if (count < 20) 
    { 
     result.next_key = null; 
    } 
    else 
    { 
     result.next_key = (int.Parse(next_key) + 20).ToString(); 
    } 

    return result; 
} 
+0

は、我々はこれを行うことができますか?クライアントは一度に結果を探しているので、 – trx

+0

@trxクライアントの処理は一度、複数のAPIを呼び出し、すべてのデータ取得を完了します。 –

関連する問題