on 2014 Mar 25 7:57 AM
In addition to deleting and inserting test data, we need to call sa_reset_identity
for some of our integration tests and it takes about ~0.5 seconds for each table. We frequently run many such tests for many tables and it sums up to a big total. Is there a way to do the same thing faster? (bonus: why does it need 0.5secs?)
Update: TRUNCATE did it.
Request clarification before answering.
I'll go for the bonus point: sa_reset_identity() has a checkpoint as a side effect.
[GuessworkAlert]
The reason for that? Probably because a checkpoint is required to force the change to actually appear in SYSTABCOL.max_identity, which is what you (probably) want when you call sa_reset_identity().
[DeepGuessworkAlert]
I am guessing that putting the call inside an event, and using TRIGGER EVENT to fire-and-forget the event multiple times, MIGHT help... but the probability is low, about the same probability as it MIGHT crash the engine having multiple checkpoints in progress at the same time... the highest probability is that it will have NO REAL EFFECT because the engine will single-thread all the checkpoints.
[/DeepGuessworkAlert]
True relief probably requires a change to the engine. TRUNCATE TABLE might do the trick if the tables are empty.
[/GuessworkAlert]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
This forum needs a PREPARE TO ANSWER statement (like PREPARE TO COMMIT), or maybe a READ WITH HOLD, intent-to-answer-lock, something like that... something for Volker to use to warn other people off 🙂
something for Volker to use to warn others off 🙂
or vice versa:)
Well, it's a wiki, not a RDBMS - with SQL Anywehre, writers would surely block writers:)
FWIW, I would guess that several "parallel" implicit checkpoints (as issued via a bunch of parallel events) would still lead to serialized checkpoint execution, as John has told here in detail:
What are all the bad things which happen when a CHECKPOINT occurs?
I'd think it has to do with the fact that sa_reset_identity() does an automatic checkpoint, so you will have a series of checkpoints when doing a series of such reset calls... - cf. the docs:
Side effects
Causes a checkpoint to occur after the value has been updated
The need for the implicit checkpoint might have to do with the fact that the system catalog stores the maximum value within the syscolumn.max_identity (or systabcol for SA 10 and above) columns, and these are only modified (like other catalog metadata) by a checkpoint, as Glenn has explained here:
What causes syscolumn.max_identity to be updated?
If that is still correct, then I would suspect there is no faster way - unless you might want to suggest an enhanced sa_reset_identity procedure that could reset several tables in one go (i.e. with only one checkpoint call)...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hm, my reasoning would not tell why a TRUNCATE TABLE (as suggested by Breck) seems to be able to reset the max identity value without a checkpoint (only with a leading and trailing commit):
If the table contains a column defined as DEFAULT AUTOINCREMENT or DEFAULT GLOBAL AUTOINCREMENT, the truncation operation resets the next available value for the column.
Right, I can't jump to that conclusion. Well, I've tried further and my tests' speed is great now, although I don't know how. Even the 'slow' truncate as in Breck's book seems to do the job. I'll test some more and publish the results here.
If the table is actually empty, I'm guessing all forms of TRUNCATE will be fast. So will DELETE, but it doesn't reset the autoincrement columns.
User | Count |
---|---|
76 | |
30 | |
8 | |
8 | |
7 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.