Skip to content

Instantly share code, notes, and snippets.

@chrigl
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrigl/b0ee338bf232f0229529 to your computer and use it in GitHub Desktop.
Save chrigl/b0ee338bf232f0229529 to your computer and use it in GitHub Desktop.
This is why using random IDs is usually a bad idea

This is a comment on "Advanced Database Programming with Python"

For example in MySQL, the index is implemented as a balanced tree. Quite obvious. If your are inserting a new entry with a random number, the tree may have to be rebalanced again. Now imagine, you are running a master node with several hundreds of slave nodes, each one of it has to rebalance its index. Maybe there are some usecases, where random ID actually makes sense. But most of the times they result in hard to debug problems on a large scale environment.

Again in MySQL, they assume, you will need the recently inserted entry in short time again. They get this entry because of its location in the balanced tree. So, with random IDs, you break this mechanism as well.

However. All this stuff depends on the DBMS you are using, but auto_increment is a well known and well tested feature within decades. Do not try to solve problems that do not exist ;)

[1] https://ep2014.europython.eu/en/schedule/sessions/104/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment