Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
How Modern SQL Databases
...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
TL;DR:
SQL = Awesome
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why are you here?
Because:
… This cannot ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Me – @lukaseder
SQL is a device whose mys...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
When you leave the room
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why do I talk about SQL?
SQL is the only ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why doesn’t anyone else talk about SQL?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why doesn’t anyone else talk about SQL?
¯...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why doesn’t anyone else talk about SQL?
E...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Vlad shows JPA’s only important feature
H...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Vlad shows JPA’s only important feature
H...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What is SQL?
Who has seen
my other talk?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What is SQL?
I mean this one
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
10 SQL tricks to convince you SQL is awes...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
10 SQL tricks to convince you SQL is awes...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
A SQL trick
-- Query from http://explaine...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
A SQL trick: Generating the Mandelbrot Se...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
But today…
But today, we’ll talk
about mo...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
But today…
But today, we’ll talk
about mo...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
But today…
Is this SQL?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Who can handle this SQL statement here?
S...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy stuff
Perfect
This talk is for you.
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What will I talk about?
• SQL is awesome
...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What will I talk about?
• SQL is awesome
...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
There are two ways of proving this
1. By ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Disclaimer
This is going to be a high lev...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business is asking
What is the daily
reve...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business is asking
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business is asking
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business is asking
What is the daily
reve...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business is asking
What is the daily
reve...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
No problemo
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, right?
SELECT
title, payment_date, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, right?
SELECT
title, payment_date, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, right?
SELECT
title, payment_date, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, right?
SELECT
title, payment_date, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Imperative style
Let’s write this in
«cla...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
“Classic Java”
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s get rolling
class Film {
}
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s get rolling
You still with me?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Obviously, Film is a tuple
class Film {
i...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Because, Java
class Film {
int filmId;
St...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Never forget these, of course
class Film ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Abstractions, ladies and gentlemen!
class...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Abstractions, ladies and gentlemen!
inter...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Just to be sure, super generic!
interface...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Some Spring
interface FilmFactory {
Film ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Some Spring, and a little Lombok
@Functio...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Oh, what the heck
@FunctionalInterface
in...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Oh, what the heck
@FunctionalInterface
in...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Average enterprise bean is visible from s...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Enough of this. More info here
www.annota...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Hmm, relationships?
class Film {
int film...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s assume, we copy relational model 1:...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s assume, we copy relational model 1:...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s assume, we copy relational model 1:...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s assume, we copy relational model 1:...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s assume, we copy relational model 1:...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
If you must @OneToMany
Ask Vlad
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now, calculate. This be the result type
/...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now, calculate. This be the result type
/...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Money, eh?
JSR 354 FTW!
(Java Money API)
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Money, eh?
JSR 354 FTW!
(Java Money API)
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now, calculate. This be the result type
/...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now, calculate. Wait…
// Or this?
Map<Fil...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now, calculate. Or better
// Or maybe thi...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now, calculate. Aagh
// Kewl, wrote some ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Five hours later
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Who in here has felt this way before?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Now let’s calculate, for real
Map<Film, M...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, so far
Map<Film, Map<LocalDate, Big...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, so far
Map<Film, Map<LocalDate, Big...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, so far
Map<Film, Map<LocalDate, Big...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Easy, so far
Map<Film, Map<LocalDate, Big...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Still following?
Map<Film, Map<LocalDate,...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Still following?
Map<Film, Map<LocalDate,...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Still following?
Map<Film, Map<LocalDate,...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Aaah, lazy initialization!
Map<Film, Map<...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Aaah, lazy initialization!
Map<Film, Map<...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s have that Map discussion again
Map<...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, next
Map<Film, Map<LocalDate, BigDeci...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping
Map<Film, Map<LocalDate, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping
Map<Film, Map<LocalDate, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping
Map<Film, Map<LocalDate, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping
Map<Film, Map<LocalDate, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping
Map<Film, Map<LocalDate, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Entity Graph – How JPA folks write SQL
@N...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Entity Graph – How JPA folks write SQL
@N...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Remember this slide?
@FunctionalInterface...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Your code now visible from Andromeda Gala...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping, done. Right?
Map<Film, M...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping, done. Wrong!
Map<Film, M...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
OK, now looping, done. OK, better with Ja...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Feel good about yourself?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business is asking again
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Programmer reactions
SQL Developer Java D...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SQL dev: I knew this was coming!
SELECT
t...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SQL dev: Swoosh!
SELECT
title, payment_da...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SQL dev: Joke’s on you!
SELECT
title, sto...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Java dev: Gaaah, OK
Map<Film, Map<LocalDa...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Java dev: Let’s make some room
Map<Film, ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Java dev: Let’s see, if I tweak this and ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Java dev: Let’s see, if I tweak this and ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Java dev: Let’s see, if I tweak this and ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Map<Film, Map<Integer, Map<LocalDate, Big...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Meanwhile…
While you and your
architect e...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Meanwhile… The inevitable
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What on earth is a cumulative sum??
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What on earth is a cumulative sum??
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What on earth is a cumulative sum??
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Java dev: 😵
// Thanks God it’s Friday!
th...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Meanwhile, the SQL developer…
SELECT
titl...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Meanwhile, the SQL developer…
SELECT
titl...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Meanwhile, the SQL developer…
SELECT
titl...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What does it do?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
Do you
get the
message?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
SQL is a 4GL
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
In SQL, we only
declare the r...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
The database will
figure out ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
Algorithms are
boring (mostly)
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
We don’t care about
• Writing...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
3GL vs. 4GL
We do care about
• Business l...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Is Java 8 Streams “declarative” ?
The Str...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Is Java 8 Streams “declarative” ?
The Str...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
In a way, it’s very similar
TABLE : Strea...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Well,...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
We can do a bit better, though
Better Str...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
We can do a bit better, though
Seq.seq(pe...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<F...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<T...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Let’s try that Java algorithm again
Map<T...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Bad news is:
How much time did
we spend o...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Remember this slide? Was it all that hard...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Everyone understands now…
SQL is a 4GL
Th...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Everyone understands now…
How much time d...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What will I talk about?
• SQL is awesome
...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Suspense!
Best chapter:
Performance
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
But first
How does SQL
work?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Business asking
How many films did each
a...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
They want this
Starts with «A»
How many f...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
This is the query. But what really happen...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
O...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
2...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
2...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
2...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
2...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Logical Operations order
1. Load tables
2...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Complete order
Logical operations order
1...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
But that’s n...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
Can we see t...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
It’s called «execution plan»
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Who has seen an execution plan?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SQL Developer
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
pgAdmin III
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SQL Workbench
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
SQL Server Management Studio
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
How many row...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
How much tim...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Imperative
Actual operations order
1. Acc...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
Imperative
A...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
Imperative
A...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
Imperative
A...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
Imperative
A...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What’s the actual algorithm?
Imperative
A...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Huh!
Declarative vs
Imperative
Would you ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
E.g. Apparently, it’s faster in this
case...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Big O Notation
Hash join: O(N)
(N = film_...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Hash join: O(N)
(N = film_actors (bigger ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Well...
It depends!
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What you think you have
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What you may really have
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What you may really have
«Works on my mac...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
More info
bigocheatsheet.com
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Here’s an interesting
thing to...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Histogram
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Histogram
-- Predicate 1
WHERE...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Histogram
-- Predicate 1
WHERE...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Histogram
-- Not very selectiv...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Histogram
-- Not very selectiv...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
Accessing the index is only wo...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
The database knows all these t...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Statistics
The database knows all these t...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Expression trees
Everything has a cost
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Expression trees
How does SQL work?
1. A ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
How does SQL work?
1. A parsed SQL string...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Expression trees
How does SQL work?
1. A ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
How does SQL work?
1. A parsed SQL string...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
How does SQL work?
1. A parsed SQL string...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
How does SQL work?
1. A parsed SQL string...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
How does SQL work?
1. A parsed SQL string...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Expression trees
How does SQL work?
1. A ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Expression trees
How does SQL work?
1. A ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Expression trees
Special treat:
Oracle 12...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
What will I talk about?
• SQL is awesome
...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why do I talk about SQL?
Now that you kno...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
This?
Map<Film, Map<Integer, Map<LocalDat...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Or, this?
SELECT
title, store_id, payment...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why do I talk about SQL?
SQL is the only ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Why doesn’t anyone else talk about SQL?
¯...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Can I write SQL in Java?
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Can I write SQL in Java? – Yes. With jOOQ
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Can I write SQL in Java? – Yes. With jOOQ
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Can I write SQL in Java? – Yes. With jOOQ
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
So what’s the key takeaway?
1. Can you do...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
If you haven’t had enough
Google «10 SQL ...
Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
Thank you
Check out our trainings:
http:/...
Nächste SlideShare
Wird geladen in …5
×

von

How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 1 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 2 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 3 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 4 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 5 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 6 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 7 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 8 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 9 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 10 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 11 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 12 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 13 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 14 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 15 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 16 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 17 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 18 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 19 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 20 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 21 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 22 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 23 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 24 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 25 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 26 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 27 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 28 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 29 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 30 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 31 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 32 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 33 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 34 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 35 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 36 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 37 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 38 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 39 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 40 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 41 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 42 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 43 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 44 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 45 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 46 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 47 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 48 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 49 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 50 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 51 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 52 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 53 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 54 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 55 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 56 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 57 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 58 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 59 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 60 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 61 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 62 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 63 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 64 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 65 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 66 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 67 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 68 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 69 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 70 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 71 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 72 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 73 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 74 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 75 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 76 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 77 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 78 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 79 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 80 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 81 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 82 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 83 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 84 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 85 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 86 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 87 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 88 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 89 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 90 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 91 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 92 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 93 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 94 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 95 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 96 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 97 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 98 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 99 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 100 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 101 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 102 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 103 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 104 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 105 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 106 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 107 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 108 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 109 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 110 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 111 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 112 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 113 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 114 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 115 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 116 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 117 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 118 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 119 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 120 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 121 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 122 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 123 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 124 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 125 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 126 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 127 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 128 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 129 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 130 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 131 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 132 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 133 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 134 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 135 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 136 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 137 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 138 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 139 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 140 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 141 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 142 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 143 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 144 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 145 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 146 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 147 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 148 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 149 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 150 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 151 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 152 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 153 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 154 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 155 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 156 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 157 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 158 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 159 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 160 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 161 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 162 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 163 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 164 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 165 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 166 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 167 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 168 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 169 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 170 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 171 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 172 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 173 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 174 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 175 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 176 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 177 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 178 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 179 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 180 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 181 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 182 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 183 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 184 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 185 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 186 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 187 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 188 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 189 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 190 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 191 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 192 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 193 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 194 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 195 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 196 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 197 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 198 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 199 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 200 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 201 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 202 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 203 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 204 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 205 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 206 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 207 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 208 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 209 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 210 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 211 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 212 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 213 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 214 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 215 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 216 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 217 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 218 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 219 How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of Slide 220
Nächste SlideShare
10 SQL Tricks that You Didn't Think Were Possible
Weiter
Herunterladen, um offline zu lesen und im Vollbildmodus anzuzeigen.

19 Gefällt mir

Teilen

Herunterladen, um offline zu lesen

How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of

Herunterladen, um offline zu lesen

SQL is the only ever successful, mainstream, and general-purpose 4GL (Fourth Generation Programming Language) and it is awesome!

With modern cost based optimisation, relational databases like Oracle, SQL Server, PostgreSQL finally keep up to the promise of a powerful declarative programming model by adapting to ever changing productive data without performance penalties. Thousand-line-long, complex SQL statements can be run in far below a millisecond against billion-row strong tables if database developers know their ways around the SQL language - and the best news is: It's not that hard!

In this talk, I'll show how the SQL database will constantly outperform any hand written data retrieval algorithm - or in other words - how SQL, being a logic language, is the best language for business logic.

Ähnliche Bücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

Ähnliche Hörbücher

Kostenlos mit einer 30-tägigen Testversion von Scribd

Alle anzeigen

How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of

  1. 1. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! How Modern SQL Databases Come up with Algorithms that You Would Have Never Dreamed Of
  2. 2. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! TL;DR: SQL = Awesome
  3. 3. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why are you here? Because: … This cannot be true … This must not be true … Is it true, though? … OMG imma rewrite all code to SQL
  4. 4. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Me – @lukaseder SQL is a device whose mystery is only exceeded by its power! - Founder and CEO at Data Geekery - Oracle Java Champion - JUG.ch Board Member
  5. 5. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 When you leave the room
  6. 6. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why do I talk about SQL? SQL is the only ever successful, mainstream, and general- purpose 4GL (Fourth- Generation Programming Language) And it is awesome!
  7. 7. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why doesn’t anyone else talk about SQL?
  8. 8. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why doesn’t anyone else talk about SQL? ¯_(シ)_/¯
  9. 9. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why doesn’t anyone else talk about SQL? Except Vlad
  10. 10. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Vlad shows JPA’s only important feature His blog: http://tiny.cc/jpa-only-useful-feature Vlad: Hibernate Developer Advocate
  11. 11. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Vlad shows JPA’s only important feature His blog: http://tiny.cc/jpa-only-useful-feature Vlad: Hibernate Developer Advocate
  12. 12. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What is SQL? Who has seen my other talk?
  13. 13. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What is SQL? I mean this one
  14. 14. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 10 SQL tricks to convince you SQL is awesome
  15. 15. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 10 SQL tricks to convince you SQL is awesome Not that hard to find
  16. 16. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 A SQL trick -- Query from http://explainextended.com/2013/12/31/happy-new-year-5/ WITH RECURSIVE q(r, i, rx, ix, g) AS ( SELECT r::DOUBLE PRECISION * 0.02, i::DOUBLE PRECISION * 0.02, .0::DOUBLE PRECISION , .0::DOUBLE PRECISION, 0 FROM generate_series(-60, 20) r, generate_series(-50, 50) i UNION ALL SELECT r, i, CASE WHEN abs(rx * rx + ix * ix) <= 2 THEN rx * rx - ix * ix END + r, CASE WHEN abs(rx * rx + ix * ix) <= 2 THEN 2 * rx * ix END + i, g + 1 FROM q WHERE rx IS NOT NULL AND g < 99 ) SELECT array_to_string(array_agg(s ORDER BY r), '') FROM ( SELECT i, r, substring(' .:-=+*#%@', max(g) / 10 + 1, 1) s FROM q GROUP BY i, r ) q GROUP BY i ORDER BY i
  17. 17. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 A SQL trick: Generating the Mandelbrot Set -- Query from http://explainextended.com/2013/12/31/happy-new-year-5/ WITH RECURSIVE q(r, i, rx, ix, g) AS ( SELECT r::DOUBLE PRECISION * 0.02, i::DOUBLE PRECISION * 0.02, .0::DOUBLE PRECISION , .0::DOUBLE PRECISION, 0 FROM generate_series(-60, 20) r, generate_series(-50, 50) i UNION ALL SELECT r, i, CASE WHEN abs(rx * rx + ix * ix) <= 2 THEN rx * rx - ix * ix END + r, CASE WHEN abs(rx * rx + ix * ix) <= 2 THEN 2 * rx * ix END + i, g + 1 FROM q WHERE rx IS NOT NULL AND g < 99 ) SELECT array_to_string(array_agg(s ORDER BY r), '') FROM ( SELECT i, r, substring(' .:-=+*#%@', max(g) / 10 + 1, 1) s FROM q GROUP BY i, r ) q GROUP BY i ORDER BY i
  18. 18. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 But today… But today, we’ll talk about more basic SQL
  19. 19. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 But today… But today, we’ll talk about more basic SQL (still awesome)
  20. 20. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 But today… Is this SQL?
  21. 21. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Who can handle this SQL statement here? SELECT * FROM person WHERE id = 42
  22. 22. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy stuff Perfect This talk is for you.
  23. 23. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What will I talk about? • SQL is awesome • SQL is productive • SQL is fast
  24. 24. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What will I talk about? • SQL is awesome • SQL is productive • SQL is fast
  25. 25. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 There are two ways of proving this 1. By example 2. By alternative (Java)
  26. 26. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Disclaimer This is going to be a high level, conceptual story about SQL We’re not going into deeper levels.
  27. 27. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business is asking What is the daily revenue of each store
  28. 28. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business is asking
  29. 29. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business is asking
  30. 30. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business is asking What is the daily revenue of each store
  31. 31. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business is asking What is the daily revenue of each store Film Money Day JOIN
  32. 32. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 No problemo
  33. 33. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, right? SELECT title, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, payment_date ORDER BY title, payment_date
  34. 34. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, right? SELECT title, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, payment_date ORDER BY title, payment_date Money per film
  35. 35. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, right? SELECT title, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, payment_date ORDER BY title, payment_date Relationship film ↔ money
  36. 36. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, right? SELECT title, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, payment_date ORDER BY title, payment_date Aggregation per film / date
  37. 37. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Imperative style Let’s write this in «classic» Java
  38. 38. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 “Classic Java”
  39. 39. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s get rolling class Film { }
  40. 40. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s get rolling You still with me?
  41. 41. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Obviously, Film is a tuple class Film { int filmId; String title; }
  42. 42. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Because, Java class Film { int filmId; String title; int getFilmId() { return filmId; } ... }
  43. 43. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Never forget these, of course class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... }
  44. 44. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Abstractions, ladies and gentlemen! class FilmFactory { Film newFilm() { ... } } class FilmFactoryBuilder { FilmFactory newFilmFactory() { ... } }
  45. 45. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Abstractions, ladies and gentlemen! interface FilmFactory { Film newFilm(); } interface FilmFactoryBuilder { FilmFactory newFilmFactory(); }
  46. 46. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Just to be sure, super generic! interface FilmFactory { Film newFilm(); } interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } class FilmFactoryImpl { @Override public Film newFilm() { ... } } class FilmFactoryBuilderImpl { @Override public FilmFactory newFilmFactory() { ... } }
  47. 47. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Some Spring interface FilmFactory { Film newFilm(); } interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean class FilmFactoryImpl { @Override public Film newFilm() { ... } } @Bean class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  48. 48. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Some Spring, and a little Lombok @FunctionalInterface interface FilmFactory { Film newFilm(); } @FunctionalInterface interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean @EnableAspectJAutoProxy @EnableAutoConfiguration class FilmFactoryImpl { @Override public Film newFilm() { ... } } @Bean @Configuration @NoArgsConstructor class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  49. 49. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Oh, what the heck @FunctionalInterface interface FilmFactory { Film newFilm(); } @FunctionalInterface interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean @EnableAspectJAutoProxy @EnableAutoConfiguration class FilmFactoryImpl { @Override public Film newFilm() { ... } } @Bean @Configuration @NoArgsConstructor @Cloneable @SneakyThrows @ResultSetMapping @ConcurrentInitialiserProxyFactory @SpringPrefetchAdapter @AdapterProxyBeanMethod @AdapterBeanProxyMethod class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  50. 50. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Oh, what the heck @FunctionalInterface interface FilmFactory { Film newFilm(); } @FunctionalInterface interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean @EnableAspectJAutoProxy @EnableAutoConfiguration class FilmFactoryImpl { @Override @ThisMethodDidntHaveAnnotationsYet @OKWeStillHaveSomeSpaceLeft @LetsSeeIfWeCanReachThePrintMargin public Film newFilm() { ... } } @Bean @Configuration @NoArgsConstructor @Cloneable @SneakyThrows @ResultSetMapping @ConcurrentInitialiserProxyFactory @SpringPrefetchAdapter @AdapterProxyBeanMethod @AdapterBeanProxyMethod @MoreAndMoreAnnotations @CanYouEvenReadThis @IsThereStillAnyRealLogicLeft @ThisJokeNeverGetsLame @Annotatiomania @WhoWillBeNextYearsAnnotatiomaniac class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  51. 51. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Average enterprise bean is visible from space @FunctionalInterface interface FilmFactory { Film newFilm(); } @FunctionalInterface @NoArgsConstructor @Cloneable @SneakyThrows interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean @EnableAspectJAutoProxy @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @EnableAutoConfiguration @NoArgsConstructor @Cloneable @SneakyThrows class FilmFactoryImpl { @Override @NoArgsConstructor @Cloneable @SneakyThrows @ThisMethodDidntHaveAnnotationsYet @OKWeStillHaveSomeSpaceLeft @LetsSeeIfWeCanReachThePrintMargin public Film newFilm() { ... } } @Bean @Configuration @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @ResultSetMapping @ConcurrentInitialiserProxyFactory @SpringPrefetchAdapter @AdapterProxyBeanMethod @AdapterBeanProxyMethod @NoArgsConstructor @Cloneable @SneakyThrows @MoreAndMoreAnnotations @CanYouEvenReadThis @NoArgsConstructor @Cloneable @SneakyThrows @IsThereStillAnyRealLogicLeft @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @ThisJokeNeverGetsLame @Annotatiomania @WhoWillBeNextYearsAnnotatiomaniac class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  52. 52. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Enough of this. More info here www.annotatiomania.com
  53. 53. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Hmm, relationships? class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } // Hmm... Inventory? Store? Rental? Payment? int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... }
  54. 54. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s assume, we copy relational model 1:1 class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } List<Inventory> getInventories() { ... } int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... }
  55. 55. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s assume, we copy relational model 1:1 class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } @OneToMany(mappedBy = "film") List<Inventory> getInventories() { ... } int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... }
  56. 56. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s assume, we copy relational model 1:1 class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } @OneToMany(mappedBy = "film") List<Inventory> getInventories() { ... } int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... } Anyone seen these lovely things?
  57. 57. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s assume, we copy relational model 1:1 class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } @OneToMany(mappedBy = "film", fetch = FetchType.EAGER) List<Inventory> getInventories() { ... } int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... } How about this? Care discuss this?
  58. 58. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s assume, we copy relational model 1:1 class Film { int filmId; String title; int getFilmId() { ... } void setFilmId(int filmId) { ... } String getTitle() { ... } void setTitle(String title) { ... } @OneToMany(mappedBy = "film", fetch = FetchType.LAZY) List<Inventory> getInventories() { ... } int hashCode() { ... } boolean equals(Object o) { ... } String toString() { ... } ... } How about this? Care discuss this?
  59. 59. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 If you must @OneToMany Ask Vlad
  60. 60. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now, calculate. This be the result type // This is the optimal result type Map<Film, Map<LocalDate, BigDecimal>> result = new HashMap<>();
  61. 61. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now, calculate. This be the result type // This is the optimal result type Map<Film, Map<LocalDate, BigDecimal>> result = new HashMap<>(); Money Per film Per date
  62. 62. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Money, eh? JSR 354 FTW! (Java Money API)
  63. 63. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Money, eh? JSR 354 FTW! (Java Money API)
  64. 64. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now, calculate. This be the result type // This is the optimal result type Map<Film, Map<LocalDate, BigDecimal>> result = new HashMap<>();
  65. 65. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now, calculate. Wait… // Or this? Map<Film, Map<LocalDate, BigDecimal>> result = new LinkedHashMap<>();
  66. 66. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now, calculate. Or better // Or maybe this? SortedMap<Film, SortedMap<LocalDate, BigDecimal>> result = new TreeMap<>();
  67. 67. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now, calculate. Aagh // Kewl, wrote some library MultiKeyMap< Film, LocalDate, BigDecimal> result = new MultiKeyMap<>();
  68. 68. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Five hours later
  69. 69. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Who in here has felt this way before?
  70. 70. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Now let’s calculate, for real Map<Film, Map<LocalDate, BigDecimal>> result = ...;
  71. 71. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, so far Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO
  72. 72. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, so far Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO // FIXME 2002-04-01 Will optimize this later
  73. 73. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, so far Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO // FIXME 2003-04-01 Will optimize this “later”
  74. 74. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Easy, so far Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO // FIXME 2003-04-01 Will optimize this “later” // FIXME 2005-04-01 URGENT! This is really slow!
  75. 75. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Still following? Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) ; // TODO
  76. 76. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Still following? Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); // TODO }
  77. 77. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Still following? Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); // TODO }
  78. 78. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Aaah, lazy initialization! Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { daily = new HashMap<>(); result.put(film, daily); } // TODO }
  79. 79. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Aaah, lazy initialization! Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { daily = new HashMap<>(); result.put(film, daily); } // TODO }
  80. 80. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s have that Map discussion again Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { daily = new LinkedHashMap<>(); // TODO: Better Map! result.put(film, daily); } // TODO }
  81. 81. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, next Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } // TODO }
  82. 82. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) ; // TODO }
  83. 83. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) ; // TODO }
  84. 84. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment payment : rental.getPayments()) ; // TODO }
  85. 85. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment payment : rental.getPayments()) ; // TODO } EAGER or LAZY?
  86. 86. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment payment : rental.getPayments()) ; // TODO } Or, entity graph?
  87. 87. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Entity Graph – How JPA folks write SQL @NamedEntityGraph( name = "graph.film.inventories.rentals.payments", attributeNodes = @NamedAttributeNode( value = "inventories", subgraph = "inventories" ), subgraphs = { @NamedSubgraph( name = "inventories", attributeNodes = @NamedAttributeNode( value = "rentals", subgraph = "rentals" ) ), ... } )
  88. 88. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Entity Graph – How JPA folks write SQL @NamedEntityGraph( name = "graph.film.inventories.rentals.payments", attributeNodes = @NamedAttributeNode( value = "inventories", subgraph = "inventories" ), subgraphs = { @NamedSubgraph( name = "inventories", attributeNodes = @NamedAttributeNode( value = "rentals", subgraph = "rentals" ) ), ... } )
  89. 89. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Remember this slide? @FunctionalInterface interface FilmFactory { Film newFilm(); } @FunctionalInterface @NoArgsConstructor @Cloneable @SneakyThrows interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean @EnableAspectJAutoProxy @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @EnableAutoConfiguration @NoArgsConstructor @Cloneable @SneakyThrows class FilmFactoryImpl { @Override @NoArgsConstructor @Cloneable @SneakyThrows @ThisMethodDidntHaveAnnotationsYet @OKWeStillHaveSomeSpaceLeft @LetsSeeIfWeCanReachThePrintMargin public Film newFilm() { ... } } @Bean @Configuration @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @ResultSetMapping @ConcurrentInitialiserProxyFactory @SpringPrefetchAdapter @AdapterProxyBeanMethod @AdapterBeanProxyMethod @NoArgsConstructor @Cloneable @SneakyThrows @MoreAndMoreAnnotations @CanYouEvenReadThis @NoArgsConstructor @Cloneable @SneakyThrows @IsThereStillAnyRealLogicLeft @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @ThisJokeNeverGetsLame @Annotatiomania @WhoWillBeNextYearsAnnotatiomaniac class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  90. 90. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Your code now visible from Andromeda Galaxy @FunctionalInterface interface FilmFactory { Film newFilm(); } @FunctionalInterface @NoArgsConstructor @Cloneable @SneakyThrows interface FilmFactoryBuilder { FilmFactory newFilmFactory(); } @Bean @EnableAspectJAutoProxy @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @EnableAutoConfiguration @NoArgsConstructor @Cloneable @SneakyThrows class FilmFactoryImpl { @Override @NoArgsConstructor @Cloneable @SneakyThrows @ThisMethodDidntHaveAnnotationsYet @OKWeStillHaveSomeSpaceLeft @LetsSeeIfWeCanReachThePrintMargin public Film newFilm() { ... } } @Bean @Configuration @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @ResultSetMapping @ConcurrentInitialiserProxyFactory @SpringPrefetchAdapter @AdapterProxyBeanMethod @AdapterBeanProxyMethod @NoArgsConstructor @Cloneable @SneakyThrows @MoreAndMoreAnnotations @CanYouEvenReadThis @NoArgsConstructor @Cloneable @SneakyThrows @IsThereStillAnyRealLogicLeft @NoArgsConstructor @Cloneable @SneakyThrows @NoArgsConstructor @Cloneable @SneakyThrows @ThisJokeNeverGetsLame @Annotatiomania @WhoWillBeNextYearsAnnotatiomaniac class FilmFactoryBuilderImpl { @Override @EnableCaching public FilmFactory newFilmFactory() { ... } }
  91. 91. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping, done. Right? Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment payment : rental.getPayments()) daily.put( payment.getPaymentDate(), payment.getAmount()); }
  92. 92. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping, done. Wrong! Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.get(film); if (daily == null) { ... } for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment payment : rental.getPayments()) { LocalDate date = payment.getPaymentDate(); BigDecimal amount = payment.getAmount(); if (daily.containsKey(date)) daily.put(date, daily.get(date).add(amount)); else daily.put(date, amount); } }
  93. 93. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 OK, now looping, done. OK, better with Java 8 Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment payment : rental.getPayments()) daily.compute( payment.getPaymentDate(), (k, v) -> v == null ? payment.getAmount() : payment.getAmount().add(v)); }
  94. 94. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Feel good about yourself?
  95. 95. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business is asking again
  96. 96. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Programmer reactions SQL Developer Java Developer
  97. 97. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SQL dev: I knew this was coming! SELECT title, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, payment_date ORDER BY title, payment_date
  98. 98. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SQL dev: Swoosh! SELECT title, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, payment_date ORDER BY title, payment_date
  99. 99. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SQL dev: Joke’s on you! SELECT title, store_id, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, store_id, payment_date ORDER BY title, store_id, payment_date
  100. 100. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Java dev: Gaaah, OK Map<Film, Map<LocalDate, BigDecimal>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); }
  101. 101. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Java dev: Let’s make some room Map<Film, Map<LocalDate, BigDecimal> > result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<LocalDate, BigDecimal> daily = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); }
  102. 102. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Java dev: Let’s see, if I tweak this and then… Map<Film, Map<Integer, Map<LocalDate, BigDecimal>>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<Integer, Map<LocalDate, BigDecimal>> dailyPerStore = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) { Map<LocalDate, BigDecimal> daily = dailyPerStore.computeIfAbsent( inventory.getStoreId(), k -> new HashMap()); for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); } }
  103. 103. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Java dev: Let’s see, if I tweak this and then… Map<Film, Map<Integer, Map<LocalDate, BigDecimal>>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<Integer, Map<LocalDate, BigDecimal>> dailyPerStore = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) { Map<LocalDate, BigDecimal> daily = dailyPerStore.computeIfAbsent( inventory.getStoreId(), k -> new HashMap()); for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); } }Architect: (that’s this guy)
  104. 104. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Java dev: Let’s see, if I tweak this and then… Map<Film, Map<Integer, Map<LocalDate, BigDecimal>>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<Integer, Map<LocalDate, BigDecimal>> dailyPerStore = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) { Map<LocalDate, BigDecimal> daily = dailyPerStore.computeIfAbsent( inventory.getStoreId(), k -> new HashMap()); for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); } }Architect: We can’t have such types. Factor out in a new class. And Factory
  105. 105. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Map<Film, Map<Integer, Map<LocalDate, BigDecimal>>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<Integer, Map<LocalDate, BigDecimal>> dailyPerStore = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) { Map<LocalDate, BigDecimal> daily = dailyPerStore.computeIfAbsent( inventory.getStoreId(), k -> new HashMap()); for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); } } And you’re like…
  106. 106. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Meanwhile… While you and your architect engage in nominal vs structural typing philosophies...
  107. 107. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Meanwhile… The inevitable
  108. 108. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What on earth is a cumulative sum??
  109. 109. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What on earth is a cumulative sum??
  110. 110. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What on earth is a cumulative sum??
  111. 111. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Java dev: 😵 // Thanks God it’s Friday! throw new UnsupportedOperationException();
  112. 112. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Meanwhile, the SQL developer… SELECT title, store_id, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, store_id, payment_date ORDER BY title, store_id, payment_date
  113. 113. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Meanwhile, the SQL developer… SELECT title, store_id, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, store_id, payment_date ORDER BY title, store_id, payment_date
  114. 114. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Meanwhile, the SQL developer… SELECT title, store_id, payment_date, SUM(SUM(amount)) OVER ( PARTITION BY title, store_id ORDER BY payment_date ) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, store_id, payment_date ORDER BY title, store_id, payment_dateWindow function
  115. 115. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What does it do?
  116. 116. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL Do you get the message?
  117. 117. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL SQL is a 4GL
  118. 118. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL In SQL, we only declare the result.
  119. 119. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL The database will figure out the algorithm for us.
  120. 120. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL Algorithms are boring (mostly)
  121. 121. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL We don’t care about • Writing algorithms (using them is a different story) • Premature Optimisation • Data type details (HashMap<A, B> or List<Entry<A, B>>?) • Local variables • Caches • Loops • Initialisation (or NullPointerException)
  122. 122. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 3GL vs. 4GL We do care about • Business logic • Just a bit of indexing That’s it!
  123. 123. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Is Java 8 Streams “declarative” ? The Stream API looks a lot like SQL’s declarative style
  124. 124. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Is Java 8 Streams “declarative” ? The Stream API looks a lot like SQL’s declarative style FROM WHERE FETCH NEXT GROUP BY
  125. 125. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 In a way, it’s very similar TABLE : Stream<Tuple<..>> SELECT : map() DISTINCT : distinct() JOIN : flatMap() WHERE / HAVING : filter() GROUP BY : collect() ORDER BY : sorted() UNION ALL : concat() See: http://blog.jooq.org/2015/08/13/common-sql-clauses-and-their-equivalents-in-java-8-streams/
  126. 126. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, BigDecimal>>> result = ...; List<Film> films = loadAllFilms(); // TODO for (Film film : films) { Map<Integer, Map<LocalDate, BigDecimal>> dailyPerStore = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) { Map<LocalDate, BigDecimal> daily = dailyPerStore.computeIfAbsent( inventory.getStoreId(), k -> new HashMap()); for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); } }
  127. 127. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() .flatMap(f -> f.getInventories().stream()) .flatMap(i -> i.getRentals().stream()) .flatMap(r -> r.getPayments().stream()) .collect(... /* amount per film and store */ ...);
  128. 128. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() // Stream<Film> .flatMap(f -> f.getInventories().stream()) // Stream<Inventory> .flatMap(i -> i.getRentals().stream()) // Stream<Rental> .flatMap(r -> r.getPayments().stream()) // Stream<Payment> .collect(... /* amount per film and store */ ...);
  129. 129. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() // Stream<Film> .flatMap(f -> f.getInventories().stream()) // Stream<Inventory> .flatMap(i -> i.getRentals().stream()) // Stream<Rental> .flatMap(r -> r.getPayments().stream()) // Stream<Payment> .collect(... /* amount per film and store */ ...); How to access «f» and «i» in the Collector?
  130. 130. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() .flatMap(f -> f.getInventories().stream().map(i -> entry(f, i))) .flatMap(fi -> fi.v2.getRentals().stream().map(r -> entry(fi, r))) .flatMap(fir -> fir.v3.getPayments().stream().map(p -> entry(fir, p))) .collect(... /* amount per film and store */ ...); Use some wrapper types and pray for stack allocation to happen
  131. 131. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() .flatMap(f -> f.getInventories().stream().map(i -> entry(f, i))) .flatMap(fi -> fi.v2.getRentals().stream().map(r -> entry(fi, r))) .flatMap(fir -> fir.v3.getPayments().stream().map(p -> entry(fir, p))) .collect( Collectors.groupingBy(firp -> firp.getKey().getKey().getKey(), Collectors.groupingBy(firp -> firp.getKey().getKey().getValue() .getStoreId(), Collectors.groupingBy(firp -> firp.getValue().getPaymentDate(), Collectors.summingDouble( firp -> firp.getValue().getAmount().doubleValue() ) ) ) ) );
  132. 132. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() .flatMap(f -> f.getInventories().stream().map(i -> entry(f, i))) .flatMap(fi -> fi.v2.getRentals().stream().map(r -> entry(fi, r))) .flatMap(fir -> fir.v3.getPayments().stream().map(p -> entry(fir, p))) .collect( Collectors.groupingBy(firp -> firp.getKey().getKey().getKey(), Collectors.groupingBy(firp -> firp.getKey().getKey().getValue() .getStoreId(), Collectors.groupingBy(firp -> firp.getValue().getPaymentDate(), Collectors.summingDouble( firp -> firp.getValue().getAmount().doubleValue() ) ) ) ) ); But what the hell is this? (╯°□°)╯︵ ┻━┻
  133. 133. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Well, it certainly looks declarative
  134. 134. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 We can do a bit better, though Better Streams: https://github.com/jOOQ/jOOL
  135. 135. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 We can do a bit better, though Seq.seq(persons) .collect( count(), max(Person::getAge), min(Person::getHeight), avg(Person::getWeight) ); // (3, Optional[35], // Optional[1.69], Optional[70.0])
  136. 136. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = loadAllFilms() .stream() .flatMap(f -> f.getInventories().stream().map(i -> entry(f, i))) .flatMap(fi -> fi.v2.getRentals().stream().map(r -> entry(fi, r))) .flatMap(fir -> fir.v3.getPayments().stream().map(p -> entry(fir, p))) .collect( Collectors.groupingBy(firp -> firp.getKey().getKey().getKey(), Collectors.groupingBy(firp -> firp.getKey().getKey().getValue() .getStoreId(), Collectors.groupingBy(firp -> firp.getValue().getPaymentDate(), Collectors.summingDouble( firp -> firp.getValue().getAmount().doubleValue() ) ) ) ) );
  137. 137. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = Seq.seq(loadAllFilms()) .flatMap(f -> f.getInventories().stream().map(i -> entry(f, i))) .flatMap(fi -> fi.v2.getRentals().stream().map(r -> entry(fi, r))) .flatMap(fir -> fir.v3.getPayments().stream().map(p -> entry(fir, p))) .collect( Collectors.groupingBy(firp -> firp.getKey().getKey().getKey(), Collectors.groupingBy(firp -> firp.getKey().getKey().getValue() .getStoreId(), Collectors.groupingBy(firp -> firp.getValue().getPaymentDate(), Collectors.summingDouble( firp -> firp.getValue().getAmount().doubleValue() ) ) ) ) ); Use a Seq instead of a Stream
  138. 138. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) .crossApply(fi -> fi.v2.getRentals()) .crossApply(fir -> fir.v2.getPayments()) .collect( Collectors.groupingBy(firp -> firp.v1.v1.v1, Collectors.groupingBy(firp -> firp.v1.v1.v2.getStoreId(), Collectors.groupingBy(firp -> firp.v2.getPaymentDate(), Collectors.summingDouble( firp -> firp.v2.getAmount().doubleValue() ) ) ) ) ); Use SQL style CROSS APPLY instead of flatMap()
  139. 139. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) // <Film, Inventory> .crossApply(fi -> fi.v2.getRentals()) // <<F, I>, Rental> .crossApply(fir -> fir.v2.getPayments()) // <<<F, I>, R>, Payment> .collect( Collectors.groupingBy(firp -> firp.v1.v1.v1, Collectors.groupingBy(firp -> firp.v1.v1.v2.getStoreId(), Collectors.groupingBy(firp -> firp.v2.getPaymentDate(), Collectors.summingDouble( firp -> firp.v2.getAmount().doubleValue() ) ) ) ) );
  140. 140. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) .crossApply(fi -> fi.v2.getRentals()) .crossApply(fir -> fir.v2.getPayments()) .collect( Collectors.groupingBy(firp -> firp.v1.v1.v1, Collectors.groupingBy(firp -> firp.v1.v1.v2.getStoreId(), Collectors.groupingBy(firp -> firp.v2.getPaymentDate(), Collectors.summingDouble( firp -> firp.v2.getAmount().doubleValue() ) ) ) ) ); <<<Film, Inventory>, Rental>, Payment>
  141. 141. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) .crossApply(fi -> fi.v2.getRentals().stream() .flatMap(r -> r.getPayments().stream())) .collect( Collectors.groupingBy(fip -> fip.v1.v1, Collectors.groupingBy(fip -> fip.v1.v2.getStoreId(), Collectors.groupingBy(fip -> fip.v2.getPaymentDate(), Collectors.summingDouble( fip -> fip.v2.getAmount().doubleValue() ) ) ) ) ); Hm, never really needed the rentals
  142. 142. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Film, Map<Integer, Map<LocalDate, Double>>> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) .crossApply(fi -> fi.v2.getRentals().stream() .flatMap(r -> r.getPayments().stream())) .collect( Collectors.groupingBy(fip -> fip.v1.v1, Collectors.groupingBy(fip -> fip.v1.v2.getStoreId(), Collectors.groupingBy(fip -> fip.v2.getPaymentDate(), Collectors.summingDouble( fip -> fip.v2.getAmount().doubleValue() ) ) ) ) ); A little bit better now...
  143. 143. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Tuple3<Film, Integer, LocalDate>, Double> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) .crossApply(fi -> fi.v2.getRentals().stream() .flatMap(r -> r.getPayments().stream())) .groupBy( fip -> tuple( fip.v1.v1, fip.v1.v2.getStoreId(), fip.v2.getPaymentDate() ), Agg.sum(fip -> fip.v2.getAmount()) ); Oh, not that bad, actually ┬─┬ ノ( ゜-゜ノ)
  144. 144. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Let’s try that Java algorithm again Map<Tuple3<Film, Integer, LocalDate>, Double> result = Seq.seq(loadAllFilms()) .crossApply(f -> f.getInventories()) .crossApply(fi -> fi.v2.getRentals().stream() .flatMap(r -> r.getPayments().stream())) .groupBy( fip -> tuple( fip.v1.v1, fip.v1.v2.getStoreId(), fip.v2.getPaymentDate() ), Agg.sum(fip -> fip.v2.getAmount()) ); Result type a bit nicer, too
  145. 145. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Bad news is: How much time did we spend on this problem now?
  146. 146. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Remember this slide? Was it all that hard? SELECT title, store_id, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, store_id, payment_date ORDER BY title, store_id, payment_date
  147. 147. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Everyone understands now… SQL is a 4GL This means great improvement of developer productivity
  148. 148. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Everyone understands now… How much time did I spend talking about SQL so far? How much time on Java? SQL is easier = SQL is more productive
  149. 149. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What will I talk about? • SQL is awesome • SQL is productive • SQL is fast
  150. 150. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Suspense! Best chapter: Performance
  151. 151. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 But first How does SQL work?
  152. 152. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Business asking How many films did each actor whose last name starts with «A» play in?
  153. 153. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 They want this Starts with «A» How many films
  154. 154. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 This is the query. But what really happens? SELECT first_name, last_name, count(*) FROM actor JOIN film_actor USING (actor_id) WHERE last_name LIKE 'A%' GROUP BY first_name, last_name ORDER BY count(*) DESC
  155. 155. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables
  156. 156. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables Observe: «LOGICAL». Not «ACTUAL»
  157. 157. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables 2. Apply predicates
  158. 158. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables 2. Apply predicates 3. Collect groups
  159. 159. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables 2. Apply predicates 3. Collect groups 4. Aggregate
  160. 160. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables 2. Apply predicates 3. Collect groups 4. Aggregate 5. Project
  161. 161. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Logical Operations order 1. Load tables 2. Apply predicates 3. Collect groups 4. Aggregate 5. Project 6. Order
  162. 162. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Complete order Logical operations order 1. FROM, JOIN, APPLY 2. WHERE 3. CONNECT BY (Oracle) 4. GROUP BY 5. Aggregations 6. HAVING 7. WINDOW 8. MODEL (Oracle) 9. SELECT 10. DISTINCT 11. UNION, INTERSECT, EXCEPT 12. ORDER BY 13. OFFSET 14. LIMIT 15. FOR UPDATE
  163. 163. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? But that’s not what really happens! Any algorithm that produces the same result is acceptable.
  164. 164. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? Can we see the actual algorithm?
  165. 165. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 It’s called «execution plan»
  166. 166. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Who has seen an execution plan?
  167. 167. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SQL Developer
  168. 168. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 pgAdmin III
  169. 169. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SQL Workbench
  170. 170. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 SQL Server Management Studio
  171. 171. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? How many rows from operation
  172. 172. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? How much time does it take
  173. 173. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Imperative Actual operations order 1. Access ROWIDs from Index given the predicate What’s the actual algorithm?
  174. 174. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? Imperative Actual operations order 1. Access ROWIDs from Index given the predicate 2. Access other columns from the same row in the table
  175. 175. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? Imperative Actual operations order 1. Access ROWIDs from Index given the predicate 2. Access other columns from the same row in the table 3. Load foreign key relationship index into memory
  176. 176. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? Imperative Actual operations order 1. Access ROWIDs from Index given the predicate 2. Access other columns from the same row in the table 3. Load foreign key relationship index into memory 4. Put both data sets in hashmaps and join them
  177. 177. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? Imperative Actual operations order 1. Access ROWIDs from Index given the predicate 2. Access other columns from the same row in the table 3. Load foreign key relationship index into memory 4. Put both data sets in hashmaps and join them 5. Create another hashmap to group rows
  178. 178. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What’s the actual algorithm? Imperative Actual operations order 1. Access ROWIDs from Index given the predicate 2. Access other columns from the same row in the table 3. Load foreign key relationship index into memory 4. Put both data sets in hashmaps and join them 5. Create another hashmap to group rows 6. Sort the result
  179. 179. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Huh! Declarative vs Imperative Would you have chosen the same algorithm?
  180. 180. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 E.g. Apparently, it’s faster in this case to load 5462 rows into memory than seeking 6 counts individually... Huh!
  181. 181. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Big O Notation Hash join: O(N) (N = film_actors (bigger table)) Nested loop join with indexes: O(log M x log N) (M = actors, N = film_actors)
  182. 182. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Hash join: O(N) (N = film_actors (bigger table)) Nested loop join with indexes: O(log M x log N) (M = actors, N = film_actors) Big O Notation
  183. 183. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Well... It depends!
  184. 184. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What you think you have
  185. 185. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What you may really have
  186. 186. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What you may really have «Works on my machine» ಠ_ಠ
  187. 187. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 More info bigocheatsheet.com
  188. 188. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Here’s an interesting thing to think about: Histograms
  189. 189. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Histogram
  190. 190. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Histogram -- Predicate 1 WHERE amount BETWEEN -2000 AND 2000 -- Predicate 2 WHERE amount BETWEEN 5000 AND 9000
  191. 191. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Histogram -- Predicate 1 WHERE amount BETWEEN -2000 AND 2000 -- Predicate 2 WHERE amount BETWEEN 5000 AND 9000 Do both predicates profit from indexing?
  192. 192. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Histogram -- Not very selective predicate WHERE amount BETWEEN -2000 AND 2000 -- Very selective predicate WHERE amount BETWEEN 5000 AND 9000
  193. 193. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Histogram -- Not very selective predicate WHERE amount BETWEEN -2000 AND 2000 -- Very selective predicate WHERE amount BETWEEN 5000 AND 9000
  194. 194. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics Accessing the index is only worth the pain if it helps reduce the result set Otherwise, just read from the table
  195. 195. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics The database knows all these things. • How many rows are returned from a table? • From a query? • How much does it cost to access the disk? • The cache? • How often is this query run? • How often is the table accessed?
  196. 196. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Statistics The database knows all these things. • How much memory do we have? • How many processes are running in parallel right now? • Is the operating system doing anything? • How does the query perform with this bind variable? • Are we running on an HDD or SSD or Flash or RAM? • How many locks do we have on our rows?
  197. 197. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Expression trees Everything has a cost
  198. 198. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Expression trees How does SQL work? 1. A parsed SQL string produces an expression tree
  199. 199. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 How does SQL work? 1. A parsed SQL string produces an expression tree A + (A – B) Expression trees + -A A B
  200. 200. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Expression trees How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other
  201. 201. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other A + (A – B) Expression trees + -A A B
  202. 202. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other A + (A – B) (A + A) - B Expression trees + -A A B + - A A B
  203. 203. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other A + (A – B) (2 * A) - B Expression trees + -A A B * - 2 A B
  204. 204. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other A + (A – B) (2 * A) - B Expression trees + -A A B * - 2 A B E.g.: Accessing A is super expensive
  205. 205. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Expression trees How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other 3. Each expression tree has a corresponding execution plan and an associated, estimated cost 4. The «best» plan (= lowest estimated cost) is chosen 5. The actual execution cost is compared with the estimate to improve future estimates
  206. 206. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Expression trees How does SQL work? 1. A parsed SQL string produces an expression tree 2. Multiple expression trees are equivalent: They can be transformed into each other 3. Each expression tree has a corresponding execution plan and an associated, estimated cost 4. The «best» plan (= lowest estimated cost) is chosen 5. The actual execution cost is compared with the estimate to improve future estimates When’s the last time you wrote Java code that did this?
  207. 207. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Expression trees Special treat: Oracle 12c can abort and switch execution plans «in flight». This is called «Adaptive Execution Plans».
  208. 208. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 What will I talk about? • SQL is awesome • SQL is productive • SQL is fast
  209. 209. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why do I talk about SQL? Now that you know all of this... What do you prefer?
  210. 210. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 This? Map<Film, Map<Integer, Map<LocalDate, BigDecimal>>> result = ...; List<Film> films = nPlusOneLoadAllFilms(); // TODO for (Film film : films) { Map<Integer, Map<LocalDate, BigDecimal>> dailyPerStore = result.computeIfAbsent(film, k -> new HashMap<>()); for (Inventory inventory : film.getInventories()) { Map<LocalDate, BigDecimal> daily = dailyPerStore.computeIfAbsent( inventory.getStoreId(), k -> new HashMap()); for (Rental rental : inventory.getRentals()) for (Payment p : rental.getPayments()) daily.compute( p.getPaymentDate(), (k, v) -> v == null ? p.getAmount() : p.getAmount().add(v)); } }
  211. 211. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Or, this? SELECT title, store_id, payment_date, SUM(amount) FROM film JOIN inventory USING (film_id) JOIN rental USING (inventory_id) JOIN payment USING (rental_id) GROUP BY film_id, store_id, payment_date ORDER BY title, store_id, payment_date
  212. 212. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why do I talk about SQL? SQL is the only ever successful, mainstream, and general- purpose 4GL (Fourth- Generation Programming Language) And it is awesome!
  213. 213. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Why doesn’t anyone else talk about SQL? ¯_(シ)_/¯
  214. 214. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Can I write SQL in Java?
  215. 215. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Can I write SQL in Java? – Yes. With jOOQ
  216. 216. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Can I write SQL in Java? – Yes. With jOOQ
  217. 217. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Can I write SQL in Java? – Yes. With jOOQ
  218. 218. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 So what’s the key takeaway? 1. Can you do it in the database? Yes 2. Can you do it in the database? Yes (... after visiting my 2 day SQL training) 3. Can you do it in your database? Yes (... unless you're using MySQL) 4. Should you do it in the database? Yes 5. Do long talk titles attract attention? Yes 6. Will this talk ever end? Yes
  219. 219. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 If you haven’t had enough Google «10 SQL Tricks» and find the other talk’s transcript https://blog.jooq.org/2016/04/25/10-sql- tricks-that-you-didnt-think-were-possible/
  220. 220. Copyright (c) 2009-2017 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 Thank you Check out our trainings: http://www.jooq.org/training Coordinates • Blog: http://blog.jooq.org (excellent Java SQL content) • Twitter: @JavaOOQ / @lukaseder (more lame jokes) • E-Mail: lukas.eder@datageekery.com • Bank account: CH57 8148 7000 0SQL AWSM 7
  • number0

    Feb. 7, 2021
  • SurjyaNarayanDas

    May. 13, 2020
  • AlbertoGonzalez148

    Aug. 25, 2017
  • ssuserbb038e

    Jun. 8, 2017
  • josejuanmontiel

    May. 12, 2017
  • VincentKirsch

    Mar. 5, 2017
  • EliasNema

    Mar. 1, 2017
  • DanielEkberg2

    Feb. 28, 2017
  • StaniiVeljko

    Feb. 27, 2017
  • yannickgrenzinger

    Feb. 27, 2017
  • AntoineLatrille

    Feb. 27, 2017
  • PriamoGermosen

    Feb. 26, 2017
  • QianqianSong2

    Feb. 26, 2017
  • googya

    Feb. 26, 2017
  • RomanAtaman

    Feb. 26, 2017
  • ongobongo

    Feb. 26, 2017
  • AnthonyMorrisonFighter

    Feb. 26, 2017
  • kwangyongkim

    Feb. 26, 2017
  • rponte

    Feb. 25, 2017

SQL is the only ever successful, mainstream, and general-purpose 4GL (Fourth Generation Programming Language) and it is awesome! With modern cost based optimisation, relational databases like Oracle, SQL Server, PostgreSQL finally keep up to the promise of a powerful declarative programming model by adapting to ever changing productive data without performance penalties. Thousand-line-long, complex SQL statements can be run in far below a millisecond against billion-row strong tables if database developers know their ways around the SQL language - and the best news is: It's not that hard! In this talk, I'll show how the SQL database will constantly outperform any hand written data retrieval algorithm - or in other words - how SQL, being a logic language, is the best language for business logic.

Aufrufe

Aufrufe insgesamt

34.027

Auf Slideshare

0

Aus Einbettungen

0

Anzahl der Einbettungen

28.802

Befehle

Downloads

109

Geteilt

0

Kommentare

0

Likes

19

×