Subject Re: [firebird-support] Re: Slow query with like '%xxx%' clause - PROBLEM SOLVED!
Author unordained
---------- Original Message -----------
From: "peixinhosdalua" <peixinhosdalua@...>
> CREATE VIEW LISTA_CLIENTES_PESQUISA (IDCLIENTES, PESQUISA)
> AS
>
> select c.IDCLIENTES,
> cp.DESCRICAO||p.NOME||c.IDCLIENTES||c.CLIENTE||c.MORADA|| c.CP||c.LOCALIDADE||
c.PAIS||c.TELEFONE||c.FAX||
> c.EMAIL||
> c.PER_DESCONTO||
> c.CONTRIBUINTE||c.COD_CLIENTE_FAT
> FROM CLIENTES c
> inner join PAISES p on p.CODIGO_ISO_3166_1=c.PAIS
> inner join COND_PAGAMENTO cp on cp.IDCOND_PAGAMENTO=c.CONDICOES_PAGAMENTO
> order by c.CLIENTE;
------- End of Original Message -------

Might I suggest, also:

coalesce(cp.descricao, '') || coalesce(p.nome, '') || ... ? (any single null in a
concatenation will cause the result to be null)

I realize there's a join involved here, but you could still make this a bit more
reusable by, on each table to be searched, adding a computed-by column that does
this work for you:

alter table clientes add search_field computed by (coalesce(cp.descricao, '')
|| ... )

You could retain the view, but the view would now only need to do:

clientes.search_field || paises.search_field || ...

And you'd be able to do your searches on single tables anytime you like, without
repeating the full query, or using the view that does more than you need.

-Philip