http://www.perlmonks.org?node_id=368382


in reply to Re^2: Backslash and Underscore problem with DBI and PostgreSQL.
in thread Backslash and Underscore problem with DBI and PostgreSQL.

Is this fast:
select ... from .. where lower(username) = lower(?)
This may or may not be fast - it depends on how the query engine runs, and whether the optimizer can use an index when you apply a function on a column. It may work fine with Postgres, but I know that Sybase will not generate a good query plan with such a query.

BTW - If you use "LIKE ..." you can normally escape any potential wildcard characters yourself. Sybase uses square brackets to do the escaping (and can use alternate escape characters as well), so that you could write:

SELECT ... FROM ... where foo like ? ESCAPE '\'
and then pass "foo\_" as the search parameter and not get wildcard expansion on the underscore. Check the Postgres docs for similar functionality.

Michael

Replies are listed 'Best First'.
Re^4: Backslash and Underscore problem with DBI and PostgreSQL.
by ctilmes (Vicar) on Jun 21, 2004 at 11:44 UTC
    This may or may not be fast - it depends on how the query engine runs, and whether the optimizer can use an index when you apply a function on a column.

    Probably premature optimization unless there are a lot of users, but PostgreSQL support indexes on expressions:
    http://www.postgresql.org/docs/7.4/interactive/indexes-expressional.html.

    As always, "explain" can be used to examine the optimizer's query plan.

Re^4: Backslash and Underscore problem with DBI and PostgreSQL.
by Seumas (Curate) on Jun 21, 2004 at 23:58 UTC
    I didn't expect lower() to have much of a speed impact and the query plan shows that the difference between doing a straight '=' or '~~*' and lower() is only 44msec versus 54msec.

    My indexes are pretty tight and it's a well (and often) maintained database. With millions of records, it has to be. :)

    The reason I didn't use LIKE is that I'm trying to allow users to create usernames that contain any characters they want without having to manually escape dozens of special characters myself.