2017-02-15 2 views
0

erstes Dokument db Zähl- keine Unterstützung bieten() und sie haben ein stored procedure bekommen die Zählung zur Verfügung gestellt, die ich in meiner gespeicherten Prozedur verwendet habe unterErste unterschiedliche Ergebnisse für Zählung in documentdb

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; 
    } 
} 
} 

Das Problem ist, Jedes Mal, wenn ich diese Prozedur aus C# web api aufrufen, gibt es mir andere Ergebnisse und nicht die tatsächliche Anzahl (Wie meine Ergebnismenge ist 17491, aber zurück 17020 oder 17202 und manchmal die Summe). Es funktioniert ordnungsgemäß, wenn die Anzahl der Ergebnisse weniger sind dh auf einer anderen Sammlung. Ich habe versucht, beide Verfahren zu trennen, aber immer noch dasselbe.

Antwort

0

Gespeicherte Prozeduren werden zwangsweise beendet, wenn sie einen oder mehrere Grenzwerte überschreiten (mindestens Zeit). Aus diesem Grund hat das ursprüngliche Beispiel, mit dem Sie eine Verknüpfung hergestellt haben, das Fortsetzungstoken im Textkörper zurückgegeben. Ihre obige Version hat das entfernt, aber Sie müssen es wiederherstellen. Wenn das Ergebnis, das vom ersten Aufruf der gespeicherten Prozedur zurückkehrt, ein Fortsetzungstoken enthält, wissen Sie, dass Sie es erneut aufrufen müssen. Die endgültige Anzahl wird die Summe aller Aufrufe der gespeicherten Prozedur sein.

Sie möchten vielleicht auch die maxResult zurück auf etwas wie 1000 oder 10.000 senken. Dies gibt Ihnen eine feinere Granularität und nach meiner Erfahrung bekommt die Antwort tatsächlich schneller als eine wirklich große Zahl.

Ich bin kein großer Fan der Art, wie die Beispielzählung gespeicherte Prozedur geschrieben wird. Here ist mein Äquivalent. Der Hauptunterschied besteht darin, dass die Form des zurückgegebenen Objekts der Form entspricht, die es akzeptiert, und alle Zustände, die zum Neustarten einer abgeschlossenen gespeicherten Prozedur erforderlich sind, werden jedes Mal hin- und hergereicht. Ich habe über diesen Ansatz zum Schreiben gespeicherter Prozeduren here geschrieben. documentdb-utils (wo diese Links enthalten sind) enthält einen Wrapper für das von Azure bereitgestellte node.js-SDK, das automatisch vorzeitig abgebrochene gespeicherte Prozeduren aufruft, bis sie tatsächlich fertig sind. Vollständige Offenlegung, ich bin der Autor von Documentdb-utils.

Verwandte Themen