title: SQL: Inner joining two massive tables
body: <p>I have two massive tables with about 100 million records each and I'm afraid I needed to perform an Inner Join between the two. Now, both tables are very simple; here's the description:</p>
  
  <p>BioEntity table:</p>
  
  <ul>
  <li>BioEntityId (int)</li>
  <li>Name (nvarchar 4000, although this is an overkill)</li>
  <li>TypeId (int)</li>
  </ul>
  
  <p>EGM table (an auxiliar table, in fact, resulting of bulk import operations):</p>
  
  <ul>
  <li>EMGId (int)</li>
  <li>PId (int)</li>
  <li>Name (nvarchar 4000, although this is an overkill)</li>
  <li>TypeId (int)</li>
  <li>LastModified (date)</li>
  </ul>
  
  <p>I need to get a matching Name in order to associate BioEntityId with the PId residing in the EGM table. Originally, I tried to do everything with a single inner join but the query appeared to be taking way too long and the logfile of the database (in simple recovery mode) managed to chew up all the available disk space (that's just over 200 GB, when the database occupies 18GB) and the query would fail after waiting for two days, If I'm not mistaken. I managed to keep the log from growing (only 33 MB now) but the query has been running non-stop for 6 days now and it doesn't look like it's gonna stop anytime soon.</p>
  
  <p>I'm running it on a fairly decent computer (4GB RAM, Core 2 Duo (E8400) 3GHz, Windows Server 2008, SQL Server 2008) and I've noticed that the computer jams occasionally every 30 seconds (give or take) for a couple of seconds. This makes it quite hard to use it for anything else, which is really getting on my nerves.   </p>
  
  <p>Now, here's the query:</p>
  
  <pre><code> SELECT EGM.Name, BioEntity.BioEntityId INTO AUX
   FROM EGM INNER JOIN BioEntity 
   ON EGM.name LIKE BioEntity.Name AND EGM.TypeId = BioEntity.TypeId
  </code></pre>
  
  <p>I had manually setup some indexes; both EGM and BioEntity had a non-clustered covering index containing TypeId and Name. However, the query ran for five days and it <strong>did not end</strong> either, so I tried running Database Tuning Advisor to get the thing to work. It suggested deleting my older indexes and creating statistics and two clustered indexes instead (one on each table, just containing the TypeId which I find rather odd - or just plain dumb - but I gave it a go anyway). </p>
  
  <p>It has been running for 6 days now and I'm still not sure what to do...
  Any ideas guys? How can I make this faster (or, at least, finite)?</p>
  
  <p><strong>Update:</strong>
  - Ok, I've canceled the query and rebooted the server to get the OS up and running again
  - I'm rerunning the workflow with your proposed changes, specifically cropping the nvarchar field to a much smaller size and swapping "like" for "=". This is gonna take at least two hours, so I'll be posting further updates later on</p>
  
  <p><strong>Update 2 (1PM GMT time, 18/11/09):</strong>
  - The estimated execution plan reveals a 67% cost regarding table scans followed by a 33% hash match. Next comes 0% parallelism (isn't this strange? This is the first time I'm using the estimated execution plan but this particular fact just lifted my eyebrow), 0% hash match, more 0% parallelism, 0% top, 0% table insert and finally another 0% select into. Seems the indexes are crap, as expected, so I'll be making manual indexes and discard the crappy suggested ones. </p>