views:

83

answers:

4

I'm implementing a database where several tables have string data as candidate keys (eg: username) and will be correspondingly indexed. For these fields I want:

  1. Case insensitivity when someone queries the table on those keys

  2. The initially written case to be preserved somehow so that the application can present the data to the user with the original case used

I also want the database schema to be as database independent as possible, as the application code is (or should not be) not slaved to a particular RDBMS.

Also worth noting is that the vast majority of queries done on the database will be done by the application code, not via direct table access by the client.

In implementing this, I'm running into a lot of annoying issues. One is that not all RDBMS implement COLLATE (which is where cases sensitivity appears to be tunable at schema level) in the same way. Another issue is that the collation and case sensitivity options can be set at multiple levels (server, database, table (?), column) and I can't guarantee to the application what setting it will get. Yet another issue is that COLLATE itself can get hairy because there is a heck of a lot more in there than simply case sensitivity (eg: unicode options).

To avoid all of these headaches, what I'm considering is dodging the issue altogether by storing two columns for one piece of data. One column with the original case, another dropped to lower case by the application layer.

eg: Two of the fields in the table

user_name = "fredflintstone" (a unique index on this one)
orig_name = "FredFlintstone" (just data... no constraints)

The pros and cons of this as I see it are:

Pros:

  1. No ambiguity - the application code will manage the case conversions and I never need to worry about unit tests failing "mysteriously" when the underlying RDBMS/settings changes.

  2. Searches on the index will be clean and never be slowed down by collation features or calls to LOWER() or anything (assuming such things slow down the index, which seems logical)

Cons:

  1. Extra storage space required for the doubled-up data

  2. It seems a bit brutish

I know it will work, but at the same time it smells wrong.

Is it insane/pointless to do this? Is there something I don't know that makes the case sensitivity issue less tricky than it seems to me at the moment?

+1  A: 

Suggest your search queries do something like this:

  • SELECT * FROM Users WHERE LOWER(UserName) = LOWER('fredFlinstone')
  • explicitly include the COLLATION hint on the query when case sensitivity should be ignored/respected

I'd consider the duplication of data for case sensitivity too onerous.

p.campbell
Does LOWER kill the benefit of indexing?
Russ
Most DBMS will not be able to use an index for such a query, which could be a problem. The obvious solution are function-based indices, but not all DBMS have them. Plus, the problems with different COLLATE implementations remain, as mentioned in the question.
sleske
@Russ, in Oracle you can create a function based index so that the column itself indexed with the Lower() -- (http://www.akadia.com/services/ora_function_based_index_2.html)
tanging
@Russ: Yes, it does, unless you create a function-based index for "LOWER(UserName)". However, not all DBMS have function-based indices (e.g. Oracle and PostgreSQL do, MySQL does not).
sleske
@sleske - Thanks - I will look into function base indexing, even though not available across DBMS implementations. This is not something I've heard of before.
Russ
+2  A: 

Of course, decisions like this are always a trade-off, but I don't think this is necessarily "doubled-up data". Lowercasing a string can be a non-trivial operation, in particular if you go beyond ASCII, so the lowercased version of the string is not just "duplicate". It is somewhat related to the original string, but not more than that.

If you think of it as an analog to storing computed results in the DB, it becomes more natural.

The option of querying on UPPER(UserName) is another good solution, which avoids the second column. However, to use it you need at least a reliable UPPER function (where in particular you can control the locale that it uses for non-ASCII characters), and probably function-based indices for decent performance.

sleske
+1  A: 

I've often seen data duplicated in this way for performance reasons. It allows you to keep the original casing (which you'll obviously need as you're not always able to guess what the casing should be, you can't be sure that each name begins with a capital letter for example). If the database doesn't support other ways to do this (functional indexes), then this is practical, not crazy. You can keep the data consistent by using triggers.

steinar
"practical, not crazy" is exactly how I'm viewing it. But it seems like it should be a common problem and I have the feeling I'm missing something since I have not seen this done and don't see any examples around of this.
Russ
Well, there are some databases that just dont support functional indexes. You'll very likely have to index data like this so this will be the only option. I've seen serious applications using this kind if indexing so I would definitely say it's not crazy or out of the ordinary. Where functional indexes are supported (e.g. Oracle and Postgre), using those would probably make more sense.
steinar
+1  A: 

Searches on the index will be clean and never be slowed down by collation features or calls to LOWER() or anything (assuming such things slow down the index, which seems logical)

No, that's not logical. You can have indexes on constant functions.

create index users_name on users(name); -- index on name
create index users_name_lower on users(lower(name)); -- index on the function result

Your RDBMS should be smart enough to know to use users_name_lower when it gets this query:

select * from users where lower(name) = ?

Without users_name_lower, yes, that would have to walk the table. With the functional index, it does the right thing.

Andy Lester