title: activerecord-sqlserver-adapter unicode slow performance
body: <p>I'm on Rails 3.2.18 and MRI-Ruby-2.0.0</p>
  
  <p>Gemfile:</p>
  
  <pre><code>gem 'tiny_tds'
  gem 'activerecord-sqlserver-adapter'
  </code></pre>
  
  <p>$ gem list | grep -E "tds|sql"</p>
  
  <pre><code>activerecord-sqlserver-adapter (3.2.12)
  sqlite3 (1.3.9)
  tiny_tds (0.6.2)
  </code></pre>
  
  <p>database.yml:</p>
  
  <pre><code>development:
    adapter: sqlserver
    database: DBTEST
    username: domain\username
    password: somepwd
    host: myhostname
    port: 1433
    timeout: 180000
  </code></pre>
  
  <p>Problem: Queries take too long to complete. See:</p>
  
  <pre><code>&gt; MyModel.find_all_by_NRPIS '12700333166'
    MyModel Load (85869.3ms)  EXEC sp_executesql N'SELECT [MyTable].* FROM [MyTable] WHERE [MyTable].[NRPIS] = N''12700333166'''
  </code></pre>
  
  <p>So I see the problem is in <code>N</code> unicode conversions, because when I query without it, its really fast, see:</p>
  
  <pre><code>   &gt;&gt; ActiveRecord::Base.connection.select_all   "SELECT [MyTable].* FROM [MyTable] WHERE [MyTable].[NRPIS] = '12700333166'" }
     (62.5ms)  EXEC sp_executesql N'SELECT [MyTable].* FROM [MyTable] WHERE [TBCAGEDTrabalhador].[NRPIS] = ''12700333166'''
  
     &gt;&gt; ActiveRecord::Base.connection.select_all   "SELECT [MyTable].* FROM [MyTable] WHERE [MyTable].[NRPIS] = N'12700333166'" }
     (102324.0ms)  EXEC sp_executesql N'SELECT [MyTable].* FROM [MyTable] WHERE [MyTable].[NRPIS] = N''12700333166'''
  </code></pre>
  
  <p><strong>What is happening here and how can I solve the issue ?</strong></p>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/8996224/sp-executesql-with-n-arguments-is-causing-slow-performance">THIS QUESTION</a>, but I would really like to tell sqlserver-adapter not to convert anything to unicode.</p>