0
votes

I am wondering if there is any way to query using the AltLink and not the SelfLink?

SELECT * FROM c
--WHERE c._alt = 'dbs/DefaultDb/colls/DefaultColl/docs/cd9d67d5-b82e-4ec5-aad3-91e784906f6e'
WHERE c._self = 'dbs/Rr4MAA==/colls/Rr4MAOW90GI=/docs/Rr4MAOW90GIBAAAAAAAAAA==/'

UPDATE:

The reason im searching for a way to use the AltLink in SQL is due to the issue of a simple query for a document by its id will continusouly grow in cost as the collection grows. Where as the .net SDK allows the use of the AltLink to get the document and the cost is almost always between 1 to 1.5 RU.

RUCost.png

1
Just curious why you would need to do such a query. What problem are you trying to solve? - David Makogon
If you can run a query against a collection then you need nothing else other than the id to find a document so the idea of the alt link doesn't really make sense, but I might be missing something - Nick Chapsas
David Makogon, the issue is that my collection has 10,000 documents. If I query by the documents by there id the RU cost is 277. If I query by the SelfLink the cost 1.2 RU. If I query by the document id and the document type (its an enum) the RU cost is 2.4. Using the SelfLink and AltLink is supposedly the fastest and cheapest way to retrieve a document. - Capt. Quint
@user6196566 - try doing a read by document id (queries will always cost more than reads, when you know the id). This (a read) is the cheapest (RU-wise) to read a document. Not a query. - David Makogon
Nick Chapsas, good to hear from you! I've read many of your posts on stackoverflow over the last few months as I have been prototyping in cosmos db. My problem Nick is that the .net SDK has a method called ReadDocumentAsync. This method returns back a single document by its Id. I cannot get the cosmos emulator to return back the same record as efficiently. Using the SelfLink seems fastest, but to use the self link I must know the RId uim looking for. Currently I only store the parents id on child documents, not the RId of the parent. - Capt. Quint

1 Answers

1
votes

There are a few things that I need to make clear for you before you can understand why what you are asking for doesn't make sense.

Direct reads using self/alt link and partition key value will ALWAYS be more efficient than queries with self/alt link and partition key value. It's just the way Cosmos works.

The reason why what you're asking for isn't even possible in the first place is because the id value is not unique within a collection. You can have unlimited items with the same id as long as they are in a different logical partition (which means that you id value is only unique within it's own logical partition).

This means that in such a scenario, your AltLink for every single document in a collection will be exactly the same. How would the server know which document you truly want? This is also true about the SelfLink which is unique per document because it uses resource ids. However the server doesn't have the capability to know where this resource id lives unless you point it to a logical partition.

The way you should do what you are asking for is to use the Read methods of the SDKs or the Read method of the REST API, alongside the partition key value that you're looking for.