Testing more, even with covered indexes (See explain) N1QL is slower. I have tested with other views and n1ql and produced similar results. Any idea on how to improve it? I even tried to return just the meta().id in the N1QL and
"view_duration = 0.332000941 rows=221"
“n1ql_duration = 0.691879602 rows=221”
Current View
function (doc, meta) {
if (meta.id && (meta.id.indexOf('_sync') === -1) && !doc._deleted) {
if (doc.type === 'change-log-entry') {
if( doc.changedAt && doc.changeType === 'task-create' && doc.changeInfo && doc.changeInfo.details) {
var normalizeDate = new Date(doc.changedAt);
var isEmergency = !!doc.changeInfo.details.isEmergency;
emit([doc.location, normalizeDate.toISOString()], {
'taskId': doc.primaryTarget,
'isEmergency':isEmergency});
}
}
}
}
N1QL
Index
CREATE INDEX `change_log_task_create_v1` ON `sync_gateway_sw1`(`type`,(meta().`id`),`_deleted`,softDelete, `location`,`changeType`,`changedAt`, changeInfo.details.isEmergency)
WHERE ((((((not ((meta().`id`) like "_sync:%")) and ((`_deleted` = false) or (`_deleted` is missing))) AND (softDelete is MISSING or softDelete = false) and (`type` = "change-log-entry")) and
(`changeType` = "task-create")) and (`location` is not null)) and (`changedAt` is not null))
and changeInfo is NOT NULL and changeInfo.details is NOT NULL
Query
select meta().id,location, changeInfo.details.isEmergency
FROM sync_gateway_sw1 use index(change_log_task_create_v1)
WHERE meta().id NOT LIKE '_sync:%'
AND (_deleted = FALSE OR _deleted IS MISSING)
AND (softDelete is MISSING or softDelete = false)
AND type = ‘change-log-entry’ and changeType = 'task-create’
and location is not null and changedAt is not null
and location = '79497d97-3eb6-400c-8ed0-d48d28d327cb’
and changedAt between ‘2015-08-22’ and '2016-08-22’
and changeInfo is NOT NULL and changeInfo.details is NOT NULL
EXPLAIN
[
{
“plan”: {
"#operator": “Sequence”,
"~children": [
{
"#operator": “IndexScan”,
“covers”: [
“cover ((sync_gateway_sw1
.type
))”,
“cover ((meta(sync_gateway_sw1
).id
))”,
“cover ((sync_gateway_sw1
._deleted
))”,
“cover ((sync_gateway_sw1
.softDelete
))”,
“cover ((sync_gateway_sw1
.location
))”,
“cover ((sync_gateway_sw1
.changeType
))”,
“cover ((sync_gateway_sw1
.changedAt
))”,
“cover ((((sync_gateway_sw1
.changeInfo
).details
).isEmergency
))”,
“cover ((meta(sync_gateway_sw1
).id
))”
],
“filter_covers”: {
“cover ((((sync_gateway_sw1
.changeInfo
).details
) is not null))”: true,
“cover (((sync_gateway_sw1
.changeInfo
) is not null))”: true,
“cover (((sync_gateway_sw1
.changedAt
) is not null))”: true,
“cover (((sync_gateway_sw1
.location
) is not null))”: true,
“cover ((sync_gateway_sw1
.changeType
))”: “task-create”,
“cover ((sync_gateway_sw1
.type
))”: “change-log-entry”,
“cover ((not ((meta(sync_gateway_sw1
).id
) like “_sync:%”)))”: true
},
“index”: “change_log_task_create_v1”,
“index_id”: “1d6cfba2866ddb20”,
“keyspace”: “sync_gateway_sw1”,
“namespace”: “default”,
“spans”: [
{
“Range”: {
“High”: [
“successor(“change-log-entry”)”
],
“Inclusion”: 1,
“Low”: [
"“change-log-entry”"
]
}
}
],
“using”: “gsi”
},
{
"#operator": “Parallel”,
"~child": {
"#operator": “Sequence”,
"~children": [
{
"#operator": “Filter”,
“condition”: “((((((((((cover ((not ((meta(sync_gateway_sw1
).id
) like “_sync:%”))) and ((cover ((sync_gateway_sw1
._deleted
)) = false) or (cover ((sync_gateway_sw1
._deleted
)) is missing))) and ((cover ((sync_gateway_sw1
.softDelete
)) is missing) or (cover ((sync_gateway_sw1
.softDelete
)) = false))) and (cover ((sync_gateway_sw1
.type
)) = “change-log-entry”)) and (cover ((sync_gateway_sw1
.changeType
)) = “task-create”)) and cover (((sync_gateway_sw1
.location
) is not null))) and cover (((sync_gateway_sw1
.changedAt
) is not null))) and (cover ((sync_gateway_sw1
.location
)) = “79497d97-3eb6-400c-8ed0-d48d28d327cb”)) and (cover ((sync_gateway_sw1
.changedAt
)) between “2015-08-22” and “2016-08-22”)) and cover (((sync_gateway_sw1
.changeInfo
) is not null))) and cover ((((sync_gateway_sw1
.changeInfo
).details
) is not null)))”
},
{
"#operator": “InitialProject”,
“result_terms”: [
{
“expr”: “cover ((meta(sync_gateway_sw1
).id
))”
},
{
“expr”: “cover ((sync_gateway_sw1
.location
))”
},
{
“expr”: “cover ((((sync_gateway_sw1
.changeInfo
).details
).isEmergency
))”
}
]
},
{
"#operator": “FinalProject”
}
]
}
}
]
},
“text”: “select meta().id,location, changeInfo.details.isEmergency\nFROM sync_gateway_sw1 use index(change_log_task_create_v1)\nWHERE meta().id NOT LIKE ‘_sync:%’ \nAND (_deleted = FALSE OR _deleted IS MISSING) \nAND (softDelete is MISSING or softDelete = false) \nAND type = ‘change-log-entry’ and changeType = ‘task-create’\nand location is not null and changedAt is not null\nand location = ‘79497d97-3eb6-400c-8ed0-d48d28d327cb’\nand changedAt between ‘2015-08-22’ and ‘2016-08-22’\nand changeInfo is NOT NULL and changeInfo.details is NOT NULL”
}
]
METRICS:
"view_duration = 0.332000941 rows=221"
“n1ql_duration = 0.691879602 rows=221”