硕菊叶
硕菊叶
  • 发布:2021-01-07 15:31
  • 更新:2021-01-16 18:12
  • 阅读:1642

unicloud多条件查询,总是报超时。。实际执行5秒都不到。。

分类:uniCloud

"Exec error resulting in state DEAD :: caused by :: operation exceeded time limit" 总是报这个错。。

barcodeCollection.where(dbCmd.or([{
uid:dbCmd.exists(false).or(dbCmd.eq(data.uid)),
boxcode: data.barcode
},
{
uid:dbCmd.exists(false).or(dbCmd.eq(data.uid)),
pagecode: data.barcode
}
]))
代码如上,在条码表中,每个条码为一行,50个条码为一个pagecode,10个pagecode为一个boxcode,所以这里查询的时候需要实现通过扫码查询pagecode或者boxcode,因为不知道条码是什么类型,所以在这里使用多条件查询,然后“uid:dbCmd.exists(false).or(dbCmd.eq(data.uid)),”判断是否是该用户的条码,也就是说这里最多也只有500条数据,数据库中现在有50W数据,在这50W数据中差500条数据都超时了?不应该吧。。求大佬解答一下是我使用方式不对还是怎么样?

2021-01-07 15:31 负责人:无 分享
已邀请:
DCloud_uniCloud_WYQ

DCloud_uniCloud_WYQ

用的是云函数还是clientDB?确认一下用到的字段有没有配置索引

  • 硕菊叶 (作者)

    用的云函数,有配置索引

    2021-01-12 14:29

  • 硕菊叶 (作者)

    只要加了uid这一行,就会报错。。

    2021-01-12 14:30

  • DCloud_uniCloud_WYQ

    回复 硕菊叶: 有用skip或者count了吗,索引怎么配置的?

    2021-01-12 20:03

哈德

哈德

我也遇到了这个问题,能解决了吗?
错误提示:

InternalServerError:errCode: InternalServerError | errMsg: Error in $cursor stage :: caused by :: operation exceeded time limit
//云函数代码  
'use strict';  
exports.main = async (event, context) => {  
    const db = uniCloud.database();  
    const dbCmd = db.command;  
    const collection = db.collection('dataList');  
    const $ = dbCmd.aggregate  
    if (!event.id) {  
        return false  
    }  
    let dats = {};  
    let status = dbCmd.eq(2);  
    if (event.isMe) {  
        status = dbCmd.gte(-2);  
    } else {  
        status = dbCmd.gte(1);  
    }  
    let skip = event.skip*1;  
    if(!skip){  
        skip = 0  
    }  
    if (event.lookup) { //连表查询,添加like数据  
        if (event.type == 'dynamic' || event.type == 'dynamic2') {  
            dats = {  
                uid: event.id,  
                status: status  
            }  
            if (event.sortId) {  
                if (event.sortId > 0) {  
                    dats = {  
                        uid: event.id,  
                        status: status,  
                        sortId: event.sortId  
                    }  
                }  
            }  
        } else {  
            dats = {  
                uid: event.id,  
                status: dbCmd.eq(2)  
            }  
        }  
        let res = await collection.aggregate()  
            .sort({  
                "_id": -1  
            })  
            .match(dats)  
            .skip(skip)  
            .limit(15)  
            .lookup({  
                from: 'user-like',  
                let: {  
                    m_id: '$_id'  
                },  
                pipeline: $.pipeline()  
                    .match(dbCmd.expr($.and([  
                        $.eq(['$mid', '$$m_id']),  
                        $.eq(['$uid', event.id])  
                    ])))  
                    .project({  
                        _id: 1,  
                        uid: 1,  
                        mid: 1  
                    })  
                    .done(),  
                as: 'like'  
            })  
            .end()  
        return res.data;  
    } else {  
        if (event.type == 'dynamic' || event.type == 'dynamic2') {  
            dats = {  
                uid: event.id,  
                status: status  
            }  
            if (event.sortId) {  
                if (event.sortId > 0) {  
                    dats = {  
                        uid: event.id,  
                        status: status,  
                        sortId: event.sortId  
                    }  
                }  
            }  
        } else {  
            dats = {  
                uid: event.id,  
                status: dbCmd.eq(2)  
            }  
        }  
        let res = await collection.where(dats)  
            .skip(event.skip)  
            .limit(15)  
            .orderBy("time", "desc")  
            .get();  
        return res.data  
    }  

};  

该问题目前已经被锁定, 无法添加新回复