title: How to generate 6-8 digit random numbers without colliding with previous generated numbers in time critical fashion?
body: <p>In this scenarion I have some managers(around 150 in numbers). One of their daily job is to generate 50(constant) authorisation code (6-8 digit numbers) which are stored in db with their Id. If any authorisation code is used that code is marked as used and triggers delete them when they are 15 days old and have been used.</p>
  
  <p>In my table i have set authorisation code as unique key. i generate a random number then query the db if it exists i generate another or  i else save it.</p>
  
  <p>Every thing is fine except my logic of checking the existence of number in db.This round trip + checking is causing significant delay as of now there are over 1090083 pending authorisation code. Since these authorisation code are in circulation we cant revoke it and with current load it is taking sometime to find new numbers.</p>
  
  <p>I need to implement it in a different logic for which execution speed should be <strong>low</strong> regardles of number of random number that has been used.</p>
  
  <p>My table is designed as follows</p>
  
  <p>slno(auth increment) || auth_code (random code)  ||    auth_by  (created by)  ||   used
            (1=used/0=unused)</p>