title: MySQL - reducing number of SELECTs with one smart WHERE
body: <p>I have a table with 3 columns that keeps some four-digit <code>ids</code> in it. Like that:</p>
  
  <pre><code>+ main_id + id_1 + id_2 + id_3 +
  |---------|------|------|------|
  |    1    | 1000 | 1500 | 1900 |
  |    2    | 1001 | 1501 | 1901 |
  |    3    | 1002 | 1502 | 1902 |
  +---------+------+------+------+
  </code></pre>
  
  <p>The idea is the values can't repeat themselves via other combinations. I mean, if the table already has <code>1001 - 1501 - 1901</code>, combinations like <code>1001 - 1901 - 1501</code> or <code>1501 - 1001 - 1901</code> can't appear in the table anymore and should point the initial combination with <code>main_id</code> and should return <code>2</code> in any case.</p>
  
  <p>For this, I got a stored function to which I pass all three <code>ids</code> and get the <code>main_id</code> on exit, like this one:</p>
  
  <pre><code>SET temp_id = (SELECT `main_id` FROM `tableName` WHERE (`id_1` = id1 AND `id_2` = id2 AND `id_3` = id3) LIMIT 1);
  
  IF (temp_id IS NULL) THEN SET temp_id = (SELECT `main_id` FROM `tableName` WHERE (`id_1` = id1 AND `id_2` = id3 AND `id_3` = id2) LIMIT 1);
      IF(temp_id IS NULL) THEN SET temp_id = (SELECT `main_id` FROM `tableName` WHERE (`id_1` = id2 AND `id_2` = id1 AND `id_3` = id3) LIMIT 1);
          IF(temp_id IS NULL) THEN SET temp_id = (SELECT `main_id` FROM `tableName` WHERE (`id_1` = id2 AND `id_2` = id3 AND `id_3` = id1) LIMIT 1);
              IF(temp_id IS NULL) THEN SET temp_id = (SELECT `main_id` FROM `tableName` WHERE (`id_1` = id3 AND `id_2` = id1 AND `id_3` = id2) LIMIT 1);
                  IF(temp_id IS NULL) THEN SET temp_id = (SELECT `main_id` FROM `tableName` WHERE (`id_1` = id3 AND `id_2` = id2 AND `id_3` = id1) LIMIT 1);
                  END IF;
              END IF;
          END IF;
      END IF;
  END IF;
  
  RETURN temp_id;
  </code></pre>
  
  <p>I thought about using all of possible combinations in <code>WHERE</code> clauses in one <code>SELECT</code> with <code>OR</code> operator, like this one:</p>
  
  <pre><code>SELECT `main_id` FROM `tableName`
    WHERE (((`id_1` = id1 AND `id_2` = id2 AND `id_3` = id3)
      OR (`id_1` = id2 AND `id_2` = id1 AND `id_3` - id3)
      ...
      )) LIMIT 1
  </code></pre>
  
  <p>but it would run through all possible solutions not stopping when one found. The previous example would at least stop going deeper if the combination is found.</p>
  
  <p>For only three columns I get 6 combinations in total. I plan to do the same thing for 4 and 5 columns, giving me a <em>huge</em> amount of select queries in total.</p>
  
  <p>The question is, is there a way to speed this function up? Reduce the amount of <code>SELECT</code> queries I send? I use InnoDB, maybe something quicker would come in handy?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I need to pull out one main id for a set of three values. So far, the endless <code>if</code> statements are the fastest solution. And I'm looking for an optimization.</p>