I have what seems like a pretty simple problem. I want to combine table A with table B to get table C

table A tok count a 1 b 1 table B tok count b 1 c 1 table C (desired) tok count a 1 b 2 c 1
Constraints.
  1. The end goal is speed
  2. I have to use MySQL
  3. I have 2GB of RAM but the tables are so big (multiple millions of rows) reading into a Perl hash and incrementing count there is not an option. We do this already with 'small' < 2GB sets of data as it drop the runtime from hours to minutes
  4. tok is a primary key

What I want to do is encapsulated in this pseudocode

INSERT INTO all_tok (tok, tok1.count+tok2.count) SELECT tok, count from tok1 UNION SELECT tok, count from tok2

I need a union not a join as the final table needa to contain all the toks (at least that is what I think). If a tok exists in one table but not the other I just want the count from the table it exists in. If it does exist in both tables I need the sum of the counts. I have beaten my head against the proverbial wall for some hours. It didn't help, the only good thing was it felt good when I stopped ;-).

Any suggestions about approaches to this seemingly simple task gratefully received. Temp tables are fine.

cheers

tachyon


In reply to OT: MySQL combine 2 tables data when Perl 'fails' by tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.