Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

postgresql: Fast way to update the latest inserted row

Tags:

sql

postgresql

What is the best way to modify the latest added row without using a temporary table. E.g. the table structure is

id | text | date

My current approach would be an insert with the postgresql specific command "returning id" so that I can update the table afterwards with

update myTable set date='2013-11-11' where id = lastRow

However I have the feeling that postgresql is not simply using the last row but is iterating through millions of entries until "id = lastRow" is found. How can i directly access the last added row?

like image 913
Anonymous Avatar asked Feb 15 '23 17:02

Anonymous


1 Answers

update myTable date='2013-11-11' where id IN(
       SELECT max(id) FROM myTable
)
like image 116
mvb13 Avatar answered Feb 17 '23 10:02

mvb13