1

我正在尝试编写一个在 SQL 中看起来像这样的查询:

select * from WorkOrder wo
    where wo.userId = 1
    and wo.isSynced = 0
    and wo.status in ('COMPLETE', 'REJECTED', 'SUSPENDED_NO_ACCESS', 'SUSPENDED_OTHER');

我在 userId、isSynced 和 status 上添加了一个索引。

如果我构建如下查询,只要我只过滤 2 个不同的状态值,它就可以工作。只要我添加 3 个或更多,它就不会返回任何结果。我做错了什么还是我需要以完全不同的方式解决这个问题?

//this works
var keyRange = ydn.db.KeyRange.bound([userId, 0, Status.Complete],
            [userId, 0, Status.REJECTED]);
//this doesn't work
var keyRange = ydn.db.KeyRange.bound([userId, 0, Status.Complete],
            [userId, 0, Status.Suspended_AccessUnavailable],
            [userId, 0, Status.REJECTED]);


var iterator = new ydn.db.IndexValueIterator(Store.WorkOrder, 'userId, isSynced, status', keyRange);

return db.values(iterator)
4

1 回答 1

1

目前多查询(“IN”)和自连接(“AND”)是手动的,需要大量的样板来执行这些操作。对于这种情况下的复杂查询,将需要使用ydn.structs.Buffer.

使用排序合并或之字形合并进行自连接。

使用多个游标进行多查询。简述如下:

var iters = [];
var keys = ['COMPLETE', 'REJECTED', 'SUSPENDED_NO_ACCESS', 'SUSPENDED_OTHER'];
for (var i = 0; i < keys.length; i++) {
  iters[i] = ydn.db.IndexValueIterator.where('WorkOrder', 'status', '=', keys[i]);
});
var results = [];
db.scan(function(cursors) {
   // here we have four cursors for respective four index keys.
   // calculate lowest key(s) and put into result
   var min_i = ...
   results.push(cursors[i].getValue());
   // prepare next cursor iteration, 
   var next_position = [];
   next_position[min_i] = true; // advance lowest cursor to next position, while keeping the rest of cursor hold in current position.
   return next_position;
}, iters, 'readonly', ['WorkOrder']).then(function() {
   // use results
   console.log('results', results);
}, function(e) {
   console.error(e.stack);
});

为上述代码制作一个包装器并不难,因此该库最终将支持如下:

 var query = db.from('WorkOrder').where('status', 'in', ['COMPLETE', 'REJECTED', 'SUSPENDED_NO_ACCESS', 'SUSPENDED_OTHER']);
 query.list().then(function() {
   // use results
   console.log('results', results);
}, function(e) {
   console.error(e.message);
});
于 2014-09-05T13:35:30.543 回答