tags:

views:

51

answers:

3

I have a huge table that is mainly used for backup and administrative purposes. The only records that matters is the last inserted record.

On every hit to order by time inserted is just too slow. I want keep a separate table with the last inserted id.

In PHP I now insert, get last inserted id, and update the other table.

Is there a more efficient way to do this.

+4  A: 

You could do this on the database end by using a trigger.

Matti Virkkunen
Do you have any idea how this scales performance wise. I know that function such as COUNT(*) RAND, etc do not perform well in large datasets. Do you have any information about this on triggers.
Saif Bechan
A: 

Why don't you add an index on that field?

Quick seach and sort is exactly what an index is for.

Updating your own 'pseudo-index' in a table amounts to re-inventing the wheel.

Besides, adding a trigger to a DB always feels very dubious (as in un-obvious) to me!

lexu
Oh my question was a little different. There are different groups. So I want to have the last inserted id for the groups. With this method I need to group by, and then order by. That is inefficient. Sorry for being a bit unclear. But if however you just had answered my main question, this would not have a problem. Sometimes its just good if you answer the question people have, than try to reinvent a new method for them.
Saif Bechan
You described a problem 'I have a huge table - only last inserted record matters', then you claim another table and a trigger on insert (and update presumably, then this is archive, so no update) is the solution to that problem.. An index on the timestamp will allow you to get last one very fast. **BTW** What do you expect the group by to accomplish? Do you have multiple groups you need to know the last insert for?
lexu
Basically what is want to know is: Best way to update record X when Y is inserted.
Saif Bechan
+1  A: 

(Sorry about posting this as a separate answer, was a bit too long for a comment on Matti's answer.)

There is a small performance overhead associated with triggers, but if I recall correctly it's fairly negligible for normal use (depending on what you're doing with it of course). Mostly it'd only be a problem if you're performing bulk uploads (in which case you'd usually drop/disable the triggers for the duration of the task). Seems to me that the overhead here would be very minimal seeing as you're only really performing one INSERT/UPDATE on X in addition to the INSERT on Y.

Essentially, a trigger will scale a lot better compared to your current method because instead of having to perform a lookup to find the last updated record you can just perform the insert operation, then directly insert the primary key of the new record into the "last updated" table.

Moonshield
If the trigger will directly get the last inserted id without doing a look up this would be great. I have no bulk operations so this would not be a problem for me. I am always afraid however about using mysql's build in features because some of them just don't scale well. In this case it does. I will look up the trigger and apply it.
Saif Bechan