SlideShare a Scribd company logo
1 of 75
Download to read offline
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL, the
underestimated
“Big Data” technology
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
No – tation
Seen at the 2013 O’Reilly Strata Conf:
History of NoSQL by Mark Madsen. Picture published by Edd Dumbill
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
NoSQL?
NoSQL?
No, SQL!
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Our vision at Data Geekery
- SQL dominates database systems
- SQL is very expressive
- SQL is very type safe
SQL is a device whose mystery is
only exceeded by its power!
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Me – @lukaseder
Java developers can get back in
control of SQL with jOOQ
- Head of R&D at Data Geekery GmbH
- SQL Aficionado
- Java Aficionado
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Big Data? NoSQL?
- You’re giving up on ACID
- You’re giving up on type safety
- You’re giving up on standards
- You’re giving up on tooling
- You’re giving up on relational algebra
- You haven’t asked operations
- You don’t actually have «Big Data»
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Big Data? NoSQL?
- You’re giving up on ACID
- You’re giving up on type safety
- You’re giving up on standards
- You’re giving up on tooling
- You’re giving up on relational algebra
- You haven’t asked operations
- You don’t actually have «Big Data»
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Also Not SQL
@Entity @Table(name = "EVENTS")
public class Event {
private Long id;
private String title;
private Date date;
@Id @GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
public Long getId() { /* … */ }
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "EVENT_DATE")
public Date getDate() { /* … */ }
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Also Not SQL – Annotatiomania™
@OneToMany(mappedBy = "destCustomerId")
@ManyToMany
@Fetch(FetchMode.SUBSELECT)
@JoinTable(
name = "customer_dealer_map",
joinColumns = {
@JoinColumn(name = "customer_id", referencedColumnName = "id")
},
inverseJoinColumns = {
@JoinColumn(name = "dealer_id", referencedColumnName = "id")
}
)
private Collection dealers;
Found at http://stackoverflow.com/q/17491912/521799
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Also Not SQL – JPA 3.0 Preview
@OneToMany @OneToManyMore @AnyOne @AnyBody
@ManyToMany @Many
@Fetch @FetchMany @FetchWithDiscriminator(name = "no_name")
@JoinTable(joinColumns = {
@JoinColumn(name = "customer_id", referencedColumnName = "id")
})
@PrefetchJoinWithDiscriminator
@IfJoiningAvoidHashJoins @ButUseHashJoinsWhenMoreThan(records = 1000)
@XmlDataTransformable @SpringPrefechAdapter
private Collection employees;
Might not be true
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Shocker! You can now write SQL in Java.
JDBC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL in Java 7 – JDBC
try (PreparedStatement stmt = c.prepareStatement(sql);
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
System.out.println(
new Schema(rs.getString("SCHEMA_NAME"),
rs.getBoolean("IS_DEFAULT"))
);
}
}
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL in Java 8 – jOOQ
DSL.using(c)
.fetch(sql)
.map(rs -> new Schema(
rs.getValue("SCHEMA_NAME", String.class),
rs.getValue("IS_DEFAULT", boolean.class)
))
.forEach(System.out::println);
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Typesafe SQL in Java – jOOQ
DSL.using(c)
.select(s.SCHEMA_NAME, s.IS_DEFAULT)
.from(INFORMATION_SCHEMA.SCHEMATA.as("s"))
.orderBy(s.SCHEMA_NAME)
.map(rs -> new Schema(
rs.getValue(s.SCHEMA_NAME),
rs.getValue(s.IS_DEFAULT)
))
.forEach(System.out::println);
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL in Java 8 – Spring JDBC
new JdbcTemplate(
new SingleConnectionDataSource(c, true))
.query(sql, (rs, rowNum) ->
new Schema(
rs.getString("SCHEMA_NAME"),
rs.getBoolean("IS_DEFAULT")
))
.forEach(System.out::println);
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL in Java 8 – Apache DbUtils
new QueryRunner()
.query(c, sql, new ArrayListHandler())
.stream()
.map(array -> new Schema(
(String) array[0],
(Boolean) array[1]
))
.forEach(System.out::println);
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL in Groovy
sql.eachRow( 'select * from tableName' ) {
println "$it.id -- ${it.firstName} --"
}
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
When you should use SQL – indicators
- You need JOINs, UNIONs
- You need functions, aggregations
- You need bulk reads / writes
Calculations should be
done close to the data
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Please, run that calculation in your DB
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – NULL
-- What does this query return?
SELECT 1 AS a FROM dual
WHERE 1 IN (NULL)
UNION ALL
SELECT 2 AS a FROM dual
WHERE NOT(1 IN (NULL))
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – NULL
-- What does this query return?
SELECT 1 AS a FROM dual
WHERE 1 IN (NULL)
UNION ALL
SELECT 2 AS a FROM dual
WHERE NOT(1 IN (NULL))
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – NULL
-- Nothing! It’s the same as this
SELECT 1 AS a FROM dual
WHERE 1 = NULL
UNION ALL
SELECT 2 AS a FROM dual
WHERE 1 != NULL
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – NULL
-- Nothing! It’s the same as this
SELECT 1 AS a FROM dual
WHERE “UNKNOWN”
UNION ALL
SELECT 2 AS a FROM dual
WHERE “UNKNOWN”
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – Oracle VARCHAR2
-- What does this query return?
SELECT 1 AS a FROM dual
WHERE '' = ''
UNION ALL
SELECT 2 AS a FROM dual
WHERE 'a' != ''
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – Oracle VARCHAR2
-- Nope! Nothing again (only in Oracle).
SELECT 1 AS a FROM dual
WHERE NULL = NULL
UNION ALL
SELECT 2 AS a FROM dual
WHERE 'a' != NULL
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SQL Trivia – Oracle VARCHAR2
-- Nope! Nothing again (only in Oracle).
SELECT 1 AS a FROM dual
WHERE NULL = NULL
UNION ALL
SELECT 2 AS a FROM dual
WHERE 'a' != NULL
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Stockholm Syndrome:
We love JavaScript SQL
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Winston Churchill:
SQL is the worst form of
database querying, except
for all the other forms.
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Let’s calculate a running total
SELECT *
FROM v_transactions
WHERE account_id = 1
ORDER BY value_date DESC,
id DESC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT |
|------|------------|--------|
| 9997 | 2014-03-18 | 99.17 |
| 9981 | 2014-03-16 | 71.44 |
| 9979 | 2014-03-16 | -94.60 |
| 9977 | 2014-03-16 | -6.96 |
| 9971 | 2014-03-15 | -65.95 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | 99.17 | 19985.81 |
| 9981 | 2014-03-16 | 71.44 | 19886.64 |
| 9979 | 2014-03-16 | -94.60 | 19815.20 |
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | +99.17 =19985.81 |
| 9981 | 2014-03-16 | 71.44 | +19886.64 |
| 9979 | 2014-03-16 | -94.60 | 19815.20 |
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | 99.17 | 19985.81 |
| 9981 | 2014-03-16 | +71.44 =19886.64 |
| 9979 | 2014-03-16 | -94.60 | +19815.20 |
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Let’s calculate a running total
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 | 99.17 | 19985.81 |
| 9981 | 2014-03-16 | +71.44 =19886.64 | n
| 9979 | 2014-03-16 | -94.60 | +19815.20 | n+1
| 9977 | 2014-03-16 | -6.96 | 19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |BALANCE(ROWn) = BALANCE(ROWn+1) + AMOUNT(ROWn)
BALANCE(ROWn+1) = BALANCE(ROWn) – AMOUNT(ROWn)
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
How can we do it?
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
How can we do it?
- In Java
- Calculate on UPDATE
- Nested SELECT
- Recursive SQL
- Window functions
- MODEL clause (Oracle)
- Stored procedures
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
How can we do it? – With SQL!
- In Java
- Calculate on UPDATE
- Nested SELECT
- Recursive SQL
- Window functions
- MODEL clause (Oracle)
- Stored procedures
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Using nested SELECTs
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT
t1.*,
t1.current_balance - (
SELECT NVL(SUM(amount), 0)
FROM v_transactions t2
WHERE t2.account_id = t1.account_id
AND (t2.value_date, t2.id) >
(t1.value_date, t1.id)
) AS balance
FROM v_transactions t1
WHERE t1.account_id = 1
ORDER BY t1.value_date DESC, t1.id DESC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT
t1.*,
t1.current_balance - (
SELECT NVL(SUM(amount), 0)
FROM v_transactions t2
WHERE t2.account_id = t1.account_id
AND (t2.value_date, t2.id) >
(t1.value_date, t1.id)
) AS balance
FROM v_transactions t1
WHERE t1.account_id = 1
ORDER BY t1.value_date DESC, t1.id DESC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT
t1.*,
t1.current_balance - (
SELECT NVL(SUM(amount), 0)
FROM v_transactions t2
WHERE t2.account_id = t1.account_id
AND ((t2.value_date > t1.value_date) OR
(t2.value_date = t1.value_date AND
t2.id > t1.id))
) AS balance
FROM v_transactions t1
WHERE t1.account_id = 1 ORDER BY ...
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Using nested SELECTs
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 -(99.17)| +19985.81 |
| 9981 | 2014-03-16 -(71.44)| 19886.64 |
| 9979 | 2014-03-16 -(-94.60)| 19815.20 |
| 9977 | 2014-03-16 | -6.96 | =19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
| Id | Operation | Name | A-Rows | A-Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50 |00:00:00.77 |
| 1 | SORT AGGREGATE | | 1101 |00:00:00.76 |
|* 2 | TABLE ACCESS BY INDEX ROWID | T_TRANSACTIONS | 605K|00:00:00.69 |
|* 3 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 1212K|00:00:00.21 |
| 4 | SORT ORDER BY | | 50 |00:00:00.77 |
| 5 | NESTED LOOPS | | 1101 |00:00:00.01 |
| 6 | TABLE ACCESS BY INDEX ROWID| T_ACCOUNTS | 1 |00:00:00.01 |
|* 7 | INDEX UNIQUE SCAN | SYS_C006991 | 1 |00:00:00.01 |
| 8 | TABLE ACCESS BY INDEX ROWID| T_TRANSACTIONS | 1101 |00:00:00.01 |
|* 9 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 1101 |00:00:00.01 |
------------------------------------------------------------------------------
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Using recursive SQL
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
We need to number transactions
| ID | VALUE_DATE | AMOUNT | TRANSACTION_NR |
|------|------------|--------|----------------|
| 9997 | 2014-03-18 | 99.17 | 1 |
| 9981 | 2014-03-16 | 71.44 | 2 |
| 9979 | 2014-03-16 | -94.60 | 3 |
| 9977 | 2014-03-16 | -6.96 | 4 |
| 9971 | 2014-03-15 | -65.95 | 5 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
CREATE OR REPLACE VIEW v_transactions_by_time
AS
SELECT
t.*,
ROW_NUMBER() OVER (
PARTITION BY account_id
ORDER BY t.value_date DESC,
t.id DESC
) AS transaction_number
FROM
v_transactions t;
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
WITH ordered_with_balance (
account_id, value_date, amount, balance, transaction_number
)
AS (
SELECT t1.account_id, t1.value_date, t1.amount, t1.current_balance,
t1.transaction_number
FROM v_transactions_by_time t1
WHERE t1.transaction_number = 1
UNION ALL
SELECT t1.account_id, t1.value_date, t1.amount, t2.balance - t2.amount,
t1.transaction_number
FROM ordered_with_balance t2
JOIN v_transactions_by_time t1
ON t1.transaction_number = t2.transaction_number + 1
AND t1.account_id = t2.account_id
)
SELECT *
FROM ordered_with_balance
WHERE account_id = 1
ORDER BY transaction_number ASC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
WITH ordered_with_balance (
account_id, value_date, amount, balance, transaction_number
)
AS (
SELECT t1.account_id, t1.value_date, t1.amount, t1.current_balance,
t1.transaction_number
FROM v_transactions_by_time t1
WHERE t1.transaction_number = 1
UNION ALL
SELECT t1.account_id, t1.value_date, t1.amount, t2.balance - t2.amount,
t1.transaction_number
FROM ordered_with_balance t2
JOIN v_transactions_by_time t1
ON t1.transaction_number = t2.transaction_number + 1
AND t1.account_id = t2.account_id
)
SELECT *
FROM ordered_with_balance
WHERE account_id = 1
ORDER BY transaction_number ASC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
WITH ordered_with_balance (
account_id, value_date, amount, balance, transaction_number
)
AS (
SELECT t1.account_id, t1.value_date, t1.amount, t1.current_balance,
t1.transaction_number
FROM v_transactions_by_time t1
WHERE t1.transaction_number = 1
UNION ALL
SELECT t1.account_id, t1.value_date, t1.amount, t2.balance - t2.amount,
t1.transaction_number
FROM ordered_with_balance t2
JOIN v_transactions_by_time t1
ON t1.transaction_number = t2.transaction_number + 1
AND t1.account_id = t2.account_id
)
SELECT *
FROM ordered_with_balance
WHERE account_id = 1
ORDER BY transaction_number ASC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
---------------------------------------------------------------------------------------------------
| Id | Operation | Name | A-Rows | A-Time |
---------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50 |00:00:35.29 |
| 1 | SORT ORDER BY | | 50 |00:00:35.29 |
|* 2 | VIEW | | 1101 |00:00:35.29 |
| 3 | UNION ALL (RECURSIVE WITH) BREADTH FIRST| | 9999 |00:00:35.28 |
|* 4 | VIEW | V_TRANSACTIONS_BY_TIME | 9 |00:00:00.03 |
|* 5 | WINDOW SORT PUSHED RANK | | 18 |00:00:00.03 |
| 6 | NESTED LOOPS | | 9999 |00:00:00.01 |
| 7 | NESTED LOOPS | | 9999 |00:00:00.01 |
| 8 | TABLE ACCESS FULL | T_ACCOUNTS | 10 |00:00:00.01 |
|* 9 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 9999 |00:00:00.01 |
| 10 | TABLE ACCESS BY INDEX ROWID | T_TRANSACTIONS | 9999 |00:00:00.01 |
|* 11 | HASH JOIN | | 9990 |00:00:35.08 |
| 12 | VIEW | V_TRANSACTIONS_BY_TIME | 11M|00:00:29.13 |
| 13 | WINDOW SORT | | 11M|00:00:27.19 |
| 14 | NESTED LOOPS | | 11M|00:00:13.62 |
| 15 | NESTED LOOPS | | 11M|00:00:03.89 |
| 16 | INDEX FAST FULL SCAN | SYS_C006991 | 11450 |00:00:00.06 |
|* 17 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 11M|00:00:02.18 |
| 18 | TABLE ACCESS BY INDEX ROWID | T_TRANSACTIONS | 11M|00:00:06.15 |
| 19 | RECURSIVE WITH PUMP | | 9999 |00:00:00.01 |
---------------------------------------------------------------------------------------------------
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Using window
functions
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT
t.*,
t.current_balance - NVL(
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
),
0) AS balance
FROM v_transactions t
WHERE t.account_id = 1
ORDER BY t.value_date DESC,
t.id DESC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT
t.*,
t.current_balance - NVL(
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
),
0) AS balance
FROM v_transactions t
WHERE t.account_id = 1
ORDER BY t.value_date DESC,
t.id DESC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Using window functions
| ID | VALUE_DATE | AMOUNT | BALANCE |
|------|------------|--------|------------|
| 9997 | 2014-03-18 -(99.17)| +19985.81 |
| 9981 | 2014-03-16 -(71.44)| 19886.64 |
| 9979 | 2014-03-16 -(-94.60)| 19815.20 |
| 9977 | 2014-03-16 | -6.96 | =19909.80 |
| 9971 | 2014-03-15 | -65.95 | 19916.76 |
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
------------------------------------------------------------------------------
| Id | Operation | Name | A-Rows | A-Time |
------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50 |00:00:00.01 |
| 1 | WINDOW SORT | | 50 |00:00:00.01 |
| 2 | NESTED LOOPS | | 1101 |00:00:00.01 |
| 3 | TABLE ACCESS BY INDEX ROWID| T_ACCOUNTS | 1 |00:00:00.01 |
|* 4 | INDEX UNIQUE SCAN | SYS_C006991 | 1 |00:00:00.01 |
| 5 | TABLE ACCESS BY INDEX ROWID| T_TRANSACTIONS | 1101 |00:00:00.01 |
|* 6 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 1101 |00:00:00.01 |
------------------------------------------------------------------------------
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Using the Oracle
MODEL clause
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT account_id, value_date, amount, balance
FROM (
SELECT id, account_id, value_date, amount,
current_balance AS balance
FROM v_transactions
) t
WHERE account_id = 1
MODEL
PARTITION BY (account_id)
DIMENSION BY (
ROW_NUMBER() OVER (ORDER BY value_date DESC, id DESC) AS rn
)
MEASURES (value_date, amount, balance)
RULES (
balance[rn > 1] = balance[cv(rn) - 1] - amount[cv(rn) - 1]
)
ORDER BY rn ASC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT account_id, value_date, amount, balance
FROM (
SELECT id, account_id, value_date, amount,
current_balance AS balance
FROM v_transactions
) t
WHERE account_id = 1
MODEL
PARTITION BY (account_id)
DIMENSION BY (
ROW_NUMBER() OVER (ORDER BY value_date DESC, id DESC) AS rn
)
MEASURES (value_date, amount, balance)
RULES (
balance[rn > 1] = balance[cv(rn) - 1] - amount[cv(rn) - 1]
)
ORDER BY rn ASC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
RULES (
balance[rn > 1] = balance[cv(rn) - 1]
- amount [cv(rn) - 1]
)
-- does it look familiar?
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
--------------------------------------------------------------------------------
| Id | Operation | Name | A-Rows | A-Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 50 |00:00:00.02 |
| 1 | SORT ORDER BY | | 50 |00:00:00.02 |
| 2 | SQL MODEL ORDERED | | 1101 |00:00:00.02 |
| 3 | WINDOW SORT | | 1101 |00:00:00.01 |
| 4 | NESTED LOOPS | | 1101 |00:00:00.01 |
| 5 | TABLE ACCESS BY INDEX ROWID| T_ACCOUNTS | 1 |00:00:00.01 |
|* 6 | INDEX UNIQUE SCAN | SYS_C006991 | 1 |00:00:00.01 |
|* 7 | TABLE ACCESS FULL | T_TRANSACTIONS | 1101 |00:00:00.01 |
--------------------------------------------------------------------------------
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
The MODEL clause is
Oracle’s most
powerful and
underused feature
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Our vision at Data Geekery - Revisited
- SQL dominates database systems
- SQL is expressive
- SQL is type safe
SQL is a device whose mystery is
only exceeded by its power!
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Our vision at Data Geekery - Revisited
- SQL dominates database systems
- SQL is expressive
- SQL is type safe
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Our vision at Data Geekery - Revisited
jOOQ is the best way
to write SQL in Java
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
SELECT
t.*,
t.current_balance - NVL(
SUM(t.amount) OVER (
PARTITION BY t.account_id
ORDER BY t.value_date DESC,
t.id DESC
ROWS BETWEEN UNBOUNDED PRECEDING
AND 1 PRECEDING
),
0) AS balance
FROM v_transactions t
WHERE t.account_id = 1
ORDER BY t.value_date DESC,
t.id DESC
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
DSL.using(connection)
.select(t.VALUE_DATE,
t.AMOUNT,
t.CURRENT_BALANCE.sub(
sum(t.AMOUNT).over(
partitionBy(t.ACCOUNT_ID)
.orderBy (t.VALUE_DATE.desc(),
t.ID .desc())
.rowsBetweenUnboundedPreceding()
.andPreceding(1)
)
).nvl(0).as("balance"))
.from (V_TRANSACTIONS.as("t"))
.where (t.ACCOUNT_ID.eq(1))
.orderBy(t.VALUE_DATE.desc(),
t.ID .desc())
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Thank you
3-month jOOQ Enterprise trial:
• Send «JUGS-LU-SQL-2014» to
sales@datageekery.com
More free Java / SQL knowledge on:
• Blog: http://blog.jooq.org
• Twitter: @JavaOOQ
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
There is SQL before and
after window functions
This just in… (in case you haven’t seen enough)
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Image Copyright © fanpictor.com
Use-case: Choreo export
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Use-case: Choreo export as Excel
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Use-case: Choreo export as Excel
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Use-case: Choreo export as Excel
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
WITH data AS (SELECT d.*,
row(sector, row, scene1, scene2) block
FROM d)
SELECT data.*,
CASE WHEN LAG (block) OVER (o) IS DISTINCT FROM block
AND LEAD(block) OVER (o) IS DISTINCT FROM block
THEN 'start / stop'
WHEN LAG (block) OVER (o) IS DISTINCT FROM block
THEN 'start'
WHEN LEAD(block) OVER (o) IS DISTINCT FROM block
THEN 'stop'
ELSE '' END start_stop,
COUNT(*) OVER (PARTITION BY sector, row, scene1, scene2)
FROM data
WINDOW o AS (ORDER BY sector, row, seat)
ORDER BY sector, row, seat
Full example: http://blog.jooq.org/2014/04/15/how-to-do-this-with-sql-of-course
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Full example: http://blog.jooq.org/2014/04/15/how-to-do-this-with-sql-of-course
WITH data AS (SELECT d.*,
row(sector, row, scene1, scene2) block
FROM d)
SELECT data.*,
CASE WHEN LAG (block) OVER (o) IS DISTINCT FROM block
AND LEAD(block) OVER (o) IS DISTINCT FROM block
THEN 'start / stop'
WHEN LAG (block) OVER (o) IS DISTINCT FROM block
THEN 'start'
WHEN LEAD(block) OVER (o) IS DISTINCT FROM block
THEN 'stop'
ELSE '' END start_stop,
COUNT(*) OVER (PARTITION BY sector, row, scene1, scene2)
FROM data
WINDOW o AS (ORDER BY sector, row, seat)
ORDER BY sector, row, seat
We can compare rows with each other, not only
columns!
Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0
NoSQL? No, SQL!
Full example: http://blog.jooq.org/2014/04/15/how-to-do-this-with-sql-of-course
WITH data AS (SELECT d.*,
row(sector, row, scene1, scene2) block
FROM d)
SELECT data.*,
CASE WHEN LAG (block) OVER (o) IS DISTINCT FROM block
AND LEAD(block) OVER (o) IS DISTINCT FROM block
THEN 'start / stop'
WHEN LAG (block) OVER (o) IS DISTINCT FROM block
THEN 'start'
WHEN LEAD(block) OVER (o) IS DISTINCT FROM block
THEN 'stop'
ELSE '' END start_stop,
COUNT(*) OVER (PARTITION BY sector, row, scene1, scene2)
FROM data
WINDOW o AS (ORDER BY sector, row, seat)
ORDER BY sector, row, seat
We can reuse window specifications!

More Related Content

What's hot

Get Back in Control of Your SQL - #33rdDegree
Get Back in Control of Your SQL - #33rdDegreeGet Back in Control of Your SQL - #33rdDegree
Get Back in Control of Your SQL - #33rdDegreeDataGeekery
 
Industrializing the creation of machine images and Docker containers for clou...
Industrializing the creation of machine images and Docker containers for clou...Industrializing the creation of machine images and Docker containers for clou...
Industrializing the creation of machine images and Docker containers for clou...OW2
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개Reagan Hwang
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafMasatoshi Tada
 
Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...
Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...
Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...Amazon Web Services
 
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - EuropeThe Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - EuropeJoshua Long
 

What's hot (8)

Scala and Spring
Scala and SpringScala and Spring
Scala and Spring
 
Get Back in Control of Your SQL - #33rdDegree
Get Back in Control of Your SQL - #33rdDegreeGet Back in Control of Your SQL - #33rdDegree
Get Back in Control of Your SQL - #33rdDegree
 
Industrializing the creation of machine images and Docker containers for clou...
Industrializing the creation of machine images and Docker containers for clou...Industrializing the creation of machine images and Docker containers for clou...
Industrializing the creation of machine images and Docker containers for clou...
 
JSLounge - TypeScript 소개
JSLounge - TypeScript 소개JSLounge - TypeScript 소개
JSLounge - TypeScript 소개
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with Thymeleaf
 
TDC 2016 - Arquitetura Java - Spring Cloud
TDC 2016 - Arquitetura Java - Spring CloudTDC 2016 - Arquitetura Java - Spring Cloud
TDC 2016 - Arquitetura Java - Spring Cloud
 
Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...
Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...
Deep Dive on New Amazon EC2 Instances and Virtualization Technologies - AWS O...
 
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - EuropeThe Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
The Cloud Foundry bootcamp talk from SpringOne On The Road - Europe
 

Similar to NoSQL? No, SQL! - SQL, the underestimated "Big Data" technology

2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
2000 lines of java or 50 lines of sql the choice is yours - Lukas EderJAXLondon_Conference
 
10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were PossibleLukas Eder
 
Best Way to Write SQL in Java
Best Way to Write SQL in JavaBest Way to Write SQL in Java
Best Way to Write SQL in JavaGerger
 
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014DataGeekery
 
Semplificare l'observability per progetti Serverless
Semplificare l'observability per progetti ServerlessSemplificare l'observability per progetti Serverless
Semplificare l'observability per progetti ServerlessLuciano Mammino
 
Practical SQL Azure: Moving into the cloud
Practical SQL Azure: Moving into the cloudPractical SQL Azure: Moving into the cloud
Practical SQL Azure: Moving into the cloudTimothy Corey
 
How to lock a Python in a cage? Managing Python environment inside an R project
How to lock a Python in a cage?  Managing Python environment inside an R projectHow to lock a Python in a cage?  Managing Python environment inside an R project
How to lock a Python in a cage? Managing Python environment inside an R projectWLOG Solutions
 
SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013
SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013
SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013Nick Galbreath
 
Semantic Web Standards and the Variety “V” of Big Data
Semantic Web Standards and  the Variety “V” of Big DataSemantic Web Standards and  the Variety “V” of Big Data
Semantic Web Standards and the Variety “V” of Big Databobdc
 
DMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure WorksDMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure WorksJohannes Hoppe
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Miningllangit
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Miningllangit
 
How to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and DataflowHow to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and DataflowDaniel Zivkovic
 
GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...
GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...
GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...James Anderson
 
Data relay introduction to big data clusters
Data relay introduction to big data clustersData relay introduction to big data clusters
Data relay introduction to big data clustersChris Adkin
 
Building Microservices in the cloud at AutoScout24
Building Microservices in the cloud at AutoScout24Building Microservices in the cloud at AutoScout24
Building Microservices in the cloud at AutoScout24Christian Deger
 
Serverless DevSecOps: Why We Must Make it Everyone's Problem | Hillel Solow
Serverless DevSecOps: Why We Must Make it Everyone's Problem | Hillel SolowServerless DevSecOps: Why We Must Make it Everyone's Problem | Hillel Solow
Serverless DevSecOps: Why We Must Make it Everyone's Problem | Hillel SolowAWSCOMSUM
 

Similar to NoSQL? No, SQL! - SQL, the underestimated "Big Data" technology (20)

2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
2000 lines of java or 50 lines of sql the choice is yours - Lukas Eder
 
10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible10 SQL Tricks that You Didn't Think Were Possible
10 SQL Tricks that You Didn't Think Were Possible
 
Best Way to Write SQL in Java
Best Way to Write SQL in JavaBest Way to Write SQL in Java
Best Way to Write SQL in Java
 
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
The jOOQ Talk at the JUG-HH in Hamburg, Jan 14, 2014
 
Get Back in Control of your SQL
Get Back in Control of your SQLGet Back in Control of your SQL
Get Back in Control of your SQL
 
Semplificare l'observability per progetti Serverless
Semplificare l'observability per progetti ServerlessSemplificare l'observability per progetti Serverless
Semplificare l'observability per progetti Serverless
 
Practical SQL Azure: Moving into the cloud
Practical SQL Azure: Moving into the cloudPractical SQL Azure: Moving into the cloud
Practical SQL Azure: Moving into the cloud
 
How to lock a Python in a cage? Managing Python environment inside an R project
How to lock a Python in a cage?  Managing Python environment inside an R projectHow to lock a Python in a cage?  Managing Python environment inside an R project
How to lock a Python in a cage? Managing Python environment inside an R project
 
SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013
SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013
SQL-RISC: New Directions in SQLi Prevention - RSA USA 2013
 
Semantic Web Standards and the Variety “V” of Big Data
Semantic Web Standards and  the Variety “V” of Big DataSemantic Web Standards and  the Variety “V” of Big Data
Semantic Web Standards and the Variety “V” of Big Data
 
Azure from scratch part 4
Azure from scratch part 4Azure from scratch part 4
Azure from scratch part 4
 
DMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure WorksDMDW Lesson 02 - Basics with Adventure Works
DMDW Lesson 02 - Basics with Adventure Works
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Mining
 
SQL Server 2008 Data Mining
SQL Server 2008 Data MiningSQL Server 2008 Data Mining
SQL Server 2008 Data Mining
 
How to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and DataflowHow to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
How to build unified Batch & Streaming Pipelines with Apache Beam and Dataflow
 
Accumulo on EC2
Accumulo on EC2Accumulo on EC2
Accumulo on EC2
 
GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...
GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...
GDG Cloud Southlake #16: Priyanka Vergadia: Scalable Data Analytics in Google...
 
Data relay introduction to big data clusters
Data relay introduction to big data clustersData relay introduction to big data clusters
Data relay introduction to big data clusters
 
Building Microservices in the cloud at AutoScout24
Building Microservices in the cloud at AutoScout24Building Microservices in the cloud at AutoScout24
Building Microservices in the cloud at AutoScout24
 
Serverless DevSecOps: Why We Must Make it Everyone's Problem | Hillel Solow
Serverless DevSecOps: Why We Must Make it Everyone's Problem | Hillel SolowServerless DevSecOps: Why We Must Make it Everyone's Problem | Hillel Solow
Serverless DevSecOps: Why We Must Make it Everyone's Problem | Hillel Solow
 

Recently uploaded

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsSafe Software
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?Alexandre Beguel
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxRTS corp
 

Recently uploaded (20)

UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 
Powering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data StreamsPowering Real-Time Decisions with Continuous Data Streams
Powering Real-Time Decisions with Continuous Data Streams
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?SAM Training Session - How to use EXCEL ?
SAM Training Session - How to use EXCEL ?
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptxReal-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
Real-time Tracking and Monitoring with Cargo Cloud Solutions.pptx
 

NoSQL? No, SQL! - SQL, the underestimated "Big Data" technology

  • 1. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL, the underestimated “Big Data” technology
  • 2. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! No – tation Seen at the 2013 O’Reilly Strata Conf: History of NoSQL by Mark Madsen. Picture published by Edd Dumbill
  • 3. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! NoSQL? NoSQL? No, SQL!
  • 4. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Our vision at Data Geekery - SQL dominates database systems - SQL is very expressive - SQL is very type safe SQL is a device whose mystery is only exceeded by its power!
  • 5. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Me – @lukaseder Java developers can get back in control of SQL with jOOQ - Head of R&D at Data Geekery GmbH - SQL Aficionado - Java Aficionado
  • 6. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Big Data? NoSQL? - You’re giving up on ACID - You’re giving up on type safety - You’re giving up on standards - You’re giving up on tooling - You’re giving up on relational algebra - You haven’t asked operations - You don’t actually have «Big Data»
  • 7. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Big Data? NoSQL? - You’re giving up on ACID - You’re giving up on type safety - You’re giving up on standards - You’re giving up on tooling - You’re giving up on relational algebra - You haven’t asked operations - You don’t actually have «Big Data»
  • 8. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Also Not SQL @Entity @Table(name = "EVENTS") public class Event { private Long id; private String title; private Date date; @Id @GeneratedValue(generator = "increment") @GenericGenerator(name = "increment", strategy = "increment") public Long getId() { /* … */ } @Temporal(TemporalType.TIMESTAMP) @Column(name = "EVENT_DATE") public Date getDate() { /* … */ }
  • 9. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Also Not SQL – Annotatiomania™ @OneToMany(mappedBy = "destCustomerId") @ManyToMany @Fetch(FetchMode.SUBSELECT) @JoinTable( name = "customer_dealer_map", joinColumns = { @JoinColumn(name = "customer_id", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "dealer_id", referencedColumnName = "id") } ) private Collection dealers; Found at http://stackoverflow.com/q/17491912/521799
  • 10. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Also Not SQL – JPA 3.0 Preview @OneToMany @OneToManyMore @AnyOne @AnyBody @ManyToMany @Many @Fetch @FetchMany @FetchWithDiscriminator(name = "no_name") @JoinTable(joinColumns = { @JoinColumn(name = "customer_id", referencedColumnName = "id") }) @PrefetchJoinWithDiscriminator @IfJoiningAvoidHashJoins @ButUseHashJoinsWhenMoreThan(records = 1000) @XmlDataTransformable @SpringPrefechAdapter private Collection employees; Might not be true
  • 11. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Shocker! You can now write SQL in Java. JDBC
  • 12. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL in Java 7 – JDBC try (PreparedStatement stmt = c.prepareStatement(sql); ResultSet rs = stmt.executeQuery()) { while (rs.next()) { System.out.println( new Schema(rs.getString("SCHEMA_NAME"), rs.getBoolean("IS_DEFAULT")) ); } }
  • 13. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL in Java 8 – jOOQ DSL.using(c) .fetch(sql) .map(rs -> new Schema( rs.getValue("SCHEMA_NAME", String.class), rs.getValue("IS_DEFAULT", boolean.class) )) .forEach(System.out::println);
  • 14. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Typesafe SQL in Java – jOOQ DSL.using(c) .select(s.SCHEMA_NAME, s.IS_DEFAULT) .from(INFORMATION_SCHEMA.SCHEMATA.as("s")) .orderBy(s.SCHEMA_NAME) .map(rs -> new Schema( rs.getValue(s.SCHEMA_NAME), rs.getValue(s.IS_DEFAULT) )) .forEach(System.out::println);
  • 15. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL in Java 8 – Spring JDBC new JdbcTemplate( new SingleConnectionDataSource(c, true)) .query(sql, (rs, rowNum) -> new Schema( rs.getString("SCHEMA_NAME"), rs.getBoolean("IS_DEFAULT") )) .forEach(System.out::println);
  • 16. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL in Java 8 – Apache DbUtils new QueryRunner() .query(c, sql, new ArrayListHandler()) .stream() .map(array -> new Schema( (String) array[0], (Boolean) array[1] )) .forEach(System.out::println);
  • 17. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL in Groovy sql.eachRow( 'select * from tableName' ) { println "$it.id -- ${it.firstName} --" }
  • 18. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! When you should use SQL – indicators - You need JOINs, UNIONs - You need functions, aggregations - You need bulk reads / writes Calculations should be done close to the data
  • 19. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Please, run that calculation in your DB
  • 20. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – NULL -- What does this query return? SELECT 1 AS a FROM dual WHERE 1 IN (NULL) UNION ALL SELECT 2 AS a FROM dual WHERE NOT(1 IN (NULL))
  • 21. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – NULL -- What does this query return? SELECT 1 AS a FROM dual WHERE 1 IN (NULL) UNION ALL SELECT 2 AS a FROM dual WHERE NOT(1 IN (NULL))
  • 22. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – NULL -- Nothing! It’s the same as this SELECT 1 AS a FROM dual WHERE 1 = NULL UNION ALL SELECT 2 AS a FROM dual WHERE 1 != NULL
  • 23. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – NULL -- Nothing! It’s the same as this SELECT 1 AS a FROM dual WHERE “UNKNOWN” UNION ALL SELECT 2 AS a FROM dual WHERE “UNKNOWN”
  • 24. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – Oracle VARCHAR2 -- What does this query return? SELECT 1 AS a FROM dual WHERE '' = '' UNION ALL SELECT 2 AS a FROM dual WHERE 'a' != ''
  • 25. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – Oracle VARCHAR2 -- Nope! Nothing again (only in Oracle). SELECT 1 AS a FROM dual WHERE NULL = NULL UNION ALL SELECT 2 AS a FROM dual WHERE 'a' != NULL
  • 26. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SQL Trivia – Oracle VARCHAR2 -- Nope! Nothing again (only in Oracle). SELECT 1 AS a FROM dual WHERE NULL = NULL UNION ALL SELECT 2 AS a FROM dual WHERE 'a' != NULL
  • 27. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Stockholm Syndrome: We love JavaScript SQL
  • 28. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Winston Churchill: SQL is the worst form of database querying, except for all the other forms.
  • 29. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Let’s calculate a running total SELECT * FROM v_transactions WHERE account_id = 1 ORDER BY value_date DESC, id DESC
  • 30. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | |------|------------|--------| | 9997 | 2014-03-18 | 99.17 | | 9981 | 2014-03-16 | 71.44 | | 9979 | 2014-03-16 | -94.60 | | 9977 | 2014-03-16 | -6.96 | | 9971 | 2014-03-15 | -65.95 |
  • 31. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | 99.17 | 19985.81 | | 9981 | 2014-03-16 | 71.44 | 19886.64 | | 9979 | 2014-03-16 | -94.60 | 19815.20 | | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 32. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | +99.17 =19985.81 | | 9981 | 2014-03-16 | 71.44 | +19886.64 | | 9979 | 2014-03-16 | -94.60 | 19815.20 | | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 33. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | 99.17 | 19985.81 | | 9981 | 2014-03-16 | +71.44 =19886.64 | | 9979 | 2014-03-16 | -94.60 | +19815.20 | | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 34. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Let’s calculate a running total | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 | 99.17 | 19985.81 | | 9981 | 2014-03-16 | +71.44 =19886.64 | n | 9979 | 2014-03-16 | -94.60 | +19815.20 | n+1 | 9977 | 2014-03-16 | -6.96 | 19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |BALANCE(ROWn) = BALANCE(ROWn+1) + AMOUNT(ROWn) BALANCE(ROWn+1) = BALANCE(ROWn) – AMOUNT(ROWn)
  • 35. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! How can we do it?
  • 36. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! How can we do it? - In Java - Calculate on UPDATE - Nested SELECT - Recursive SQL - Window functions - MODEL clause (Oracle) - Stored procedures
  • 37. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! How can we do it? – With SQL! - In Java - Calculate on UPDATE - Nested SELECT - Recursive SQL - Window functions - MODEL clause (Oracle) - Stored procedures
  • 38. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Using nested SELECTs
  • 39. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT t1.*, t1.current_balance - ( SELECT NVL(SUM(amount), 0) FROM v_transactions t2 WHERE t2.account_id = t1.account_id AND (t2.value_date, t2.id) > (t1.value_date, t1.id) ) AS balance FROM v_transactions t1 WHERE t1.account_id = 1 ORDER BY t1.value_date DESC, t1.id DESC
  • 40. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT t1.*, t1.current_balance - ( SELECT NVL(SUM(amount), 0) FROM v_transactions t2 WHERE t2.account_id = t1.account_id AND (t2.value_date, t2.id) > (t1.value_date, t1.id) ) AS balance FROM v_transactions t1 WHERE t1.account_id = 1 ORDER BY t1.value_date DESC, t1.id DESC
  • 41. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT t1.*, t1.current_balance - ( SELECT NVL(SUM(amount), 0) FROM v_transactions t2 WHERE t2.account_id = t1.account_id AND ((t2.value_date > t1.value_date) OR (t2.value_date = t1.value_date AND t2.id > t1.id)) ) AS balance FROM v_transactions t1 WHERE t1.account_id = 1 ORDER BY ...
  • 42. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Using nested SELECTs | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 -(99.17)| +19985.81 | | 9981 | 2014-03-16 -(71.44)| 19886.64 | | 9979 | 2014-03-16 -(-94.60)| 19815.20 | | 9977 | 2014-03-16 | -6.96 | =19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 43. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! | Id | Operation | Name | A-Rows | A-Time | ------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 50 |00:00:00.77 | | 1 | SORT AGGREGATE | | 1101 |00:00:00.76 | |* 2 | TABLE ACCESS BY INDEX ROWID | T_TRANSACTIONS | 605K|00:00:00.69 | |* 3 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 1212K|00:00:00.21 | | 4 | SORT ORDER BY | | 50 |00:00:00.77 | | 5 | NESTED LOOPS | | 1101 |00:00:00.01 | | 6 | TABLE ACCESS BY INDEX ROWID| T_ACCOUNTS | 1 |00:00:00.01 | |* 7 | INDEX UNIQUE SCAN | SYS_C006991 | 1 |00:00:00.01 | | 8 | TABLE ACCESS BY INDEX ROWID| T_TRANSACTIONS | 1101 |00:00:00.01 | |* 9 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 1101 |00:00:00.01 | ------------------------------------------------------------------------------
  • 44. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Using recursive SQL
  • 45. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! We need to number transactions | ID | VALUE_DATE | AMOUNT | TRANSACTION_NR | |------|------------|--------|----------------| | 9997 | 2014-03-18 | 99.17 | 1 | | 9981 | 2014-03-16 | 71.44 | 2 | | 9979 | 2014-03-16 | -94.60 | 3 | | 9977 | 2014-03-16 | -6.96 | 4 | | 9971 | 2014-03-15 | -65.95 | 5 |
  • 46. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! CREATE OR REPLACE VIEW v_transactions_by_time AS SELECT t.*, ROW_NUMBER() OVER ( PARTITION BY account_id ORDER BY t.value_date DESC, t.id DESC ) AS transaction_number FROM v_transactions t;
  • 47. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! WITH ordered_with_balance ( account_id, value_date, amount, balance, transaction_number ) AS ( SELECT t1.account_id, t1.value_date, t1.amount, t1.current_balance, t1.transaction_number FROM v_transactions_by_time t1 WHERE t1.transaction_number = 1 UNION ALL SELECT t1.account_id, t1.value_date, t1.amount, t2.balance - t2.amount, t1.transaction_number FROM ordered_with_balance t2 JOIN v_transactions_by_time t1 ON t1.transaction_number = t2.transaction_number + 1 AND t1.account_id = t2.account_id ) SELECT * FROM ordered_with_balance WHERE account_id = 1 ORDER BY transaction_number ASC
  • 48. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! WITH ordered_with_balance ( account_id, value_date, amount, balance, transaction_number ) AS ( SELECT t1.account_id, t1.value_date, t1.amount, t1.current_balance, t1.transaction_number FROM v_transactions_by_time t1 WHERE t1.transaction_number = 1 UNION ALL SELECT t1.account_id, t1.value_date, t1.amount, t2.balance - t2.amount, t1.transaction_number FROM ordered_with_balance t2 JOIN v_transactions_by_time t1 ON t1.transaction_number = t2.transaction_number + 1 AND t1.account_id = t2.account_id ) SELECT * FROM ordered_with_balance WHERE account_id = 1 ORDER BY transaction_number ASC
  • 49. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! WITH ordered_with_balance ( account_id, value_date, amount, balance, transaction_number ) AS ( SELECT t1.account_id, t1.value_date, t1.amount, t1.current_balance, t1.transaction_number FROM v_transactions_by_time t1 WHERE t1.transaction_number = 1 UNION ALL SELECT t1.account_id, t1.value_date, t1.amount, t2.balance - t2.amount, t1.transaction_number FROM ordered_with_balance t2 JOIN v_transactions_by_time t1 ON t1.transaction_number = t2.transaction_number + 1 AND t1.account_id = t2.account_id ) SELECT * FROM ordered_with_balance WHERE account_id = 1 ORDER BY transaction_number ASC
  • 50. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! --------------------------------------------------------------------------------------------------- | Id | Operation | Name | A-Rows | A-Time | --------------------------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 50 |00:00:35.29 | | 1 | SORT ORDER BY | | 50 |00:00:35.29 | |* 2 | VIEW | | 1101 |00:00:35.29 | | 3 | UNION ALL (RECURSIVE WITH) BREADTH FIRST| | 9999 |00:00:35.28 | |* 4 | VIEW | V_TRANSACTIONS_BY_TIME | 9 |00:00:00.03 | |* 5 | WINDOW SORT PUSHED RANK | | 18 |00:00:00.03 | | 6 | NESTED LOOPS | | 9999 |00:00:00.01 | | 7 | NESTED LOOPS | | 9999 |00:00:00.01 | | 8 | TABLE ACCESS FULL | T_ACCOUNTS | 10 |00:00:00.01 | |* 9 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 9999 |00:00:00.01 | | 10 | TABLE ACCESS BY INDEX ROWID | T_TRANSACTIONS | 9999 |00:00:00.01 | |* 11 | HASH JOIN | | 9990 |00:00:35.08 | | 12 | VIEW | V_TRANSACTIONS_BY_TIME | 11M|00:00:29.13 | | 13 | WINDOW SORT | | 11M|00:00:27.19 | | 14 | NESTED LOOPS | | 11M|00:00:13.62 | | 15 | NESTED LOOPS | | 11M|00:00:03.89 | | 16 | INDEX FAST FULL SCAN | SYS_C006991 | 11450 |00:00:00.06 | |* 17 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 11M|00:00:02.18 | | 18 | TABLE ACCESS BY INDEX ROWID | T_TRANSACTIONS | 11M|00:00:06.15 | | 19 | RECURSIVE WITH PUMP | | 9999 |00:00:00.01 | ---------------------------------------------------------------------------------------------------
  • 51. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Using window functions
  • 52. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT t.*, t.current_balance - NVL( SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), 0) AS balance FROM v_transactions t WHERE t.account_id = 1 ORDER BY t.value_date DESC, t.id DESC
  • 53. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT t.*, t.current_balance - NVL( SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), 0) AS balance FROM v_transactions t WHERE t.account_id = 1 ORDER BY t.value_date DESC, t.id DESC
  • 54. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Using window functions | ID | VALUE_DATE | AMOUNT | BALANCE | |------|------------|--------|------------| | 9997 | 2014-03-18 -(99.17)| +19985.81 | | 9981 | 2014-03-16 -(71.44)| 19886.64 | | 9979 | 2014-03-16 -(-94.60)| 19815.20 | | 9977 | 2014-03-16 | -6.96 | =19909.80 | | 9971 | 2014-03-15 | -65.95 | 19916.76 |
  • 55. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! ------------------------------------------------------------------------------ | Id | Operation | Name | A-Rows | A-Time | ------------------------------------------------------------------------------ | 0 | SELECT STATEMENT | | 50 |00:00:00.01 | | 1 | WINDOW SORT | | 50 |00:00:00.01 | | 2 | NESTED LOOPS | | 1101 |00:00:00.01 | | 3 | TABLE ACCESS BY INDEX ROWID| T_ACCOUNTS | 1 |00:00:00.01 | |* 4 | INDEX UNIQUE SCAN | SYS_C006991 | 1 |00:00:00.01 | | 5 | TABLE ACCESS BY INDEX ROWID| T_TRANSACTIONS | 1101 |00:00:00.01 | |* 6 | INDEX RANGE SCAN | I_TRX_ACCO_ID | 1101 |00:00:00.01 | ------------------------------------------------------------------------------
  • 56. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Using the Oracle MODEL clause
  • 57. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT account_id, value_date, amount, balance FROM ( SELECT id, account_id, value_date, amount, current_balance AS balance FROM v_transactions ) t WHERE account_id = 1 MODEL PARTITION BY (account_id) DIMENSION BY ( ROW_NUMBER() OVER (ORDER BY value_date DESC, id DESC) AS rn ) MEASURES (value_date, amount, balance) RULES ( balance[rn > 1] = balance[cv(rn) - 1] - amount[cv(rn) - 1] ) ORDER BY rn ASC
  • 58. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT account_id, value_date, amount, balance FROM ( SELECT id, account_id, value_date, amount, current_balance AS balance FROM v_transactions ) t WHERE account_id = 1 MODEL PARTITION BY (account_id) DIMENSION BY ( ROW_NUMBER() OVER (ORDER BY value_date DESC, id DESC) AS rn ) MEASURES (value_date, amount, balance) RULES ( balance[rn > 1] = balance[cv(rn) - 1] - amount[cv(rn) - 1] ) ORDER BY rn ASC
  • 59. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! RULES ( balance[rn > 1] = balance[cv(rn) - 1] - amount [cv(rn) - 1] ) -- does it look familiar?
  • 60. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! -------------------------------------------------------------------------------- | Id | Operation | Name | A-Rows | A-Time | -------------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 50 |00:00:00.02 | | 1 | SORT ORDER BY | | 50 |00:00:00.02 | | 2 | SQL MODEL ORDERED | | 1101 |00:00:00.02 | | 3 | WINDOW SORT | | 1101 |00:00:00.01 | | 4 | NESTED LOOPS | | 1101 |00:00:00.01 | | 5 | TABLE ACCESS BY INDEX ROWID| T_ACCOUNTS | 1 |00:00:00.01 | |* 6 | INDEX UNIQUE SCAN | SYS_C006991 | 1 |00:00:00.01 | |* 7 | TABLE ACCESS FULL | T_TRANSACTIONS | 1101 |00:00:00.01 | --------------------------------------------------------------------------------
  • 61. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! The MODEL clause is Oracle’s most powerful and underused feature
  • 62. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Our vision at Data Geekery - Revisited - SQL dominates database systems - SQL is expressive - SQL is type safe SQL is a device whose mystery is only exceeded by its power!
  • 63. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Our vision at Data Geekery - Revisited - SQL dominates database systems - SQL is expressive - SQL is type safe
  • 64. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Our vision at Data Geekery - Revisited jOOQ is the best way to write SQL in Java
  • 65. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! SELECT t.*, t.current_balance - NVL( SUM(t.amount) OVER ( PARTITION BY t.account_id ORDER BY t.value_date DESC, t.id DESC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING ), 0) AS balance FROM v_transactions t WHERE t.account_id = 1 ORDER BY t.value_date DESC, t.id DESC
  • 66. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! DSL.using(connection) .select(t.VALUE_DATE, t.AMOUNT, t.CURRENT_BALANCE.sub( sum(t.AMOUNT).over( partitionBy(t.ACCOUNT_ID) .orderBy (t.VALUE_DATE.desc(), t.ID .desc()) .rowsBetweenUnboundedPreceding() .andPreceding(1) ) ).nvl(0).as("balance")) .from (V_TRANSACTIONS.as("t")) .where (t.ACCOUNT_ID.eq(1)) .orderBy(t.VALUE_DATE.desc(), t.ID .desc())
  • 67. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Thank you 3-month jOOQ Enterprise trial: • Send «JUGS-LU-SQL-2014» to sales@datageekery.com More free Java / SQL knowledge on: • Blog: http://blog.jooq.org • Twitter: @JavaOOQ
  • 68. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! There is SQL before and after window functions This just in… (in case you haven’t seen enough)
  • 69. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Image Copyright © fanpictor.com Use-case: Choreo export
  • 70. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Use-case: Choreo export as Excel
  • 71. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Use-case: Choreo export as Excel
  • 72. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Use-case: Choreo export as Excel
  • 73. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! WITH data AS (SELECT d.*, row(sector, row, scene1, scene2) block FROM d) SELECT data.*, CASE WHEN LAG (block) OVER (o) IS DISTINCT FROM block AND LEAD(block) OVER (o) IS DISTINCT FROM block THEN 'start / stop' WHEN LAG (block) OVER (o) IS DISTINCT FROM block THEN 'start' WHEN LEAD(block) OVER (o) IS DISTINCT FROM block THEN 'stop' ELSE '' END start_stop, COUNT(*) OVER (PARTITION BY sector, row, scene1, scene2) FROM data WINDOW o AS (ORDER BY sector, row, seat) ORDER BY sector, row, seat Full example: http://blog.jooq.org/2014/04/15/how-to-do-this-with-sql-of-course
  • 74. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Full example: http://blog.jooq.org/2014/04/15/how-to-do-this-with-sql-of-course WITH data AS (SELECT d.*, row(sector, row, scene1, scene2) block FROM d) SELECT data.*, CASE WHEN LAG (block) OVER (o) IS DISTINCT FROM block AND LEAD(block) OVER (o) IS DISTINCT FROM block THEN 'start / stop' WHEN LAG (block) OVER (o) IS DISTINCT FROM block THEN 'start' WHEN LEAD(block) OVER (o) IS DISTINCT FROM block THEN 'stop' ELSE '' END start_stop, COUNT(*) OVER (PARTITION BY sector, row, scene1, scene2) FROM data WINDOW o AS (ORDER BY sector, row, seat) ORDER BY sector, row, seat We can compare rows with each other, not only columns!
  • 75. Copyright (c) 2009-2014 by Data Geekery GmbH. Slides licensed under CC BY SA 3.0 NoSQL? No, SQL! Full example: http://blog.jooq.org/2014/04/15/how-to-do-this-with-sql-of-course WITH data AS (SELECT d.*, row(sector, row, scene1, scene2) block FROM d) SELECT data.*, CASE WHEN LAG (block) OVER (o) IS DISTINCT FROM block AND LEAD(block) OVER (o) IS DISTINCT FROM block THEN 'start / stop' WHEN LAG (block) OVER (o) IS DISTINCT FROM block THEN 'start' WHEN LEAD(block) OVER (o) IS DISTINCT FROM block THEN 'stop' ELSE '' END start_stop, COUNT(*) OVER (PARTITION BY sector, row, scene1, scene2) FROM data WINDOW o AS (ORDER BY sector, row, seat) ORDER BY sector, row, seat We can reuse window specifications!