title: MySQL: count unique days in period with overlaps and gaps (optimized)
body: <p>How to calculate the number of unique days per month from a table with two date columns, in which the periods can have gaps and overlaps?</p>
  
  <p>I rather not use a calendar table to get the unique days, because it generates a temporary table with thousands of records, and resources are limited.</p>
  
  <p>Example table:</p>
  
  <pre><code>+---------+------------+------------+
  | mygroup | alpha      | omega      |
  +---------+------------+------------+
  |       1 | 2017-02-04 | 2017-04-14 |
  |       1 | 2017-03-25 | 2017-03-28 |
  |       1 | 2017-01-23 | 2017-01-25 |
  |       2 | 2017-02-05 | 2017-02-20 |
  |       1 | 2017-04-28 | 2017-05-12 |
  |   etc.  |    etc.    |    etc.    |
  +---------+------------+------------+
  </code></pre>