title: MySQL ON DUPLICATE KEY UPDATE with nullable column in unique key
body: <p>Our MySQL web analytics database contains a summary table which is updated throughout the day as new activity is imported.  We use ON DUPLICATE KEY UPDATE in order that the summarization overwrites earlier calculations, but are having difficulty because one of the columns in the summary table's UNIQUE KEY is an optional FK, and contains NULL values. </p>
  
  <p>These NULLs are intended to mean "not present, and all such cases are equivalent". Of course, MySQL usually treats NULLs as meaning "unknown, and all such cases are not equivalent".</p>
  
  <p>Basic structure is as follows:</p>
  
  <p>An "Activity" table containing an entry for each session, each belonging to a campaign, with optional filter and transaction IDs for some entries.</p>
  
  <pre><code>CREATE TABLE `Activity` (
      `session_id` INTEGER AUTO_INCREMENT
      , `campaign_id` INTEGER NOT NULL
      , `filter_id` INTEGER DEFAULT NULL
      , `transaction_id` INTEGER DEFAULT NULL
      , PRIMARY KEY (`session_id`)
  );
  </code></pre>
  
  <p>A "Summary" table containing daily rollups of total number of sessions in activity table, an d the total number of those sessions which contain a transaction ID.  These summaries are split up, with one for every combination of campaign and (optional) filter.  This is a non-transactional table using MyISAM.</p>
  
  <pre><code>CREATE TABLE `Summary` (
      `day` DATE NOT NULL
      , `campaign_id` INTEGER NOT NULL
      , `filter_id` INTEGER DEFAULT NULL
      , `sessions` INTEGER UNSIGNED DEFAULT NULL
      , `transactions` INTEGER UNSIGNED DEFAULT NULL
      , UNIQUE KEY (`day`, `campaign_id`, `filter_id`)
  ) ENGINE=MyISAM;
  </code></pre>
  
  <p>The actual summarization query is something like the following, counting up the number of sessions and transactions, then grouping by campaign and (optional) filter.</p>
  
  <pre><code>INSERT INTO `Summary` 
      (`day`, `campaign_id`, `filter_id`, `sessions`, `transactions`)
      SELECT `day`, `campaign_id`, `filter_id
          , COUNT(`session_id`) AS `sessions`
          , COUNT(`transaction_id` IS NOT NULL) AS `transactions`
      FROM Activity
      GROUP BY `day`, `campaign_id`, `filter_id`
  ON DUPLICATE KEY UPDATE
      `sessions` = VALUES(`sessions`)
      , `transactions` = VALUES(`transactions`)
  ;
  </code></pre>
  
  <p>Everything works great, except for the summary of cases where the filter_id is NULL. In these cases, the ON DUPLICATE KEY UPDATE clause does not match the existing row, and a new row is written every time.  This is due to the fact that "NULL != NULL".  What we need, however, is "NULL = NULL" when comparing the unique keys.</p>
  
  <p>I am looking for ideas for workarounds or feedback on those we have come up with so far. Workarounds we have thought of so far follow.</p>
  
  <ol>
  <li><p>Delete all summary entries containing a NULL key value prior to running the summarization. (This is what we are doing now)
  This has the negative side effect of returning results with missing data if a query is executed during the summarization process.</p></li>
  <li><p>Change the DEFAULT NULL column to DEFAULT 0, which allows the UNIQUE KEY to be matched consistently.
  This has the negative side effect of overly complicating the development of queries against the summary table.  It forces us to use a lot of "CASE filter_id = 0 THEN NULL ELSE filter_id END", and makes for awkward joining since all of the other tables have actual NULLs for the filter_id.</p></li>
  <li><p>Create a view which returns "CASE filter_id = 0 THEN NULL ELSE filter_id END", and using this view instead of the table directly.
  The summary table contains a few hundred thousand rows, and I've been told view performance is quite poor.</p></li>
  <li><p>Allow the duplicate entries to be created, and delete the old entries after summarization completes.
  Has similar problems to deleting them ahead of time.</p></li>
  <li><p>Add a surrogate column which contains 0 for NULL, and use that surrogate in the UNIQUE KEY (actually we could use PRIMARY KEY if all columns are NOT NULL).<br>
  This solution seems reasonable, except that the example above is only an example; the actual database contains half a dozen summary tables, one of which contains four nullable columns in the UNIQUE KEY.  There is concern by some that the overhead is too much.</p></li>
  </ol>
  
  <p>Do you have a better workaround, table structure, update process or MySQL best practice which can help?</p>
  
  <p>EDIT: To clarify the "meaning of null"</p>
  
  <p>The data in the summary rows containing NULL columns are considered to belong together only in the sense that of being a single "catch-all" row in summary reports, summarizing those items for which that data point does not exist or is unknown.  So within the context of the summary table itself, the meaning is "the sum of those entries for which no value is known". Within the relational tables, on the other hand, these truly are NULL results.</p>
  
  <p>The only reason for putting them into a unique key on the summary table is to allow for automatic update (by ON DUPLICATE KEY UPDATE) when re-calculating the summary reports.</p>
  
  <p>Maybe a better way to describe it is by the specific example that one of the summary tables groups results geographically by the zip code prefix of the business address given by the respondent.  Not all respondents provide a business address, so the relationship between the transaction and addresses table is quite correctly NULL.  In the summary table for this data, a row is generated for each zip code prefix, containing the summary of data within that area. An additional row is generated to show the summary of data for which no zip code prefix is known.</p>
  
  <p>Altering the rest of the data tables to have an explicit "THERE_IS_NO_ZIP_CODE" 0-value, and placing a special record in the ZipCodePrefix table representing this value, is improper--that relationship truly is NULL.</p>