I want to be able to find all tasks with a location of ‘blah’ with a user’s name who created it. Using 4.6.3.
Task
{
base: ‘task’,
locationRoot: ‘blah’,
createdBy: 'user1’
parent: null
}
User - ID: - user1
{
id: user1,
name: 'bob',
type: user
}
INDEXES
CREATE INDEX tasks_idx
ON test
(base
,locationRoot
,createdBy
) WHERE (((base
= “task”) and (locationRoot
is not null)) and (parent
is null))
CREATE INDEX createdby_idx
ON test
(createdBy
,name
)
No Join
Query
TIME - 62 ms
Select t.base, t.type, t.locationRoot, meta(t
).id
From test
t
Where t.base=“task” AND t.locationRoot = ‘blah’ and t.parent IS NULL
EXPLAIN
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"covers": [
"cover ((`t`.`base`))",
"cover ((`t`.`locationRoot`))",
"cover ((`t`.`createdBy`))",
"cover ((meta(`t`).`id`))"
],
"filter_covers": {
"cover (((`t`.`locationRoot`) is not null))": true,
"cover (((`t`.`parent`) is null))": true,
"cover ((`t`.`base`))": "task",
"cover ((`t`.`parent`))": null
},
"index": "tasks_idx",
"index_id": "ae1445162a704134",
"keyspace": "test",
"limit": "1000",
"namespace": "default",
"spans": [
{
"Exact": true,
"Range": {
"High": [
"\"task\"",
"successor(\"blah\")"
],
"Inclusion": 1,
"Low": [
"\"task\"",
"\"blah\""
]
}
}
],
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((cover ((`t`.`base`)) = \"task\") and (cover ((`t`.`locationRoot`)) = \"blah\")) and cover (((`t`.`parent`) is null)))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "cover ((`t`.`base`))"
},
{
"expr": "cover ((`t`.`locationRoot`))"
},
{
"expr": "cover ((meta(`t`).`id`))"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
{
"#operator": "Limit",
"expr": "1000"
}
]
},
"text": "Select t.base, t.locationRoot, meta(`t`).id\nFrom `test` t\nWhere t.base=\"task\" AND t.locationRoot = 'blah' and t.parent IS NULL Limit 1000"
}
]
JOINING
Query
TIME - 2 seconds
Select t.base, t.type, t.locationRoot, meta(t
).id, u.name
From test
t LEFT JOIN test
u ON KEYS t.createdBy
Where t.base=“task” AND t.locationRoot = ‘blah’ and t.parent IS NULL
EXPLAIN
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "tasks_idx",
"index_id": "ae1445162a704134",
"keyspace": "test",
"namespace": "default",
"spans": [
{
"Exact": true,
"Range": {
"High": [
"\"task\"",
"successor(\"blah\")"
],
"Inclusion": 1,
"Low": [
"\"task\"",
"\"blah\""
]
}
}
],
"using": "gsi"
},
{
"#operator": "Fetch",
"as": "t",
"keyspace": "test",
"namespace": "default"
},
{
"#operator": "Join",
"as": "u",
"keyspace": "test",
"namespace": "default",
"on_keys": "(`t`.`createdBy`)",
"outer": true
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "((((`t`.`base`) = \"task\") and ((`t`.`locationRoot`) = \"blah\")) and ((`t`.`parent`) is null))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`t`.`base`)"
},
{
"expr": "(`t`.`type`)"
},
{
"expr": "(`t`.`locationRoot`)"
},
{
"expr": "(meta(`t`).`id`)"
},
{
"expr": "(`u`.`name`)"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
"text": "Select t.base, t.type, t.locationRoot, meta(`t`).id, u.name\nFrom `test` t LEFT JOIN `test` u ON KEYS t.createdBy\nWhere t.base=\"task\" AND t.locationRoot = 'blah' and t.parent IS NULL"
}
]