You can make the select without a join when you combine the rev
and id
into one maxRevId
value for MAX()
and then split it back to original values:
SELECT maxRevId & ((1 << 32) - 1) as id, maxRevId >> 32 AS rev
FROM (SELECT MAX(((rev << 32) | id)) AS maxRevId
FROM YourTable
GROUP BY id) x;
This is especially fast when there is a complex join instead of a single table. With the traditional approaches the complex join would be done twice.
The above combination is simple with bit functions when rev
and id
are INT UNSIGNED
(32 bit) and combined value fits to BIGINT UNSIGNED
(64 bit). When the id
& rev
are larger than 32-bit values or made of multiple columns, you need combine the value into e.g. a binary value with suitable padding for MAX()
.