<p>I have a query where I am searching against a string:</p>

<pre><code>SELECT county FROM city WHERE UPPER(name) = 'SAN FRANCISCO';
</code></pre>

<p>Now, this works fine, but it doesn't scale well, and I need to optimize it.  I have <a href="http://www.ibm.com/developerworks/db2/library/techarticle/0203adamache/0203adamache.html" rel="nofollow noreferrer">found an option</a> along the lines of creating a generated view, or something like that, but I was hoping for a simpler solution using an index.</p>

<p>We are using DB2, and I really want to use an <a href="http://ibmsystemsmag.blogs.com/db2utor/2007/11/db2-9-index-wit.html" rel="nofollow noreferrer">expression in an index</a>, but this option seems to only be available on z/OS, however we are running Linux.  I tried the expression index anyways:</p>

<pre><code>CREATE INDEX city_upper_name_idx
ON city UPPER(name) ALLOW REVERSE SCANS;
</code></pre>

<p>But of course, it chokes on the UPPER(name).</p>

<p>Is there another way I can create an index or something similar in this manner such that I don't have to restructure my existing queries to use a new generated view, or alter my existing columns, or any other such intrusive change?</p>

<p>EDIT: I'm open to hearing solutions for other databases... it might carry over to DB2...</p>

[{'answer_id': 12886, 'author': 'nsanders', 'author_id': 1244, 'author_profile': 'https://Stackoverflow.com/users/1244', 'pm_score': 3, 'selected': False, 'text': "<p>You could add an indexed column holding a numerical hash key of the city name.  (With duplicates allowed).</p>\n\n<p>Then you could do a multi-clause where :</p>\n\n<pre><code>hash = [compute hash key for 'SAN FRANCISCO']\n\nSELECT county \nFROM city \nWHERE cityHash = hash \n  AND UPPER(name) = 'SAN FRANCISCO' ;\n</code></pre>\n\n<p>Alternatively, go through your db manual and look at the options for creating table indexes.  There might be something helpful.</p>\n"}, {'answer_id': 12962, 'author': 'Mark Harrison', 'author_id': 116, 'author_profile': 'https://Stackoverflow.com/users/116', 'pm_score': 1, 'selected': False, 'text': '<p>Oracle supports function-based indexes.  Their canonical example:</p>\n\n<pre><code> create index emp_upper_idx on emp(upper(ename));  \n</code></pre>\n'}, {'answer_id': 13108, 'author': 'Neall', 'author_id': 619, 'author_profile': 'https://Stackoverflow.com/users/619', 'pm_score': 1, 'selected': False, 'text': '<p>PostgreSQL also supports indexing the results of a function:</p>\n\n<pre><code>CREATE INDEX mytable_lower_col1_idx ON mytable (lower(col1));\n</code></pre>\n\n<p>The only other option I can think of is to de-normalize your data a bit by creating another column to hold the upper-case version (updated by triggers) and index that. Blech!</p>\n'}, {'answer_id': 16544, 'author': 'Kevin Crumley', 'author_id': 1818, 'author_profile': 'https://Stackoverflow.com/users/1818', 'pm_score': 2, 'selected': False, 'text': '<p>I don\'t know whether this would work in DB2, but I\'ll tell you how I\'d do this in SQL Server.  I <em>think</em> the way MSSQL does this is ANSI standard, though the specific collation strings may differ.  Anyway, if you can do this without trashing the rest of your application -- are there other places where the "name" column needs to be case-sensitive? -- try making that whole column case-insensitive by changing the collation, then index the column.</p>\n\n<pre><code>ALTER TABLE city ALTER COLUMN name nvarchar(200) \n    COLLATE SQL_Latin1_General_CP1_CI_AS\n</code></pre>\n\n<p>...where "nvarchar(200)" stands in for whatever\'s your current column data type.  The "CI" part of the collation string is what marks it as case-insensitive in MSSQL.</p>\n\n<p>To explain... my understanding is that the index will store values in the order of the indexed column\'s collation.  Making the column\'s collation be case-insensitive would make the index store \'San Francisco\', \'SAN FRANCISCO\', and \'san francisco\' all together.  Then you should just have to remove the "UPPER()" from your query, and DB2 should know that it can use your index.</p>\n\n<p>Again, this is based solely on what I know about SQL Server, plus a couple minutes looking at the SQL-92 spec; it may or may not work for DB2.</p>\n'}, {'answer_id': 42789, 'author': 'Troels Arvin', 'author_id': 4462, 'author_profile': 'https://Stackoverflow.com/users/4462', 'pm_score': 1, 'selected': False, 'text': '<p>DB2 isn\'t strong regarding collation. And it doesn\'t have function-based indexes.</p>\n\n<p>Niek Sanders\'s suggestion would work, if you can accept that the hashing has to happen in your application (as DB2 doesn\'t have SHA or MD5 functions, as far as I know).</p>\n\n<p>However, if I were you, I\'d create a materialized view (MQT == Materialized Query Table, in db2 parlance) using <a href="http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0000927.html" rel="nofollow noreferrer">CREATE TABLE AS</a>, adding a column with a pre-computed upper-case variant of the name. Note: You may add indexes to materialized views in DB2.</p>\n'}, {'answer_id': 419382, 'author': 'paxdiablo', 'author_id': 14860, 'author_profile': 'https://Stackoverflow.com/users/14860', 'pm_score': 3, 'selected': False, 'text': '<p>Short answer, no.</p>\n\n<p>Long answer, yes if you\'re running on the mainframe, but you\'re not, so you have to use other trickery.</p>\n\n<p>DB2 (as of DB2/LUW v8) now has generated columns so you can:</p>\n\n<pre><code>CREATE TABLE tbl (\n    lname  VARCHAR(20),\n    fname  VARCHAR(20),\n    ulname VARCHAR(20) GENERATED ALWAYS AS UPPER(lname)\n);\n</code></pre>\n\n<p>and then create an index on ulname. I\'m not sure you\'re going to get it simpler than that.</p>\n\n<p>Before that, you used to have to use a combination of insert and update triggers to ensure the ulname column was kept in sync, and this was a nightmare to maintain. Also, now that this functionality is part of the core DBMS, it\'s been highly optimized (it\'s much faster than the trigger-based solution) and doesn\'t get in the way of real user triggers, so no extra DB objects to maintain.</p>\n\n<p>See <a href="http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/ad/c0007004.htm" rel="noreferrer">here</a> for details.</p>\n'}]
['https://Stackoverflow.com/questions/12855', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/122/']