ANSI SQL - Get Auto Row ID for Multiple Insert

Status
Not open for further replies.

Tornadus

Ars Scholae Palatinae
1,214
Hey devs, I'm interested in retrieving the row IDs automatically generated for each of these records:

Code:
INSERT INTO myTable ( columnB, columnC, columnD ) SELECT hur, derp, hurderp UNION ALL SELECT hur2, derp2, hurderp2 UNION SELECT ALL hur3, derp3, hurderp3

For example, in MySQL, the auto incrementing value would be in columnA.

In PHP, I would use $pdo->lastInsertId() if I only inserted one record, but I believe with the above statement being executed, it will not work.
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Is there anything unique about the set of data being inserted? For example, is there a column value they all share but no others* do? E.g. batchID, uploadTime, clientPID...?

*or very few others, relative to the size of the table. In this case, do something like:
Begin Tran
Select id into #temp from myTable where sortaUnique = value
Insert ...
Select * from myTable where id not in (select id from #temp)
Commit Tran

Make sure the transaction is set to use full phantom protection.

(excuse crappy formatting, coding on iPhone = suck)
 

Tornadus

Ars Scholae Palatinae
1,214
Unfortunately the uniqueness of the values being inserted cannot be relied on.

There's a chance that some rows may be unique while not others.

I am not very good at identifying schema structures for specific database systems, is your code portable?

If not, and I am not positive, but I think might have a solution for my needs.

If the records are inserted in the order they are listed in the statement, I can grab the last identifier, use that and subtract from it until I get the first unique identifier generated.

Does that sound like something that would work reliably and consistently?

Code:
INSERT INTO myTable ( columnB, columnC, columnD ) SELECT hur, derp, hurderp UNION ALL SELECT hur2, derp2, hurderp2 UNION SELECT ALL hur3, derp3, hurderp3

Code:
$last = $pdo->lastInsertId(); # is 3
$second = $last -  1; # is 2
$first = $second - 1; # is 1
 

Richard Berg

Ars Legatus Legionis
43,037
Subscriptor
Does that sound like something that would work reliably and consistently?
No.
- there's no guarantee the IDs will be monotonic, much less sequential
- there's no guarantee what order the UNIONs will be evaluated
- there's no protection against multiple writers, even when wrapped in a fully ANSI compliant transaction; you'd have to lock the whole table
- you seem to be issuing each statement as separate batches from PHP, which is utterly doomed regardless

IMO you have little choice but to add a batchID column.
 

tinyMan

Ars Tribunus Angusticlavius
6,149
Subscriptor++
In our in-house federated DB system, we use a solution that could probably work for you, but its ugly.

Build a central web service/component/module that hands out blocks of IDs and manages the free/used list. You can write your centralized ID hander-outer with all the locks and mutex's you'd like to make it absolutely impossible for two callers to get assigned the same ID. It shouldn't be the performance "long pole" in any of your algorithms, and if it is, just hand out 10x as many IDs in one group.

Then every time you have a batch of data to insert, just request the requisite number of ID blocks needed to hold your data, pre-assign the IDs to the rows and then fire off the batch to the server. Then you can do whatever you want locally or remotely and you have the IDs that you need in both places.

Using this method, its possible to get performance that is almost identical to having the database do the ID generation.

Unfortunately, its ugly. It requires a separate piece of software to manage IDs and assignments and you lose the ability to get an id that increments by one each row with no missing numbers, but many times you don't actually need that.

Incidentally, I'd really look at whether you can live with slower individual inserts, before going off into this direction.
 

ShuggyCoUk

Ars Legatus Legionis
10,388
Subscriptor++
Note: this is not likely to be the best approach, it is simply an option to evaluate.

If you have a synthetic primary key (or can handle the hassles of taking a natural one and instead having it as a unique constraint) then making that key a guid makes the process pretty trivial since you can pre-generate the guids and know exactly what was inserted.

The are multiple problems with this, notably it prevents you (at least easily) using sequential guids managed by the rdbms which can cause unpleasant write patterns and clustering (though if you cluster based on the unique constraint from the original natural key that's not an issue except in the btree used for the synthetic PK index). It also complicates your schema, which is a shame.

It also increases the size of your records (both in memory and on disk) and makes inserts and PK based lookups a bit more expensive.

If this insert is your biggest issue though it may well be that re-organising this way is your best bet, but I'd think long and hard about it before actually doing it.
 
Status
Not open for further replies.