title: Performance implications of ORDER BY COALESCE in MySQL
body: <p>My general problem is that I want users to be able to, in effect, add an arbitrary number of fields of different types to associate with items. So one solution I am considering is the following:  </p>
  
  <pre><code>table `items`
  item_id  |  name  
  
  table `parameters`  
  parameter_id  |  name  |  type
  
  table `values`  
  item_id  |  parameter_id  |  datetimevalue  |  datevalue  |  integervalue  |  etc...
  </code></pre>
  
  <p>If a user wants to add a "Date of birth" parameter to some of his items, we would add one parameter to the parameters table, and then one entry in the values table for each item that he wants to have this parameter, with the date going in the datevalue column and all other 'value' fields left null.</p>
  
  <p>To order his items by "Date of birth", supposing this parameter has parameter_id=1, I would do  </p>
  
  <pre><code>SELECT * from 
  items
  join values on items.item_id = values.item_id
  join parameters on parameters.parameter_id = values.parameter_id
  
  where parameter_id = 1
  order by coalesce(values.datetimevalue, values.datevalue, values.integervalue...)
  </code></pre>
  
  <p>My specific question is, will this ORDER BY be performant? Will it make good use of indices? Will it do unnecessary work?</p>
  
  <p>My general question is, is this approach good practice? Is there a better way to do this?</p>