N1QL tutorial for Query has example of how to offset results of the children nodes.
https://query-tutorial.couchbase.com/tutorial/#15
I am trying to write the same query for Couchbase Analytics, but get syntax error.
Goal is to get parent and children starting certain index in children array. For example:
SELECT children[2:array_length(children)]
FROM tutorial
The array slicing syntax is not yet supported in Couchbase Analytics (but it will be soon).
Currently, you can use a nested subquery to get the same result
SELECT (SELECT VALUE c
FROM t.children c
LIMIT array_length(t.children) - 2
OFFSET 2)
FROM tutorial t
Related
I have a document structure which looks something like this:
{
...
"groupedFieldKey": "groupedFieldVal",
"otherFieldKey": "otherFieldVal",
"filterFieldKey": "filterFieldVal"
...
}
I am trying to fetch all documents which are unique with respect to groupedFieldKey. I also want to fetch otherField from ANY of these documents. This otherFieldKey has minor changes from one document to another, but I am comfortable with getting ANY of these values.
SELECT DISTINCT groupedFieldKey, otherField
FROM bucket
WHERE filterFieldKey = "filterFieldVal";
This query fetches all the documents because of the minor variations.
SELECT groupedFieldKey, maxOtherFieldKey
FROM bucket
WHERE filterFieldKey = "filterFieldVal"
GROUP BY groupFieldKey
LETTING maxOtherFieldKey= MAX(otherFieldKey);
This query works as expected, but is taking a long time due to the GROUP BY step. As this query is used to show products in UI, this is not a desired behaviour. I have tried applying indexes, but it has not given fast results.
Actual details of the records:
Number of records = 100,000
Size per record = Approx 10 KB
Time taken to load the first 10 records: 3s
Is there a better way to do this? A way of getting DISTINCT only on particular fields will be good.
EDIT 1:
You can follow this discussion thread in Couchbase forum: https://forums.couchbase.com/t/getting-distinct-on-the-basis-of-a-field-with-other-fields/26458
GROUP must materialize all the documents. You can try covering index
CREATE INDEX ix1 ON bucket(filterFieldKey, groupFieldKey, otherFieldKey);
This is what the cloud datastore doc says but I'm having a hard time understanding what exactly this means:
A projection query that does not use the distinct on clause is a small operation and counts as only a single entity read for the query itself.
Grouping
Projection queries can use the distinct on clause to ensure that only the first result for each distinct combination of values for the specified properties will be returned. This will return only the first result for entities which have the same values for the properties that are being projected.
Let's say i have a table for questions and i only want to get the question text sorted by the created date would this be counted as a single read and rest as small operations?
If your goal is to just project the date and text fields, you can create a composite index on those two fields. When you query, this is a small operation with all the results as a single read. You are not trying to de-duplicate (so no distinct/on) in this case and so it is a small operation with a single read.
I have the following working query in Tinkerpop/Gremlin that can return an ordered list of elements traversed until current step.. How can I make a similar one in OrientDB SQL?
g.V().hasLabel('Firma').has('cui','13904073').until(hasLabel('Persoana')).repeat(inE('Actionar').otherV()).sack().path()
I did a similar traverse using MATCH in OrientDB but i can't find a way to get the ordered list of elements traversed:
MATCH{class:Firma,as: FirmaBaza,where:(cui=13904073)}.(inE("Actionar") {as:actEdge}.outV()) {as:act,where:($matched.FirmaBaza!= $currentMatch and #class=="Firma"),while:(true)}.in("Actionar") {as:actPers,where:(#class=="Persoana")} return $pathElements
Is there any way to get the ordered list of traversed elements using OrientDB SQL ?
I am running tests against my MongoDB and for some reason find has the same performance as count.
Stats:
orders collection size: ~20M,
orders with product_id 6: ~5K
product_id is indexed for improved performance.
Query: db.orders.find({product_id: 6}) vs db.orders.find({product_id: 6}).count()
result the orders for the product vs 5K after 0.08ms
Why count isn't dramatically faster? it can find the first and last elements position with the product_id index
As Mongo documentation for count states, calling count is same as calling find, but instead of returning the docs, it just counts them. In order to perform this count, it iterates over the cursor. It can't just read the index and determine the number of documents based on first and last value of some ID, especially since you can have index on some other field that's not ID (and Mongo IDs are not auto-incrementing). So basically find and count is the same operation, but instead of getting the documents, it just goes over them and sums their number and return it to you.
Also, if you want a faster result, you could use estimatedDocumentsCount (docs) which would go straight to collection's metadata. This results in loss of the ability to ask "What number of documents can I expect if I trigger this query?". If you need to find a count of docs for a query in a faster way, then you could use countDocuments (docs) which is a wrapper around an aggregate query. From my knowledge of Mongo, the provided query looks like a fastest way to count query results without calling count. I guess that this should be preferred way regarding performances for counting the docs from now on (since it's introduced in version 4.0.3).
Mongodb distinct command returns the list of distinct keys in a given document.
Is there any way to retrieve the whole document rather than only keys using mongodb java driver or morphia?
In the documentation it says:
When possible, the distinct command will use an index to find the documents in the query as well as to return the data.
How does this work, when I try using java driver it always returns the list of keys to my queries. And it doesn't take limit and order queries into consideration. Is it possible to give a order by and limit queries into distinct query?
Thanks for the feedback.