Part 1 of 10
Data models and query languages
A data model is the shape you give your data, and it may be the most important choice in building software. It shapes the code you write and how you think about the problem. Software stacks models in layers:
- You model the real world as objects, data structures, and APIs in your app.
- You store those in a general-purpose model: JSON or XML documents, relational tables, or graph vertices and edges.
- Database engineers turn those into bytes in memory, on disk, and on the network (storage engines, chapter 4).
- Hardware turns bytes into electrical currents, light pulses, or magnetic fields.
Each layer hides the mess below it, so a database vendor and app teams can work together. No model fits every job: a query that is easy in one model can be awkward in another. This chapter compares relational, document, graph, event sourcing, and DataFrame models, plus the languages used to query them.
Declarative and imperative queries
- In a declarative language (SQL, Cypher, SPARQL, Datalog), you describe the result: which records, sorted how, grouped and totaled how.
- The query optimizer decides how to get it: which indexes, which join method, which order of steps.
- In imperative code (a loop in Python or Java), you write the algorithm yourself.
Benefits of declarative queries:
- Shorter and easier to write.
- The database can get faster without you changing queries.
- The engine can spread work over many cores or machines, which is hard by hand.