Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:33:56+00:00 2026-05-28T20:33:56+00:00

I am having a performance problem with a Rails activerecord object using the activerecord_sqlserver_adapter.

  • 0

I am having a performance problem with a Rails activerecord object using the activerecord_sqlserver_adapter. Here is what the sql looks like that is generated and runs very slow;

EXEC sp_executesql N'SELECT COUNT(*) FROM [constituents] WHERE [constituents].[constituent_id] IN (N''10016125'', N''483663'', N''530657'', N''535217'')'

The following runs very fast;

EXEC sp_executesql N'SELECT COUNT(*) FROM [constituents] WHERE [constituents].[constituent_id] IN (''10016125'', ''483663'', ''530657'', ''535217'')'

The adapter is putting N in front of each item in the where clause that is slowing things down. The execution plan suggests I add an index, but that seems unnecessary and this is a legacy database.

Does anyone have a suggestion how I could speed this up?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:33:57+00:00Added an answer on May 28, 2026 at 8:33 pm

    I highly recommend reading How Data Access Code Affects Database Performance and Slow in the Application, Fast in SSMS? Understanding Performance Mysteries. Both articles cover this topic, and much more, in great detail.

    The summary is that the rules of Data Type Precedence dictate that an operation involving both an VARCHAR and an NVARCHAR operands must occur by converting the VARCHAR (precedence rank 27) to NVARCHAR (precedence rank 25). Therefore your query is really like
    I am having a performance problem with a Rails activerecord object using the activerecord_sqlserver_adapter. Here is what the sql looks like that is generated and runs very slow;

    SELECT COUNT(*) 
    FROM [constituents] 
    WHERE CAST([constituents].[constituent_id] as NVARCHAR(...))
    IN (N''10016125'', N''483663'', N''530657'', N''535217'');
    

    This is non-sargable, meaning an index on constituent_id will be ignored and a table scan will be performed instead.

    But the real question here is why do you use strings for what walks and quacks like an int? Shouldn’t the constituent_id column be int, along with the parameters passed in?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'am currently having a slow performance problem with a SQL inner join on two
i developed a project using Ruby on Rails and i having a huge problem
I'm having a problem with performance with the entity framework. Here's the scenario. I
I'm having a very particular performance problem at work! In the system we're using
I've been having performance problems with an SQL trigger. The problem relates to performing
Are there any performance/other implications in having an object relates to itself? (self join)
I recall having read somewhere that it is better (in terms of performance) to
It sounds like Mozilla is having good luck improving JavaScript performance with TraceMonkey .
I'm having a problem that when my frame is shown (after a login dialog)
I have a file log that I would like to parse and am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.