3

I am a newbie at mysql and databases. I have a simple question. I have created a table that has an integer type id column that is auto incremented. After each insert I get the last row inserted id (in python using cursor.lastrowid, or connection.insert_id()). I wanted to know what is the time complexity in mysql to get this value? I am guessing its O(1) as the database should be storing this value somewhere and updating it after each insert?

Thanks.

assassin
  • 19,899
  • 10
  • 30
  • 43
  • This is the `LAST_INSERT_ID()` that was created by the last `INSERT` statement of your session. So, probably MySQL stores the value somewhere for every session. – ypercubeᵀᴹ Aug 25 '11 at 10:01
  • see SO question: http://stackoverflow.com/questions/2548493/in-python-after-i-insert-into-mysqldb-how-do-i-get-the-id – Kevin Burton Aug 25 '11 at 10:11

1 Answers1

2

cursor.lastrowid will return the value from the single insert see: http://www.python.org/dev/peps/pep-0249/

connection.insert_id() will have to make a seperate call to get the last_insert_id, and would slightly slower

Kevin Burton
  • 11,676
  • 2
  • 24
  • 37