title: SQL Server Implicit convert to varchar vs bigint
body: <p>In summary, is that a slightly perfomance improvement to varchar instead of bigint.</p>
  
  <p>For example, I have a two table like this as below</p>
  
  <p><strong>Item</strong></p>
  
  <pre><code>ID bigint PK;
  item_code varchar(256);
  </code></pre>
  
  <p><strong>SalesItem</strong></p>
  
  <pre><code>ID bigint PK;
  item_id varchar(256);
  </code></pre>
  
  <p>Please take note that <code>item_id</code> is not a foreign key and due to some special case <code>item_id</code> is allowed to be <code>varchar(256)</code>;</p>
  
  <p>I tried a SQL statement like this</p>
  
  <pre><code>Select SalesItem.item_id
  from SalesItem
  left join Item on SalesItem.item_id = Item.id
  </code></pre>
  
  <p>That causes an implicit conversion that SQL Server is trying to convert varchar into int and the performance is worst.</p>
  
  <p>Somehow, we changed the sql into below:</p>
  
  <pre><code>Select SalesItem.item_id
  from SalesItem
  left join Item on SalesItem.item_id = convert(varchar(256), Item.id)
  </code></pre>
  
  <p>Well the performance is outstanding, decreased by nearly 70%-80%</p>
  
  <p>Just curios about is that the sequences on 'ON' matter? and which should I convert it? </p>
  
  <p>Instead of I convert to <code>varchar(256)</code> on <code>item.id</code> is that better that I convert to <code>bigint</code> on <code>salesitem.item_id</code>?</p>
  
  <p>For eg:</p>
  
  <pre><code>Select SalesItem.item_id
  from SalesItem
  left join Item on Item.id = convert(bigint, SalesItem.item_id)
  </code></pre>
  
  <p>Please take note that above sql is just a sample.</p>