C2. Data Models and Query Languages - Part 2
Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems
Hello 👋 ,
Welcome to a new post! In the previous post (Part-I) we covered definition of data model and comparison between different models. Phew! That was a long post. In this part we will take a look at query languages used to obtain data from these models.
Oh! I have started timing all my revision sessions. Currently, average time to revise a post stands at ~4 mins. That means, with these notes I revised (recalled) an entire chapter+ (42 pages) in just 16 mins! This time will further go down as the number of repetitions increase.
By revision I mean recalls - look at the question and try to answer it from your understanding. This is important! We are not trying to cram at all. Just trying to remember concepts which we have already understood.
Let the others know about our journey, share! 😁
Query Languages for Data
What is an imperative language?
A language where you need tell step by step instructions to perform an operation is generally called as imperative language
For example, C, C++, Java etc.
What is a declarative language?
A language where you tell what you need but don't actually specify steps to obtain that result is called as declarative languages
For example SQL, Haskell etc.
What does an imperative code looks like?
What does a declarative code looks like?
What are some differences between declarative and imperative programming languages?
Declarative programming languages are more abstract which allows underlying compiler or engine to apply the optimizations (parallelization) at runtime.
Also, abstraction allows the compiler/interpreter/engine to change itself without impacting user.
Declarative languages are less flexible compared to imperative languages.
Declarative Queries on the Web
What is an example of declarative language on the web (not just databases)?
Cascading Style Sheet - CSS. For applying a property to a class (an attribute which identifies a set of DOM elements) you would just write something like below,
In JavaScript (JS), the equivalent code would be,
You can see how expressive css code is compared to JS code. There are other problems with JS code.
You will need to write additional JS code to reflect the removal of "selected" class. In case of CSS, the browser will automatically update the UI.
If better performing APIs are now available then you need update your code to rip those benefits. In case of CSS, browser vendors can take care of that in background and your same code works with more efficiency now.
MapReduce Querying
What is MapReduce Querying model?
It is a programming model for processing large amounts of data in bulk across many machines, popularized by Google.
Map function takes input and transforms it into (key, value) pair(s). The framework will basically aggregated all such pairs with same key and give to reducer as input the reducer then processes this set and produces a single output. You can compose together many such maps and reducers.
The map reduce functions must be pure, it means, they only use the data passed to them. You can't make additional service/database calls.
How is MapReduce querying model used in databases?
Some databases such as MongoDB allows integrating map and reduce functions while querying. Look at the example below for counting student attendance from logs,
MapReduce is low-level programming model for distributed execution on a cluster of machines. SQL (Higher-level) can be implemented as a pipeline of MapReduce operations.
One problem of this model is that, coming up with map and reduce functions which works so well together is difficult for all cases.
There is declarative programming language MongoDB supported called aggregation pipeline which is a lot similar to SQL. Reinventing the SQL?
That’s it for today. In the next and last part of this chapter we will cover Graph-Like Data Models and their query languages. See you soon! 😇