Pivotal Labs

Main menu

Skip to primary content
Skip to secondary content
  • About
  • Case Studies
  • Team
    • Executives
    • Locations
      • San Francisco (HQ)
      • Boston
      • Boulder
      • Denver
      • London
      • Los Angeles
      • New York
  • Community
    • Blogs
    • Tech Talks
    • Events
  • Careers
    • Lifestyle
    • Principles & Practices
    • Benefits
    • FAQ
    • Apply
  • Contact
    • Press Room
    • Press Releases
    • In The News
    • Press Kit
  • All
  • Labs
  • Standup
  • Tracker

Standup 5/21/2010: validates_uniqueness_of and case sensitivity

Nathan Wilmes
Friday, May 21, 2010

Interesting Things

  • One of our clients had a large production issue due to a long-standing bug in Rails with case sensitivity.

Here’s the situation: Rails validates_uniqueness_of has a flag called :case_sensitive. This flag defaults to ‘true’, but can be flipped.

MySQL’s default collation is case-insensitive. As a result, queries will, in general, ignore case unless specifically overridden.

So one might imagine that setting :case_sensitive to false would be completely harmless in a standard MySQL application.

One would be wrong. Setting case_sensitive to false changes the query to lowercase the field in question, causing the MySQL database to ignore any indices it may have and turning the validates_uniqueness_of operation from something cheap and quick to something requiring a full table scan.

The open Lighthouse ticket on this issue is:

https://rails.lighthouseapp.com/projects/8994/tickets/2503-validates_uniqueness_of-is-horribly-inefficient-in-mysql

  • 0 Shares
  • Share on Facebook
  • Share on Twitter

2 Comments

  1. Nick Gauthier says:

    MySQL isn’t that great a DB. It cannot do indices on expressions.

    However, in PostgreSQL this can be solved easily with:

    CREATE INDEX test1_lower_col1_idx ON test1 (lower(col1));

    from http://www.postgresql.org/docs/8.4/static/indexes-expressional.html

    Then PG will use that index when searching on a lowered column.

    One of the many reasons we use PostgreSQL for our projects here at SmartLogic.

    May 21, 2010 at 9:57 am

  2. grosser says:

    Solution is already here since 2009 ;)
    http://pragmatig.com/2009/12/11/validates_uniqness_of-mysql-slow

    May 21, 2010 at 12:17 pm

Add New Comment Cancel reply

Your email address will not be published.

Nathan Wilmes

Nathan Wilmes
San Francisco

Recent Posts

  • Performance tuning an old Pivotal app – how I did it
  • Standup 2/24/2001-2/25/2001
  • Standup 2/23/2011 – mocking AJAX for jQuery 1.5
Subscribe to Nathan's Feed

Author Topics

agile (14)
testing (3)
ruby on rails (1)
  • About
  • Case Studies
  • Team
  • Community
  • Careers
  • Contact
  • Labs
  • Events

Contact Us

contact@pivotallabs.com
+1 415-77-PIVOT
TwitterLinkedInFacebook

Pivotal Tracker

Tracker is the award-winning agile project management tool that enables real-time collaboration around a shared, prioritized backlog.
Visit pivotaltracker.com >