Skip to content

Conversation

@faisalmansoor
Copy link
Contributor

Case specific queries does not take in to account database column collation, therefore, the generated queries force an index scan. I noticed the problem while using uniqueness validator which specifically either does a case sensitive or case insensitive comparison (I didn't find a way to make it use default database casing).

For a column with case insensitive collation, SQL_Latin1_General_CP1_CI_AS, uniqueness validator generates the following queries.

validates :title, uniqueness: true generates SELECT 1 AS one FROM [articles] WHERE [articles].[title] = N''t1'' COLLATE Latin1_General_CS_AS_WS ORDER BY [articles].[id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY

while validates :title, uniqueness: {:case_sensitive => false} generates SELECT 1 AS one FROM [articles] WHERE LOWER([articles].[title]) = LOWER(N''t1'') ORDER BY [articles].[id] ASC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY

both causes index scan, for a large table, this cause a lot io.

MySQL adapter detects column collation and generates queries which does not cause table scan, I ported the same behavior to sqlserver-adapter in this PR. It will be great if you guys can provide some feedback, if this approach seems ok then I'll add some tests also.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

metaskills added a commit that referenced this pull request Nov 7, 2015
…rformance-improvements

Improve case comparision performace
@metaskills metaskills merged commit 120c6e8 into rails-sqlserver:master Nov 7, 2015
metaskills added a commit that referenced this pull request Nov 7, 2015
@metaskills
Copy link
Member

Thanks so much for this work. I'll have master/v4.2.5 out in a few days or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants