# Create lowercase index

**URL:** <https://www.ruby-forum.com/t/create-lowercase-index/235597>\
**Category:** Rails\
**Created:** [July 2, 2013, 7:15pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597 "2013-07-02T19:15:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 2, 2013, 7:15pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/1 "2013-07-02T19:15:07Z")

</div>

Hi

I’m sorting some columns like this: MyModel.order(“LOWER(column)  
ASC”)…  
But these queries are quite slow. I’m on Postgres by the way.

Does Rails support creating a lowercase index for these situations? I  
know  
Postgres has support for it and I guess I can create one like this  
(found  
on SO):

execute “CREATE UNIQUE INDEX index\_products\_on\_lower\_name ON products  
USING btree (lower(name));”

But does Rails have support for creating it? Don’t like to use execute()  
if  
there is a better way 🙂

Cheers,  
Linus

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [July 2, 2013, 8:24pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/2 "2013-07-02T20:24:02Z")

</div>

On Tue, Jul 2, 2013 at 12:13 PM, Linus P.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> execute “CREATE UNIQUE INDEX index\_products\_on\_lower\_name ON products USING  
> btree (lower(name));”
> 
> But does Rails have support for creating it? Don’t like to use execute() if  
> there is a better way 🙂

Not that I’ve ever seen but that’s expected tbh.

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [July 3, 2013, 8:32pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/3 "2013-07-03T20:32:04Z")

</div>

Direct from Rails4.0 ActiveRecord documentation:

Creating an index with a specific method

add\_index(:developers, :name, using: ‘btree’)

generates:

CREATE INDEX index\_developers\_on\_name ON developers USING btree (name)  
– PostgreSQLCREATE INDEX index\_developers\_on\_name USING btree ON  
developers (name) – MySQL

Note: only supported by PostgreSQL and MySQL

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [July 3, 2013, 10:11pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/4 "2013-07-03T22:11:59Z")

</div>

How about case insensetive collation for your columns?

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [July 3, 2013, 10:12pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/5 "2013-07-03T22:12:35Z")

</div>

Straight from rails4.0 documentation:  
[http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html](http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html)

Creating an index with a specific method

add\_index(:developers, :name, using: ‘btree’)

generates:

CREATE INDEX index\_developers\_on\_name ON developers USING btree (name)  
– PostgreSQLCREATE INDEX index\_developers\_on\_name USING btree ON  
developers (name) – MySQL

Note: only supported by PostgreSQL and MySQL

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [July 4, 2013, 12:28pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/6 "2013-07-04T12:28:54Z")

</div>

I’m on Rails 3.2.13 I’m afraid.

I’ll just use the execute statement for now 🙂

Den onsdagen den 3:e juli 2013 kl. 20:30:38 UTC+2 skrev Rick:

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [August 15, 2013, 9:19pm UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/7 "2013-08-15T21:19:06Z")

</div>

That doesn’t address the lower(name) portion of the question, just the  
part  
where it specifies btree. Any idea how to do the lowercase portion?

---

<div class="post-metadata">

**Author:** ![cnchan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f17d59/32.png) [@cnchan](https://www.ruby-forum.com/u/cnchan)\
**Post date:** [August 16, 2013, 12:39am UTC](https://www.ruby-forum.com/t/create-lowercase-index/235597/8 "2013-08-16T00:39:26Z")

</div>

For PostgreSQL:

create index index\_developers\_on\_name on developers((lower(name)));

Note the extra set of () to indicate that lower is a functional  
expression instead of a column name.

On Aug 14, 2013, at 6:46 AM, Jonathan D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> CREATE INDEX index\_developers\_on\_name ON developers USING btree (name) –  
> PostgreSQL  
> Does Rails support creating a lowercase index for these situations? I know  
> Postgres has support for it and I guess I can create one like this (found on SO):  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/rubyonrails-talk/7e793c66-22d6-4270-86c3-580ed226e719%40googlegroups.com](https://groups.google.com/d/msgid/rubyonrails-talk/7e793c66-22d6-4270-86c3-580ed226e719%40googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

–  
Scott R.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> **[Elevated Software Development, Inc.](http://www.elevated-dev.com/)**

(303) 722-0567 voice
