[support] case sensitivity, indexes and portability was: query, like and %s

Ivan Sergio Borgonovo mail at webthatworks.it
Fri Sep 4 14:32:16 UTC 2009


On Fri, 04 Sep 2009 12:24:04 +0000
Earnie Boyd <earnie at users.sourceforge.net> wrote:

> Quoting Ivan Sergio Borgonovo <mail at webthatworks.it>:

> > $query = "SELECT DISTINCT sum(re.field_rows_ext_quantity) as q,
> > re.nid, n.title from content_field_rows_ext re JOIN node n on
> > re.nid = n.nid where lower(field_rows_ext_brand) like
> > lower('%%%s%%') group by re.nid, n.title";
> > $res = db_query($query,$marca);

> > - better you add all the group by otherwise it is not easy for
> > the DB to understand what you want and in some cases it may be
> >   impossible. Some DB accept this sloppy syntax but that doesn't
> >   make it less sloppy or non deterministic.
> > - use lower and you may exploit some functional index if any
> > - count the % properly and decide what are you really looking
> > for: '%pino', 'pino%, '%pino%' (I guess the later).

> The use of lower() may be unnecessary in some databases if the
> data column is case insensitive; e.g.: Drupal on MySQL defaults to
> a collation of utf8_general_ci.  In which case you will get all of
> the pino it if is Pino, pIno, or etc. without using the lower()
> function.

But that's going to make it not portable. Of course you could "lower"
on the php side.
But then... you still have the added cost of running a function but
you may miss the chance to use a functional index.
Any decent DB will compute lower on a const just once making the DB
solution better then the php one on most cases.
Somehow you're moving the problem of portability to the index
creation... but at least you leave a door open for optimization and
you've a portable solution.

Of course you still have the problem of %string% that can't
generally exploit any index... etc...

-- 
Ivan Sergio Borgonovo
http://www.webthatworks.it



More information about the support mailing list