Seize the deal!
Caching Best Practices
Imagine having a tool that can automatically detect if you are using JPA and Hibernate properly. Hypersistence Optimizer is that tool!
If you are only using the SQL-92 language reference, then you are overlooking so many great features like:
Our time series data consists of a total request count and a data recording time stamp:
I want to calculate the arrival velocity which can be defined as:
λ = arrival_velocity = Δcount / Δt
For each time event, we need to subtract the current and previous count and timestamp values.
Window functions allow us to aggregate/reference previous/next rows without restricting the SELECT clause to a single result row:
SELECT t as "Current time stamp", prev_t as "Previous time stamp", current_count as "Current total request count", prev_count as "Previous total request count", ROUND( ((current_count - prev_count)::numeric/ (t - prev_t)::numeric), 3 ) as "Velocity [req/sec]" FROM ( SELECT t, lag(t, 1) over () as prev_t, count as current_count, lag(count, 1) over () as prev_count FROM connection_lease_millis WINDOW grouping AS ( ORDER BY t ROWS BETWEEN 1 PRECEDING AND CURRENT ROW ) ) raw_data
Giving us the arrival velocity:
|Current time stamp||Previous time stamp||Current total request count||Previous total request count||Velocity [req/sec]|
But what if we want to calculate the arrival acceleration (e.g. so we can figure out how the arrival rate fluctuates), which is
arrival_acceleration = Δarrival_velocity/ Δt
This is how we can do it:
SELECT t as "Current time stamp", prev_t as "Previous time stamp", velocity "Velocity [Req/sec]", ROUND( (velocity - lag(velocity, 1) over ())::numeric / (t - prev_t)::numeric, 3 ) as "Acceleration [req/sec2]" FROM ( SELECT t, prev_t, current_count, prev_count, ROUND( ((current_count - prev_count)::numeric/ (t - prev_t)::numeric), 3 ) as velocity FROM ( SELECT t, lag(t, 1) over () as prev_t, count as current_count, lag(count, 1) over () as prev_count FROM connection_lease_millis WINDOW grouping AS ( ORDER BY t ROWS BETWEEN 1 PRECEDING AND CURRENT ROW ) ) raw_data ) velocity_data
|Current time stamp||Previous time stamp||Velocity [Req/sec]||Acceleration [req/sec2]|
Giving us a nice overview over the arrival rate distribution:
If you enjoyed this article, I bet you are going to love my upcoming Online Workshops!
- Caching Best Practices with JPA and Hibernate (2.5 hours) on the 30th of September
- High-Performance SQL (4 hours) on the 6th of October in collaboration with Voxxed Days Ticino
- High-Performance SQL (12 hours) starting on the 28th of October in collaboration with Bouvet
SQL has more to offer than the standard aggregate functions. The window functions allow you to group rows while still retaining the selection criteria.
How many of you are still using the 1.0 versions of Java, C# or Python? Shouldn’t we benefit from the latest SQL features the same way we do with any other programming language we use on a daily basis?
In case you’re still skeptic, this great article may shatter your doubts.