~/devreads

#oracle

45 posts

18 Mar 2025

vladmihalcea 1 min read

Introduction In this article, I’m going to explain how to map the Oracle TIMESTAMP WITH TIME ZONE with JPA and Hibernate. While the DATE and TIMESTAMP column types are suitable for mapping the LocalDateTime, the TIMESTAMP WITH TIME ZONE column type allows us to map both the ZonedDateTime and OffsetDateTime. Oracle TIMESTAMP WITH TIME ZONE Traditionally, Oracle provided the DATE…

hibernatejpaoffsetdatetimeoracletimezone

24 Jul 2024

vladmihalcea 1 min read

Introduction In this article, we are going to see how we can use symbolic links to move the DB data folder. The reason why I needed to move the data folder from the C to the D Windows partition was because the C partition was running out of disk space. DB data folder A relational database system requires to store…

databaseoraclesymbolic links

18 Apr 2024

vladmihalcea 1 min read

Introduction In this article, we are going to investigate how to calculate percentiles with the SQL PERCENTILE_CONT function. Domain Model Let’s consider we have the following quotes table that holds the historical price values of various stocks and indexes: The quotes table is populated with data that looks like this: SQL PERCENTILE_CONT The SQL Standard provides support for a great…

sqloraclepercentilespercentile contpostgresql

1 Mar 2024

lukaseder 1 min read

I’ve blogged about generic ways of getting top 1 or top n per category queries before on this blog. An Oracle specific version in that post used the arcane KEEP syntax: This is a bit difficult to read when you see it for the first time. Think of it as a complicated way to say … Continue reading Getting Top…

sqlaggregate functionsany valuekeeporacle

19 Dec 2023

vladmihalcea 1 min read

Introduction In this article, we are going to see how the Hibernate StatelessSession Upsert method works. As I explained in this article, the UPSERT operation allows you to INSERT a record if there is no such record matching the filtering criteria or to UPDATE it in case the record exists. While many relational database systems offer the standard MERGE command…

hibernatemysqloraclepostgresqlsql server

17 May 2023

vladmihalcea 1 min read

Introduction In this article, we are going to see what is the standard SQL operation order. Once you understand the order in which SQL operations are executed, then it will be clear why the Oracle legacy pagination query required a Derived Table in order to make sure that the ROWNUM pseudocolumn is computed after executing the ORDER BY clause. SQL…

sqlmysqloraclepostgresqlsql server

21 Oct 2022

lukaseder 1 min read

Using jOOQ’s code generator to call stored procedures is a popular reason to use jOOQ. For example, when you have a procedure like the following Oracle PL/SQL procedure: jOOQ will generate code for you to call very simply, like this: This will execute the following, taking care of binding all IN and OUT parameters for … Continue reading Calling Procedures…

jooq-in-usedefault parametersjooqoraclepl pgsql

28 Jul 2022

lukaseder 1 min read

jOOQ is mainly known for its powerful type safe, embedded, dynamic SQL capabilities that are made available through code generation. However, a secondary use case of code generation is to use it for stored procedures (possibly exclusively for stored procedures). Stored procedures are powerful ways of moving complex data processing logic to the server. This … Continue reading The Best…

jooq-in-usecode generationjdbcjooqoracle

24 Apr 2019

lukaseder 1 min read

I found a very interesting SQL question on Twitter recently: Hi @sfonplsql we have some scenario, Let us 01Jan Mkt Value 100, 02Jan 120, next entry available 25th Jan 125, from 3rd Jan 24 Jan, our value should be 120. How to arrive ? Thanks @oraclebase — Vikki (@vikkiarul) April 23, 2019 Rephrasing the question: … Continue reading Using IGNORE…

sqlignore nullslast valueoraclesql standard

16 Apr 2019

22 Jan 2019

lukaseder 1 min read

B-Tree indexes are perfect when your data is uniformly distributed. They are not really useful, when you have skewed data. I’ll explain later why this is the case, but let’s first learn how to detect “skew” What is skew? Skew is a term from statistics when a normal distribution is not symmetric. The example given … Continue reading Calculate Percentiles…

sqlinverse distribution functionoraclepercentilespercentile cont

7 Sept 2018

lukaseder 1 min read

I recently stumbled upon a curious query on a customer’s productive Oracle database: Two things caught my attention: The query was executed many billions of times per month, accounting for about 0.3% of that system’s load. That’s 0.3% for something extremely silly! I don’t think that customer would ever qualify the DUAL table as SYS.DUAL, … Continue reading Beware of…

sqlcontext switchoracleperformancepl sql

18 Dec 2017

lukaseder 1 min read

Or: Move That Loop into the Server Already! This article will illustrate the significance of something that I always thought to be common sense, but I keep seeing people getting this (very) wrong in their productive systems. Chances are, in fact, that most applications out there suffer from this performance problem – and the fix … Continue reading The Cost…

sqlclient-serverdistributed systemsoracleperformance

1 Jun 2017

lukaseder 1 min read

The following two indexes are redundant in most SQL databases: It is usually safe to drop the first index, because all queries that query the LAST_NAME column only can still profit from the second index I_ACTOR_2. The reason being that LAST_NAME is the first column of the composite index I_ACTOR_2 (it would be a different … Continue reading How to…

sqlindexingoracleperformancepostgresql

31 May 2017

lukaseder 1 min read

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 different predicate. Preferably in a single query. Challenge accepted! Canonical Idea: Use a Common Table Expression We’re querying the Sakila … Continue reading How to…

sqlcommon table expressionoraclepostgresqlsql performance

8 May 2017

lukaseder 1 min read

A large-ish customer in banking (largest tables on that particular system: ~1 billion rows) once decided to separate the OLTP database from the “log database” in order to better use resources and prevent contention on some tables, as the append-only log database is used heavily for analytic querying of all sorts. That seems to make … Continue reading The Difficulty…

sqldatabase linksoracleremote databasesql performance

31 Mar 2017

lukaseder 1 min read

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 only then we can get its plan. I’ve blogged about this previously, here. However, thanks to lateral unnesting, … Continue reading How to…

sqlexecution planslaterallateral unnestingoracle

29 Mar 2017

lukaseder 1 min read

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 fast”: Some of these bullets were already covered on this blog. For instance avoiding needless, mandatory work, … Continue reading How to…

sqlbenchmarkingoraclepostgresqlsql benchmarking

7 Mar 2017

lukaseder 1 min read

There are already only very few real-world use-cases for FULL [ OUTER ] JOIN, but maybe, you have run into this beast in the past. But when was the last time you’ve seen a NATURAL JOIN? Right. A quick reminder from our article about JOINs: FULL JOIN A FULL JOIN is a type of OUTER … Continue reading Impress Your…

sqlfull joinfull outer joinnatural joinoracle

10 Feb 2017

lukaseder 1 min read

Sometimes, SQL can just be so beautiful. One of the less mainstream features in SQL is the array type (or nested collections). In fact, it’s so not mainstream that only 2 major databases actually support it: Oracle and PostgreSQL (and HSQLDB and H2 in the Java ecosystem). In PostgreSQL, you can write: Or in Oracle: … Continue reading Beautiful SQL:…

sqlarraylateralnested collectionsoracle

18 Jan 2017

lukaseder 1 min read

A very interesting feature of the SQL Server and PostgreSQL databases (and some others, including SQLite) is the partial index (sometimes also called “filtered index”). That’s an index that contains only “parts” of the table data. For instance, we can write the following index in SQL Server and PostgreSQL: Let’s imagine you have a house … Continue reading How to…

sqlindexoptimisationoraclepartial index

8 Nov 2016

lukaseder 1 min read

Some of the biggest limitations when working with Oracle PL/SQL from Java is the lack of support for a variety of PL/SQL features through the JDBC interface. This lack of support is actually not limited to JDBC, but also extends to Oracle SQL. For instance, if you’re using the useful PL/SQL BOOLEAN type as such: … Continue reading Use jOOQ…

jooq-developmentsqljooqoraclepl sql

1 Nov 2016

lukaseder 1 min read

I’ve recently discovered a rather significant performance issue on a productive Oracle 11g customer database. And I’m sure you have this issue too, which is why I’m documenting it here. This is a simplified representation of the setup at the customer site: ID PAYMENT_DATE TEXT ---------- ------------ ----------------------------------- 33803 21.05.16 DcTNBOrkQIgMtbietUWOsSFNMIqGLlDw... 29505 09.03.16 VIuPaOAQqzCMlFBYPQtvqUSbWYPDndJD... 10738 … Continue reading Why You…

sqldate time arithmeticindexingoracleperformance

21 Oct 2016

lukaseder 1 min read

Are you working with someone else’s schema and they haven’t declared nice names for all their constraints? Unfortunately, it is all too easy to create a table like this: Or like this: Sure, you get a little convenience when writing the table. But from now on, you’re stuck with weird, system generated names both for … Continue reading How to…

sqlconstraint namesconstraintsexecution plansoracle

20 Oct 2016

lukaseder 1 min read

Everyone who writes complex SQL (or dare I say, move business logic into the database?) wishes for parameterised views from time to time. This hypothetical syntax would be terrific: Or as user-defined functions if you insist (as available in SQL Server): As always on this blog, I’m using the useful Sakila database for examples. The … Continue reading Be Careful…

sqloracleparameterised viewssys context

5 Oct 2016

lukaseder 1 min read

In my SQL Masterclass, I frequently remind participants of the fact how important statistics are for a modern cost based optimiser. For instance, if you consider the fact that in an average E-Banking system’s bookings table, you will probably have a transaction amount histogram like the following: In other words, most of your transactions are … Continue reading Why You…

sqlcbocost based optimizeroptimizeroracle

14 Sept 2016

lukaseder 1 min read

A while ago, I blogged about the importance of avoiding unnecessary COUNT(*) queries:https://blog.jooq.org/sql-tip-of-the-day-be-wary-of-select-count … and how to replace them with equivalent EXISTS queries As I’m updating the SQL training to show also PostgreSQL performance characteristics in addition to Oracle, I really have to reiterate this topic. Please repeat after me: Thou shalt not use COUNT(*) … Continue reading Avoid Using…

sqlcountexistsoracleperformance

15 Jul 2016

lukaseder 1 min read

It seems that perfection is attained not when there is nothing more to add, but when there is nothing more to remove. – Antoine de Saint Exupéry in Terre des Hommes As SQL developers, we keep adding more and more indexes to our tables. Every time we run new queries that are potentially slow, a … Continue reading How to…

sqlindexingoperationsoracle

14 Jul 2016

lukaseder 1 min read

A lot of people use SQL constraints mainly to enforce data integrity, and that’s already a very good thing. A UNIQUE constraint, for instance, makes sure that there is at most one instance of any possible value (or tuple, in the case of a composite constraint) in a table. For instance: Constraints are also good … Continue reading How Adding…

sqlconstraintsoracleperformanceunique constraint

18 Jan 2016

lukaseder 1 min read

I’ve recently encountered a very interesting question on Stack Overflow by an unnamed user. The question was about generating a table of the following form in Oracle, using a table valued function: Description COUNT ------------------- TEST1 10 TEST2 15 TEST3 25 TEST4 50 The logic that should be implemented for the COUNT column is the … Continue reading Impress Your…

sqloraclepivotsql serverunpivot

30 Dec 2015

lukaseder 1 min read

Like many old databases, Oracle has legacy data types, which are rather nasty to work with in every day SQL. Usually, you don’t run into wild encounters of LONG and LONG RAW data types anymore, but when you’re working with an old database, or with the dictionary views, you might just have to deal with … Continue reading Oracle LONG…

sqljooqlonglong raworacle

17 Dec 2015

lukaseder 1 min read

The following is a very common problem in all data related technologies and we’re going to look into two very lean, SQL-based solutions for it: How do I fill the cells of a sparse data set with the “previous non-empty value”? The problem The problem is really simple and I’m reusing the example provided by … Continue reading How to…

sqlmodel clauseoraclesparse datawindow functions

8 Oct 2015

lukaseder 1 min read

Do you want to know real quick what kind of indexes there are on any given table in your Oracle schema? Nothing simpler than that. Just run the following query: The above query is ran against the Sakila database. Just replace the “FILM_ACTOR” table by your table and you’re all set. The result looks like: … Continue reading How to…

sqldictionary viewsindexesoracle

15 Sept 2015

lukaseder 1 min read

I’m seeing people do this all the time. They want to hammer a date or timestamp constant into their SQL query, and the only function they know is the TO_DATE() or TO_TIMESTAMP() date parsing function: As observed in this Stack Overflow question, for instance: Date parsing is important only if your date input is really … Continue reading Don’t Format…

sqldateoracletimestamp

17 Jun 2015

lukaseder 1 min read

Before you read on, please note that since jOOQ 3.19, policies are supported out of the box, to implement the same functionality in a much simpler way than what this article suggests! Some time ago, we’ve promised to follow up on our Constraints on Views article with a sequel showing how to implement client-side row-level … Continue reading Implementing Client-Side…

sqljooqoraclepostgresqlrow-level security

13 Apr 2015

lukaseder 1 min read

Syntax is one of those topics. One of those emotional topics that lead to very very very important discussions. I personally like PL/SQL. It is extremely verbose, and precise. It forces you to adhere to a very strong and rigid type system, slowing you down, which is likely to help you avoid mistakes. There is … Continue reading It’s the…

sqljavaoraclepl sqlsyntax

7 Apr 2015

lukaseder 1 min read

Sometimes you simply cannot avoid it: Pessimistic locking via SQL. In fact, it’s an awesome tool when you want to synchronise several applications on a shared, global lock. Some may think this is abusing the database. We think use the tools you have if they can solve the problem you have. For instance, the RDBMS … Continue reading How to…

sqlconcurrencyjoojooqoracle

26 Feb 2015

lukaseder 1 min read

This can happen ever so easily. You adapt a table by adding a new column: You go on, implementing your business logic – absolutely no problem. But then, later on (perhaps in production), some batch job fails because it makes some strong assumptions about data types. Namely, it assumes that the two tables payments and … Continue reading How to…

sqlcase expressionfilter clauseoraclepivot

18 Aug 2014

lukaseder 1 min read

Oracle SYNONYMs are a great feature. You can implement all sorts of backwards-compatibility tweaks simply by creating SYNONYMs in your database. Consider the following schema: Now you can query your same old table through three different names, it’ll all result in the same output: The trouble is, when you see my_table_bak in code (or some … Continue reading All You…

sqlcommon table expressionshierarchical sqloraclerecursive sql

5 Aug 2014

lukaseder 1 min read

Every once in a while, we run into these rare SQL issues where we’d like to do something that seems out of the ordinary. One of these things is pivoting rows to columns. A recent question on Stack Overflow by Valiante asked for precisely this. Going from this table: +------+------------+----------------+-------------------+ | dnId | propNameId | … Continue reading Are You…

sqloraclepivotsql server

14 Jul 2014

lukaseder 1 min read

Imagine you want to collect detailed usage statistics to tune your Oracle database, e.g. if you want to have A-Rows and A-Time values in your execution plans (by default, Oracle only reports E-Rows and E-Time with “E” for “Estimated”. But usually, you will care more about the “A” for “Actual”). All you have to do … Continue reading Logon Triggers:…

sqllogon triggersoraclestatisticsstatistics level

2 Jun 2014

lukaseder 1 min read

In most cases, simply creating an Oracle SEQUENCE with all defaults is good enough: This sequence can then be used immediately in triggers when inserting new records in a table: But if your table has heavy throughput with millions of insertions per day (e.g. a log table), you better configure the sequence cache correctly. The … Continue reading Don’t Forget…

sqlcacheoracleperformancesequences

12 May 2014

lukaseder 1 min read

Recently, at the office: Bob: I’ve looked into that slow query you’ve told me about yesterday, Alice. I’ve added the indexes you wanted. Everything should be fine now Alice: Thanks Bob. I’ll quickly check … Nope Bob, still slow, it didn’t seem to work Bob: You’re right Alice! It looks like Oracle isn’t picking up … Continue reading The Index…

sqlindexindex range scannulloracle

29 Apr 2014

lukaseder 1 min read

If anything at all, our jOOQ talks at various JUGs and conferences have revealed mostly one thing: Java developers don’t know SQL. And it isn’t even necessarily our fault. We’re just not exposed to SQL nowadays. But consider this: We developers (or our customers) are paying millions of dollars every year to Oracle, Microsoft, IBM, … Continue reading NoSQL? No,…

sqlanalytic functionsmodel clauseoracleperformance

17 Feb 2014

lukaseder 1 min read

Do you profile your code using JProfiler or YourKit? You should, because their licenses are worth every penny when you can find a very hidden and subtle bottleneck deep down in your application. For instance, the following chart shows nicely that there was a significant performance issue in jOOQ related to reflection: I’ll spare you … Continue reading Free Java…

javajava misison controljprofileroracleoracle java mission control