Available in versions: Dev (3.20) | Latest (3.19) | 3.18

This is experimental functionality, and as such subject to change. Use at your own risk!

Unnecessary DISTINCT

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

When every GROUP BY expression is also projected in a SELECT DISTINCT clause at least once, in any order, then the DISTINCT keyword is unnecessary.

Using Settings.transformPatternsUnnecessaryDistinct, the following transformations can be achieved:

-- With Settings.transformPatternsUnnecessaryDistinct active, this:
SELECT DISTINCT b, a, count(*) FROM t GROUP BY a, b;

-- ... is transformed into the equivalent expression:
SELECT b, a, count(*) FROM t GROUP BY a, b;

In simpler cases, such as projection only queries, DISTINCT is also never necessary:

-- With Settings.transformPatternsUnnecessaryDistinct active, this:
SELECT DISTINCT 1, count(*);

-- ... is transformed into the equivalent expression:
SELECT 1, count(*);

References to this page

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo