Faster SQL Through Occasionally Choosing Natural Keys Over Surrogate Keys
There are many many opinions out there regarding the old surrogate key vs. natural key debate. Most of the times, surrogate keys (e.g. sequence generated IDs) win because they’re much easier to design:...
View ArticleCreating Tables Dum and Dee in PostgreSQL
I was nerd-sniped: @lukaseder @danieldietrich @odersky why can't I represent table dee and table dum in SQL then? ¬_¬ — Benji Weber (@benjiweber) March 17, 2017 So tables dee and dum are two...
View ArticleHow to Benchmark Alternative SQL Queries to Find the Fastest Query
Tuning SQL isn’t always easy, and it takes a lot of practice to recognise how any given query can be optimised. One of the most important slides of my SQL training is the one summarising “how to be...
View ArticleSQL IN Predicate: With IN List or With Array? Which is Faster?
Hah! Got nerd-sniped again: http://stackoverflow.com/questions/43099226/how-to-make-jooq-to-use-arrays-in-the-in-clause/43102102 A jOOQ user was wondering why jOOQ would generate an IN list for a...
View ArticleHow to Fetch Multiple Oracle Execution Plans in One Nice Query
When looking at execution plans in Oracle, we’ll have to do several steps to be able to call the DBMS_XPLAN package functions. In fact, we have to find out the SQL_ID for a given statement first, and...
View ArticleHow to Calculate Multiple Aggregate Functions in a Single Query
At a customer site, I’ve recently encountered a report where a programmer needed to count quite a bit of stuff from a single table. The counts all differed in the way they used specific predicates. The...
View ArticleDon’t Use the String Concatenation “Trick” in SQL Predicates
In SQL, quite often, we want to compare several values with each other. For instance, when we’re looking for a specific user by their first and last names, we’ll write a query like this one: SELECT *...
View Article10 Nice Examples of Writing SQL in Kotlin With jOOQ
Kotlin is the next big thing. With Google announcing official support for Kotlin on Android, we’ll see a lot more traction for this lovely language. After today's #kotlin announcement, we're betting...
View ArticleHow to Generate at Least One Row in SQL
There are some situations where you would like to have at least one (empty) row in your result set in SQL. Imagine the following situation. We’re querying the Sakila database for actors and their...
View ArticleWhen to Use Bind Values, and When to Use Inline Values in SQL
Users of jOOQ, PL/SQL, T-SQL are spoiled as they hardly ever need to worry about bind values. Consider the following statements: Using jOOQ public int countActors(String firstName, String lastName) {...
View ArticleHow to Execute a SQL Query Only if Another SQL Query has no Results
I stumbled upon an interesting question on Stack Overflow recently. A user wanted to query a table for a given predicate. If that predicate returns no rows, they wanted to run another query using a...
View ArticleHow to Find Redundant Indexes in SQL
The following two indexes are redundant in most SQL databases: CREATE INDEX i_actor_1 ON actor (last_name); CREATE INDEX i_actor_2 ON actor (last_name, first_name); It is usually safe to drop the first...
View ArticleHow to Use SQL INTERSECT to Work Around SQL’s NULL Logic
ANOTHER SQL Post this week? I got nerd-sniped: Lazy Internet: In Oracle, how do i query for where (A, B, C) in (('a', 'b', null), 'a', null, 'c') considering Oracle's null handling? — Rafael...
View ArticlejOOQ Tuesdays: Gerald Sangudi and Keshav Murthy Reveal the Secrets of N1QL...
Welcome to the jOOQ Tuesdays series. In this series, we’ll publish an article on the third Tuesday every other month where we interview someone we find exciting in our industry from a jOOQ perspective....
View ArticleORMs Should Update “Changed” Values, Not Just “Modified” Ones
In this article, I will establish how the SQL language and its implementations distinguish between changed values and modified values, where a changed value is a value that has been “touched”, but not...
View Article5 Things You May Not Have Known About jOOQ
jOOQ has been around for a while now (since 2009!) and by now we can say we’ve seen quite a bit of things about the SQL and Java languages. Some of our design decisions are particular in the way jOOQ...
View ArticleFinding all Palindromes Contained in Strings with SQL
SQL is a really cool language. I can write really complex business logic with this logic programming language. I was again thrilled about SQL recently, at a customer site: Writing some nifty SQL...
View ArticleJOIN Elimination: An Essential Optimiser Feature for Advanced SQL Usage
The SQL language has one great advantage over procedural, object oriented, and “ordinary” functional programming languages. The fact that it is truly declarative (i.e. a 4GL / fourth generation...
View ArticleHow to Write Efficient TOP N Queries in SQL
A very common type of SQL query is the TOP-N query, where we need the “TOP N” records ordered by some value, possibly per category. In this blog post, we’re going to look into a variety of different...
View Article10 Cool SQL Optimisations That do not Depend on the Cost Model
Cost Based Optimisation is the de-facto standard way to optimise SQL queries in most modern databases. It is the reason why it is really really hard to implement a complex, hand-written algorithm in a...
View Article