php - Number sequence generated IDs - Efficient way to my possible worst scenario -


my setup:

mysql , php

system scenario:

i have more 10 type of system users: example :customer , employee

everytime customer or employee added system, system automatically generate id each user based on current date.

ex (customer):

today june 20,2015 , customer 3rd sign up. id 06202015-03. everytime user (any type of user) signup sequence number increment 1 in day basis only. every next day sequence counter 0.

general question: given concern of id generation solved, practice pre-process next sequence #? mean system pullout next sequence number saved on db table? or should process next sequence number until new user signing up?

update (added best possible scenario) :

example date: june 20,2015

  1. customer 1 signup = generated id 06202015-01

  2. customer 2 signup = generated id 06202015-02

    and on...

worst possible scenario during signup:

  1. 2 or more user signing simoltaneously
  2. if customer1 deleted (by admin) on same day , customer2 signed up, customer 2 should #1 id (06202015-01) , not *-02 customer1 being deleted already.

.

i know best way generate sequence number efficiently:

  1. is stored procedure best fit this? or should use #2?(see below)
  2. is practice process next sequence number (using php function) everytime user signed up?

the #2 process think best , easier way process auto id generation i'm thinking if 2 or more users simultaneously singing up?

on latest update, sequence predictable. concern best or efficient way sequence number. thru stored procedure or using php script function given worst scenarios stated.

general question: given concern of id generation solved, practice pre-process next sequence #? mean system pullout next sequence number saved on db table? or should process next sequence number until new user signing up?

if id dependent on date user signs up, can't predict next id because don't know when next user sign (unless clairvoyant).

to make easier obtain next value split id 2 columns, column date , column sequence, u can use:

ifnull((select max(sequence) usertable signup_date = current_date), 0) + 1 

imo there's no best practise, it's personal preference. there's third option, before insert trigger.

to avoid duplicates add unique index both columns.

in addition can lock table:

lock tables user_table write; /* call(sproc) or insert statement, or select , insert statements */ unlock tables; 

with write lock no other session can access table untill lock released (it wait)


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -