Warm up
Tasks
- Please create a new collection that:
- Will use already created a database
- Add the following document to the collection:
{ "lastName":"Cartwright", "parents":[ { "firstName":"Elvira", "role":"mother", "age":64 }, { "firstName":"Randolph", "role":"father", "age":67 } ], "children":[ { "grade":5, "pets":[], "firstName":"Dana", "age":15, "gender":"female" }, { "grade":7, "pets":[ { "name":"Concepcion", "type":"guinea pig" }, { "name":"Haleigh", "type":"hamster" } ], "name":"Pat", "age":13, "gender":"male" } ], "address":{ "state":"North Dakota", "city":"West Bretthaven", "country":"Guinea" } }
- Familiarise with presented queries:
SELECT * FROM c SELECT * FROM family_1 SELECT f.address FROM family_1 as f SELECT f.address.city, f.address.state FROM family_1 as f SELECT * FROM c WHERE c.address.city = 'West Bretthaven' SELECT * FROM c.children SELECT * FROM c.children WHERE c.address.city = 'West Bretthaven' SELECT c.children FROM c WHERE c.address.city = 'West Bretthaven' SELECT * FROM ch in c.children SELECT ch.firstName ?? ch.name as firstName, ch.grade, ARRAY_LENGTH(ch.pets) ?? 0 AS numberOfPets, ch.pets FROM ch in c.children SELECT { "firstName" : ch.firstName ?? ch.name , "grade": ch.grade, "numberOfPets": ARRAY_LENGTH(ch.pets) ?? 0, "pets": ch.pets } AS child FROM ch in c.children SELECT [ ch.firstName ?? ch.name, ch.grade, ARRAY_LENGTH(ch.pets) ?? 0, ch.pets ] AS Child FROM ch in c.children SELECT VALUE [ ch.firstName ?? ch.name, ch.grade, ARRAY_LENGTH(ch.pets) ?? 0, ch.pets ] FROM ch in c.children SELECT ch.name ?? ch.firstName AS childName, f.parents, ARRAY_LENGTH(ch.pets) ?? 0 AS numberOfPets, ch.pets FROM c AS f JOIN ch IN f.children SELECT ch.name ?? ch.firstName AS childName, f.parents, ARRAY_LENGTH(ch.pets) ?? 0 AS numberOfPets, p.name AS petName, p.type FROM c AS f JOIN ch IN f.children JOIN p IN ch.pets SELECT p.name FROM c AS f JOIN ch IN f.children JOIN p IN ch.pets SELECT VALUE p.name FROM c AS f JOIN ch IN f.children JOIN p IN ch.pets
Main part
Preparation
- Please create a new collection that:
- Will use already created a database
- Partition key will be set to /lastName
- Please import a list of families by migration tool:
- Add json file to import and select to decompress data
- Remember to add the name of Database at the end of the connection string. It should look like:
AccountEndpoint=<CosmosDBEndpoint>;AccountKey=<CosmosDBKey>;Database=<DatabaseName>;
- To increase the speed of import, you can increase in Advanced Options in section Target Information
- Collection Throughput – 10 000
- Number of parallel requests – 1000
NOTE: After import, please reduce Throughput to a minimal level – 400 (open your collection and select Scale & Settings)
Downloads
Queries
Try to prepare the following queries and monitor the cost. Please remember that the minimum value of throughput is 400 RU/s:
- Select all elements from the database
- Provide names of families stored in the database
- Count number of families stored in the database
- Get all different names from the database
- Get all last names and id
- Get all last names as a flat string array
- Get all different last names as a flat string array
- Find families that the last name starts at A or B or C or D
- Can you do it in different ways? Which one is the cheapest?
- Flatten fields id, last name, city and country across all documents
- Get all cities and states from database
- Find all families that live in the UK or USA.
- Then for each family, please provide a family name, number of kids, country and name of the currency used in that country
- Provide a list of children. It should contain the following information:
- Full name of the child
- Mother full name
- Father full name
- Grade
- Gender
- Provide a list of families sorted by the age of the first parent
- Could you please try to find the most sophisticated query on those data?
- Check how those queries cost when you are searching within one partition (add the last name into where)
Other
Check how metrics connected to your Cosmos DB service looks like.