firebird icon indicating copy to clipboard operation
firebird copied to clipboard

Index for FK create with "WHERE IS NOT NULL" condition by default

Open pbhofstede2 opened this issue 5 months ago • 0 comments

From firebird-support group: https://groups.google.com/g/firebird-support/c/xNOqT9-88MQ

FB 5.0.3 (as well on earlier 5.x versions). I've got a query like

select t1.ID from tableX t1 where (t1.FK2 IS NULL) and (t1.FK1 = char_to_uuid('9A05D014-1414-4B93-ADC3-1723C47F80E9')) and (t1.INTFIELD = 2) and (t1.DATEFIELD = '19.10.2024')

PLAN is: PLAN (T1 INDEX (COMPOUND_IDX4, FK2_IDX))

COMPOUND_IDX4 = (FK1, INTFIELD, DATEFIELD); FK2_IDX = (FK2)

SQL Duration: 1s734ms Fetches from cache = 23.754

When I force to don't use the index of the FK2-field using coalesce (its char(16) OCTETS):

select t1.ID from tableX t1 where (coalesce(t1.FK2, NULL) IS NULL) and (t1.FK1 = char_to_uuid('9A05D014-1414-4B93-ADC3-1723C47F80E9')) and (t1.INTFIELD = 2) and (t1.DATEFIELD = '19.10.2024')

PLAN Is:
PLAN (T1 INDEX ( COMPOUND_IDX4 ))

SQL Duration: 0s015ms Fetches from cache = 843

Recordcount result = 839 selectivity COMPOUND_IDX4 = 0,000001068938 selectivity FK2_IDX = 0,000003139274

Suggestion: Let foreign keys be able to use partial indexes and by default apply "WHERE IS NOT NULL" condition. Related to: #7964, #7258 Make FOREIGN KEY constraint to use any suitable index

pbhofstede2 avatar Jul 21 '25 09:07 pbhofstede2