Mysql – Multiple queries (if conditions) in the best performing way


I would like to create a simple licensing system. Upon the very first authentication with a valid license key I need to set a "start date". Every key has a specified validity (for example 30 days). So once the start_date is set to the current timestamp I also have to set the end_date which is start_date += length .

Of course it is possible that someone authenticates with a key which has start_date and end_date already set. This means I have to check with a if-condition whether I need to set start_date and end_date and I wonder what would be the smartest / best performing way to do this? I am using MySQL 5.6.

This is the Table structure: enter image description here

My necessary MySQL checks in pseudo:

if(supplied_key found as key_id){
  if(start_date != null)
  if(NOW() >= end_date)) return "ok";
  else return "Time exceeded";
 start_date = NOW();
 end_date = start_date + length;
 return "ok";
else return "Key invalid";

Best Answer

You could possibly rid your entire else block in pseudo if you used default values correctly. Also why have start time, wouldn't expire time sound more reasonable? Preset expire time with default value of getdate()+whatever. I see set operations that are meaningless too. Hope this advice you find helpful. I'm not a mysql developer though.