in reply to [SOLVED] Need advice how to diagnose the problem when syntax using MAX() is correct according to MySQL monitor, but errors out in DBI

I need the highest article ID in the column "db_art_id".

No, you don't. Or at least, you should not need it.

An ID column should be just that - an identifier, uniquely identifying a single record. It should have no other meaning, no other value, no ordering. It should not matter if the ID is a number or any other unique collection of bits. It should not matter how the ID is generated. The generator simply must never return an ID value more than once.

A common idea that I've heard by way too many database users in the last few decades is to get the maximum of an ID column, increment it by 1, and use that value as the ID value for the next record to be inserted - or worse - to predict the next ID that will be used by the database engine for that column. It sounds like it would work, and the most evil part of this idea is that it will actually work for a while. But then, parallel access and/or rollbacks will happen and things will go horribly wrong.

Yes, in theory, you really could get away with max(id_col)+1 - if you add a lot of locking and transaction handling to that. Things you can't do in SQL, and must do in your application. In other words: If you re-invent what has already been implemented in the database engine. And you can be very sure that the implementation in the database has been tested way better than anything you can come up with, including UUIDs. (And no, UUIDs aren't guaranteed to be unique, UUID collisions are just unlikely. The same is true for hash functions like the SHA family.)

See Re^2: Perl DBI and Foreign Keys, Re^6: Perl DBI and Foreign Keys, and SELECT LAST_INSERT_ID does not work for details.

Alexander

Footnote: It is sometimes really hard to make people not use max(id)+1. I had to threaten to change database sequences to begin at MAXINT and decrement by 3, 7 or 11 just to stop collegues from using max(id)+1.

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
  • Comment on Re: Need advice how to diagnose the problem when syntax using MAX() is correct according to MySQL monitor, but errors out in DBI
  • Download Code

Replies are listed 'Best First'.
Re^2: Need advice how to diagnose the problem when syntax using MAX() is correct according to MySQL monitor, but errors out in DBI
by NERDVANA (Priest) on Nov 22, 2025 at 20:00 UTC
    Well, that's very opinionated take on what he's trying to accomplish. I have some "select max(id)" queries in production right now, for the purpose of monitoring whether a database has had any new records added to certain tables, and sets off alarms if they stop changing within a certain time window. It's a nice efficient query, and applicable to a very large percentage of table designs.

      Similar in my case. I use PostgreSQL and a lot of tables have BIGSERIAL as primary key (bigint automatically generated by a sequence). Yes, seuqnces may have "holes" and are not always perfectly in time order (depending on the transactions that generated them), but a "ORDER BY article_id DESC LIMIT 10" gives a nice overview of the last vew additions to a table.

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Also check out my sisters artwork and my weekly webcomics