Subject Most efficient SQL? Need advice.
Author garthtissington
Hello, I'm just looking for guidance on the best way to achieve a
desired result on a Firebird 1.5 server.

First I'll describe the situation:
- I Have two main tables A and B Joined on a primary key in A This
relationship is 1:1 or 1:0 never 1:many
- In tabels A and B I have integer Foreign keys (Let's cal then
FK_a... FKn) into Domain Lists or Lookup tabels (PK is from a
generator). Lets cal them DL_a...DN_n Some of these domain lists are
the output of a view. These domain lists are again 1:0 or 1:1 in as
much as the foreign key allows nulls.
- The SQL I have build is:
Select <flds> from
a
left outer join b on a.pk=b.pk
left outer join dl_a on a.fk_a=dl_a.pk
left outer join dl_b on a.fk_b=dl_b.pk
left outer join dl_c on a.fk_c=dl_c.pk
left outer join dl_d on a.fk_d=dl_d.pk
left outer join dl_e on b.fk_e=dl_e.pk
left outer join dl_f on b.fk_f=dl_f.pk
left outer join dl_g on b.fk_g=dl_g.pk
left outer join dl_h on b.fk_h=dl_h.pk
... and so on .

Finally, each domain list (dl_a...dl_n) has a field
named "sort_order" which allows the user to specify a sort order
other than the DB coallation order.

Now, what I want to do is load this query into a grid probably
TwwDBGrid. I need to be able to allow the user to select any
dl_column and sort the grid based on the rank field + one other char
field from A or B (e.g. Sort By DL_STATUS.rank + A.ClientName)

What is the most efficient way to set this up. My DB has approx.
450000 rows in Tables a nad b so this has to be very quick.

Any suggestions are very much appreciated.