Subject OT: Histograms (was: Slow query because an index is not picked up during sort)
Author Svein Erling Tysvær
>> Firebird doesn't have histograms yet, and logically speaking (I do not
>> know the internals of the optimizer, nor the selectivity of your
>> indexes, so this is how I would think if I should do things manually):

>Just as an aside, histograms will be less of a gain for Firebird than for
>some databases because we optimize queries when they're prepared rather
>than when they're run. That means that for many queries, the optimizer
>won't know what value the query will produce for any particular index look
>up. That's particularly true for the inner look up on nested loop joins.

Sure, Ann, sad, but true. The fact you mention could influence how we code. E.g. where I know would write (in Delphi):

with TIB_Cursor1 do
begin
SQL.Add('SELECT * FROM MyTable WHERE SkewedBooleanValue = :Parameter');
Prepare;
if <Whatever> then
Params[0].AsString:='Y'
else
Params[0].AsString:='N';
end;

It could be more sensible to write

with TIB_Cursor1 do
begin
if <Whatever> then
SQL.Add('SELECT * FROM MyTable WHERE SkewedAndIndexedBooleanValue = ''Y''')
else
SQL.Add('SELECT * FROM MyTable WHERE SkewedAndIndexedBooleanValue = ''N''');
Prepare;
end;

to take advantage of the histograms in Firebird 3. Moreover, histograms might influence which indexes are sensible to create.

Set