Skip to content

Latest commit

 

History

History
596 lines (432 loc) · 9.39 KB

slides.md

File metadata and controls

596 lines (432 loc) · 9.39 KB
license theme layout background highlighter lineNumbers info drawings css title
CC0 1.0
default
cover
shiki
false
Presentation on Datomic and Datalog for the DevCord Discord server
persist
unocss
Kein SQL Mehr

Kein SQL Mehr

Eine Einführung in Datomic-Style-Datenbanken


<style> li { @apply text-2xl; } </style>

Inhalt

  1. Was wir von Datenbanken wollen
  2. Die aktuelle Datenbanklandschaft
  3. Die Fakten-Datenbank
  4. Datalog als Query-Sprache
  5. Weiterführend

Was wir von Datenbanken wollen

<style> li { @apply text-2xl; } </style>
  • Effiziente Datenspeicherung
  • Möglichst der echten Welt treue Modellierung der Daten (Kapazitätserhaltung)
  • Einfache Änderung der Modelle (Flexibilität)
  • Übersichtliche APIs
  • Gute Integration in unseren Anwendungen
  • Konsistenz

layout: statement

Klingt sinnvoll, oder?


Die aktuelle Datenbanklandschaft

Wie schlägt sich SQL?

<style> li { @apply text-2xl; } h2 { text-align: left; } </style>

  • Effizienz
  • Kapazitätserhaltung
  • Flexibilität
  • Übersichtlichkeit
  • Gute Integration in unseren Anwendungen
  • Konsistenz


Die aktuelle Datenbanklandschaft

<style> li { @apply text-2xl; } h2 { text-align: left; } </style>

Und was ist mit populären NoSQL-Datenbanken?

  • MongoDB? Angenehme APIs und Integration, aber limitierte Modellierung
  • Apache Cassandra, Neo4J? Kompletter Overkill für die meisten Anwendungen, kompliziert
  • Key-Value-Datenbanken sind offensichtlich nicht immer ausreichend...
  • Konsistenz bei fast allen:


layout: full

Die Fakten-Datenbank

<style> .shiki { margin: auto; margin-top: 10em; transform: scale(2); } .label { position: absolute; top: 12em; @apply text-3xl; } .label-1 { left: 7.5em; @apply text-red-600; } .label-2 { left: 13.5em; @apply text-green-600; } .label-3 { left: 22em; @apply text-blue-600; } </style>
[1234 :person/name "Jane Doe"]
Entity Attribute Value
<style> img { max-height: 90%; width: auto; margin: auto; } </style>

Die Fakten-Datenbank


Die Fakten-Datenbank

<style> li { @apply text-xl; } code { @apply text-xl; } </style>
  • Die Datenbank ist eine Sammlung von Fakten über Entitäten
  • Wir können an einem bestimmten Zeitpunkt
    den unveränderlichen Wert der Datenbank auslesen
  • Fakten können hinzugefügt oder widerrufen werden (Transaktionen)
  • Informationen über Transaktionen sind selbst Fakten...
  • ...Wodurch die Analyse "vergangener" Werte der Datenbank möglich ist (Zeitreise!)
var conn = connectToDatabase(config);
Datahike.transact(conn, setJaneDoesAgeTo25Tx);
var db1 = Datahike.dConn(conn); // aktuellen Datenbank-Wert holen
var age1 = Datahike.q(findJaneDoesAgeQuery, db1); // 25
Datahike.transact(conn, setJaneDoesAgeTo99Tx);
var age2 = Datahike.q(findJaneDoesAgeQuery, db1); // immer noch 25

Die Fakten-Datenbank

Wie füge ich einen Fakt hinzu?

[:db/add 1234 :person/name "Jane Doe"]

Wie widerrufe ich einen Fakt?

[:db/retract 1234 :person/name "Jane Doe"]

Wie erstelle ich eine neue Entität?

[[:db/add "temp-id" :person/name "Jane Doe"]
 [:db/add "temp-id" :person/email "[email protected]"]
 [:db/add "temp-id" :person/birthday #inst "1997-10-08"]]

Alternative Notation für mehrere Fakten über dieselbe Entität:

{:db/id ...
 :person/name "Jane Doe"
 :person/email "[email protected]"
 :person/birthday #inst "1997-10-08"}

layout: center

<style> h2 { text-align: center; } </style>

Im Folgenden stellen wir uns eine Datenbank vor, die Informationen über Filme und Schauspieler speichert.


Datalog als Query-Sprache

<style> .shiki code { @apply text-3xl; } .explanation { margin-top: 4em; } .label { position: absolute; top: 195px; @apply text-3xl; } .label-1 { left: 235px; @apply text-red-600; } .label-2 { left: 370px; @apply text-green-600; } .label-3 { left: 525px; @apply text-blue-600; } </style>
[:find ?e
 :where [?e :movie/year 1987]]

  • Finde alle Entity-IDs ?e, die das Attribut :movie/year mit dem Wert 1987 haben.
  • Finde alle Filme aus dem Jahr 1987.

E

A

V


Datalog als Query-Sprache

<style> code { @apply text-2xl; } </style>
;; Datensatz
[123 :movie/year 1985]
[123 :movie/title "Back To The Future"]
[123 :movie/cast 234]
[234 :person/name "Michael J. Fox"]

;; Query
[:find ?title
 :where [_ :movie/title ?title]]

Datalog als Query-Sprache

<style> code { @apply text-2xl; } </style>
;; Datensatz
[123 :movie/year 1985]
[123 :movie/title "Back To The Future"]
[123 :movie/cast 234]
[234 :person/name "Michael J. Fox"]

;; Query
[:find ?title
 :where [?e :movie/year 1985]
        [?e :movie/title ?title]]

Datalog als Query-Sprache

<style> code { @apply text-2xl; } .bracket { position: absolute; top: 390px; left: 535px; @apply text-6xl; } .label { position: absolute; top: 405px; left: 565px; @apply text-3xl; } </style>
;; Datensatz
[123 :movie/year 1985]
[123 :movie/title "Back To The Future"]
[123 :movie/cast 234]
[234 :person/name "Michael J. Fox"]

;; Query
[:find ?name
 :where [?m :movie/title "Back To The Future"]
        [?m :movie/cast ?p]
        [?p :person/name ?name]]

} Impliziter Join!


Datalog als Query-Sprache

<style> code { @apply text-2xl; } </style>
;; Datensatz
[123 :movie/year 1985]
[123 :movie/title "Back To The Future"]
[123 :movie/cast 234]
[234 :person/name "Michael J. Fox"]

;; Query
[:find ?name
 :in $ ?title
 :where [?m :movie/title ?title]
        [?m :movie/cast ?p]
        [?p :person/name ?name]]

Datalog als Query-Sprache

<style> h2 { text-align: left; } li { @apply text-2xl; } </style>

Außerdem...

  • Datenbank ist Eingabeparameter
    => Query über mehrere Datenbanken möglich
  • Aggregatfunktionen: count, sum, avg, ...
  • Ausdrucksklauseln: <, >, ...
  • Logische Klauseln: or, and, not
  • Aufrufen von eigenen Funktionen/Methoden innerhalb einer Query
  • Pull - Schneller Weg, um Entity-Infos zu kriegen
  • Rules - Pattern Matching auf Steroiden


Datalog als Query-Sprache

<style> code { @apply text-xl; } </style>
;; Datensatz
[123 :movie/year 1985]
[123 :movie/title "Back To The Future"]
[123 :movie/cast 234]
[234 :person/name "Michael J. Fox"]

;; Query
[:find (pull ?m [* {:movie/cast [*]}])
 :where [?m :movie/title "Back To The Future"]]
 
;; Ergebnis
[{:title "Back To The Future"
  :year 1985
  :cast [{:name "Michael J. Fox"}, ...]
  ...}]

Datalog als Query-Sprache

<style> code { @apply text-xl; } </style>
;; Query
[:find (count ?outer) .
  :in $ % ?search
  :where
  [?inner :bag/name ?search]
  [?c :bag.child/bag ?inner]
  (child ?outer ?c)]

;; Rules
[[(child ?outer ?c)
   [?outer :bag/children ?c]]
 [(child ?outer ?c)
   [?outer :bag/children ?nc]
   [?nc :bag.child/bag ?next]
   (child ?next ?c)]]

Weiterführend


Danke für eure Aufmerksamkeit

Gibt es Fragen?


(Slides zu finden auf devcord.club/datomic-intro)