If we delete row from MySQL table, and if there is some auto incremental column exists, after insertion it will be resumed incrementing by last value. That is if the last value was 1000, after deletion, incremented value will be 1001.
But what if I want it to increment that column (e.g. id) according max id each time before insertion. I know that it could be achieved by setting:
ALTER TABLE `tableName` AUTO_INCREMENT = MAX(`ID`) + 1
manually, but this sql should be called every time after the deletion or before insertion.
In MySQL table I am just inserting elements and fetching them to the java.util.List
, and I only need to get data according some interval.
Getting according id is much faster than by limit.
e.g.
SELECT * FROM `TABLE_NAME` WHERE ID >=START AND ID <=END
is much faster than
SELECT * FROM `TABLE_NAME` LIMIT START, (END-START)
And getting size of the table is faster by MAX(ID)
than COUNT(*)
Edit:
Regarding deletions, I only delete row from the end of the table or the rest from the id (including) that should be deleted, so that after deletion MAX(ID)
and COUNT(*)
will be equal.
The question is: Is there any way of setting "automated" sequential AUTO_INCREMENT in MySQL?