title: How optimize the research of next free "slot" in mysql?
body: <p>i've a problem and i can't find an easy solution.
  I have self expanding stucture made in this way.</p>
  
  <p>database1 | table1
            | table2
            ....
            | table n</p>
  
  <p>.
  .
  .</p>
  
  <p>databaseN | table 1
              table 2
              table n</p>
  
  <p>each table has a structire like this:</p>
  
  <p>id|value</p>
  
  <p>each time a number is generated is put into the right database/table/structure (is divided in this way for scalability... would be impossible to manage table of billions of records in a fas way).</p>
  
  <p>the problem that N is not fixed.... but is like a base for calculating numbers (to be precise N is known....62 but I can onlyuse a subset of "digits" that could be different in time).</p>
  
  <p>for exemple I can work only with 0 1 and 2 and after a while (when I've done all the possibilities) I want to add 4 and so on (up to base 62).</p>
  
  <p>I would like to find a simple way to find the 1st free slot to put the next randomly generated id but that could be reverted.</p>
  
  <p>Exemple:</p>
  
  <p>I have 0 1 2 3 as numbers I want use....</p>
  
  <p>the element 2313 is put on dabase  2 table 3 and there will be 13|value into table.</p>
  
  <p>the element 1301 is put on dabase  1 table 3 and there will be 01|value into table.</p>
  
  <p>I would like to generate another number based on the next free slot.
  I could test every slot starting from 0 to the biggest number but when there will be milions of records for every database and table this will be impossible.</p>
  
  <p>the next element of the 1st exemple would be 2323(and not 2314 since I'm using only the 0 1 2 3 digits).
  I would like som sort of invers code in mysql to give me the 23 slot on table 3 database 2 to transform it into the number. I could randomly generate a number and try to find the nearest free up and down but since the set is variable could not be a good choice.</p>
  
  <p>I hope it will be clear enought to tell me any suggestion ;-)</p>