2017-02-15 8 views
0

まずドキュメントDBは(カウントのためのサポートを提供していません)と、彼らは問題がある、私は取得異なる結果が

function usp_GetInfinityDataView(param) { 
    var context = getContext(); 
    var response = context.getResponse(); 
    var collection = context.getCollection(); 
    var collectionLink = collection.getSelfLink(); 
    var Rount = 0; 
    count("SELECT i.id FROM infinity i",null); 

    var query = { 
    query: 'SELECT * FROM infinity i' 
    }; 

    getNodes(param["ContinuationToken"],param["PageSize"]); 

    function getNodes(continuationToken,intPageSize) { 
    // Tune the pageSize to fit your dataset. 
    var requestOptions = { 
     continuation: continuationToken, 
     pageSize: intPageSize 
    }; 

    var accepted = collection.queryDocuments(collectionLink, query, requestOptions, 
     function(err, documentsRead, responseOptions) { 
     response.setBody({ 
       "ResponseContinuation": responseOptions.continuation, 
       "Count": Rount, 
       "ViewData": documentsRead 
      }); 
     }); 
    } 

    function count(filterQuery, continuationToken) { 
    var collection = getContext().getCollection(); 
    var maxResult = 99999999999; // MAX number of docs to process in one batch, when reached, return to client/request continuation. 
         // intentionally set low to demonstrate the concept. This can be much higher. Try experimenting. 
         // We've had it in to the high thousands before seeing the stored proceudre timing out. 

    // The number of documents counted. 
    var result = 0; 

    tryQuery(continuationToken); 

    // Helper method to check for max result and call query. 
    function tryQuery(nextContinuationToken) { 
     var responseOptions = { continuation: nextContinuationToken, pageSize : maxResult }; 

     // In case the server is running this script for long time/near timeout, it would return false, 
     // in this case we set the response to current continuation token, 
     // and the client will run this script again starting from this continuation. 
     // When the client calls this script 1st time, is passes empty continuation token. 
     if (result >= maxResult || !query(responseOptions)) { 
      setBody(nextContinuationToken); 
     } 
    } 

    function query(responseOptions) { 
     // For empty query string, use readDocuments rather than queryDocuments -- it's faster as doesn't need to process the query. 
     return (filterQuery && filterQuery.length) ? 
      collection.queryDocuments(collection.getSelfLink(), filterQuery, responseOptions, onReadDocuments) : 
      collection.readDocuments(collection.getSelfLink(), responseOptions, onReadDocuments); 
    } 

    // This is callback is called from collection.queryDocuments/readDocuments. 
    function onReadDocuments(err, docFeed, responseOptions) { 
     if (err) { 
      throw 'Error while reading document: ' + err; 
     } 

     // Increament the number of documents counted so far. 
     result += docFeed.length; 

     // If there is continuation, call query again with it, 
     // otherwise we are done, in which case set continuation to null. 
     if (responseOptions.continuation) { 
      tryQuery(responseOptions.continuation); 
     } else { 
      setBody(null); 
     } 
    } 

    // Set response body: use an object the client is expecting (2 properties: result and continuationToken). 
    function setBody(continuationToken) { 
     Rount = result; 
    } 
} 
} 

の下に私のストアドプロシージャで使用されている数を取得するためにstored procedureを提供してきました私はこの手順をC#web apiから呼び出すたびに、実際の数ではなく、実際の数を返します(結果セット数は17491ですが、17020または17202と時々合計を返します)。私は両方の手順を分離しようとしましたが、それでも同じです。

答えて

0

ストアドプロシージャは、いくつかの制限(少なくとも時間)を超えると、強制終了されます。そのため、リンクした元の例では、本文中に継続トークンが返されました。上のバージョンで削除されていますが、復元する必要があります。このようにして、ストアドプロシージャの最初の呼び出しから返された結果に継続トークンが含まれている場合は、そのトークンを再度呼び出すことができます。最終的なカウントは、ストアドプロシージャへのすべての呼び出しの合計になります。

また、maxResultを1000または10,000などに戻すこともできます。これにより細かい粒度が得られ、実際には本当に大きな数値よりも速く答えが得られます。

私はサンプルカウントストアドプロシージャが書き込まれる方法の巨大なファンではありません。 Hereは私の同等物です。主な違いは、返されるオブジェクトの形状が受け入れられる形状と同じであり、終了したストアドプロシージャを再起動するために必要なすべての状態が毎回前後に渡されることです。私はストアドプロシージャを書くこのアプローチについて書いたhere。 documentdb-utils(これらのリンクがあるところ)にはAzureが提供するnode.js SDKのラッパーが含まれています。これにより、実際に終了するまで自動的に中途半端に終了するストアドプロシージャが呼び出されます。完全な開示です、私はdocumentdb-utilsの著者です。

関連する問題