Document DB 5秒存储过程执行限制 [英] Document DB 5 secs stored procedure execution limit
问题描述
我有一个存储过程,该过程返回4000个文档,由于5秒的执行限制,我的存储过程未重新调整任何数据.我试图处理集合接受的值,但是它没有按预期工作.
I have a stored procedure which return 4000 documents, due to 5 sec execution limit my sproc is not retuning any data. I tried to handled collection accepted value but it is not working as expected.
我尝试将响应设置为返回",如果达到5秒的限制,则sproc未设置响应.
I tried to set response to "return" in case of 5 sec limit hit, sproc is not setting response.
function getRequirementNodes()
{
var context = getContext();
var response = context.getResponse();
var collection = context.getCollection();
var collectionLink = collection.getSelfLink();
var nodesBatch = [];
var continueToken= true;
var query = { query: 'SELECT * from root '};
getNodes(null)
function getNodes(continuation)
{
var requestOptions = {continuation: continuation};
var accepted = collection.queryDocuments(collectionLink, query, requestOptions,
function(err, documentsRead, responseOptions)
{
if (documentsRead.length > 0)
{
nodesBatch = nodesBatch.concat(documentsRead);
}
else if (responseOptions.continuation)
{
continueToken = responseOptions.continuation
nodesBatch = nodesBatch.concat(documentsRead);
getNodes(responseOptions.continuation);
}
else
{
continueToken= false;
response.setBody(nodesBatch);
}
});
if (!accepted)
{
response.setBody("return");
}
}
}
推荐答案
脚本从未返回空响应,因为从未调用包含response.setBody()
的块.
The script is returning a empty response, because the blocks containing response.setBody()
are never called.
我会解释.让我们细分queryDocuments
回调的这一部分:
I'll explain. Let's break this section of queryDocuments
callback down:
if (documentsRead.length > 0) {
nodesBatch = nodesBatch.concat(documentsRead);
} else if (responseOptions.continuation) {
continueToken = responseOptions.continuation
nodesBatch = nodesBatch.concat(documentsRead);
getNodes(responseOptions.continuation);
} else {
continueToken = false;
response.setBody(nodesBatch);
}
请注意,如果查询的结果位于第一页之内(很可能会)...脚本会将查询结果附加到nodesBatch
:
Note that if the query has results inside the first page (which it most likely will)... The script will append the query results on to nodesBatch
:
if (documentsRead.length > 0) {
nodesBatch = nodesBatch.concat(documentsRead);
}
然后,脚本将完成.响应正文未设置(为空),并且如果存在延续令牌,脚本不会发出后续查询.
The script will then complete. The response body is unset (empty), and the script does not issue a follow up query if there is a continuation token.
假设集合不为空,那么这可能是您遇到的行为.
Assuming the collection isn't empty, then this probably the behavior you are experiencing.
注意::如果您要查询大型数据集,则可以达到响应大小限制(1 MB).
Note: If you are querying a large dataset, it's possible to hit the response size limit (1 MB).
我重新编写了脚本以解决上述问题,并提供了一个片段来说明如何处理响应大小限制:
I've re-wrote the script to fix the issue above, and have included a snippet to illustrate how to handle the response size limit:
function getRequirementNodes(continuationToken) {
var context = getContext();
var response = context.getResponse();
var collection = context.getCollection();
var collectionLink = collection.getSelfLink();
var nodesBatch = [];
var lastContinuationToken;
var responseSize = 0;
var query = {
query: 'SELECT * FROM root'
};
getNodes(continuationToken);
function getNodes(continuationToken) {
// Tune the pageSize to fit your dataset.
var requestOptions = {
continuation: continuationToken,
pageSize: 1
};
var accepted = collection.queryDocuments(collectionLink, query, requestOptions,
function(err, documentsRead, responseOptions) {
// The size of the current query response page.
var queryPageSize = JSON.stringify(documentsRead).length;
// DocumentDB has a response size limit of 1 MB.
if (responseSize + queryPageSize < 1024 * 1024) {
// Append query results to nodesBatch.
nodesBatch = nodesBatch.concat(documentsRead);
// Keep track of the response size.
responseSize += queryPageSize;
if (responseOptions.continuation) {
// If there is a continuation token... Run the query again to get the next page of results
lastContinuationToken = responseOptions.continuation;
getNodes(responseOptions.continuation);
} else {
// If there is no continutation token, we are done. Return the response.
response.setBody({
"message": "Query completed succesfully.",
"queryResponse": nodesBatch
});
}
} else {
// If the response size limit reached; run the script again with the lastContinuationToken as a script parameter.
response.setBody({
"message": "Response size limit reached.",
"lastContinuationToken": lastContinuationToken,
"queryResponse": nodesBatch
});
}
});
if (!accepted) {
// If the execution limit reached; run the script again with the lastContinuationToken as a script parameter.
response.setBody({
"message": "Execution limit reached.",
"lastContinuationToken": lastContinuationToken,
"queryResponse": nodesBatch
});
}
}
}
这篇关于Document DB 5秒存储过程执行限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!