Tech Tip: Code to create indexes for all non-indexed-unique fields
PRODUCT: 4D | VERSION: 13.4 | PLATFORM: Mac & Win
Published On: April 11, 2014
The following code will find all fields that are marked as unique that do not have an index and create an index for those fields:
C_LONGINT($maxTableNumber_l;$currentTable_l) C_LONGINT($maxFieldCount_l;$currentField_l) C_LONGINT($dontCare_l) // For GET FIELD PROPERTIES values that are not used. C_BOOLEAN($dontCare_f;$isIndexed_f;$isUnique_f) C_TEXT($logHeader_t;$logRecord_t;$logfile_t) C_TEXT($delim_t;$lf_t) C_TIME($logfile_h) C_TEXT($tableName_t;$fieldName_t;$note_t) $maxTableNumber_l:=Get last table number For ($currentTable_l;1;$maxTableNumber_l) If (Is table number valid($currentTable_l)) $maxFieldCount_l:=Get last field number(Table($currentTable_l)) For ($currentField_l;1;$maxFieldCount_l) If (Is field number valid($currentTable_l;$currentField_l)) // Note the following line breaks over two lines in text, // it is one statement in the method: GET FIELD PROPERTIES($currentTable_l;$currentField_l;$dontCare_l;\ $dontCare_l;$isIndexed_f;$isUnique_f;$dontCare_f) If (($isUnique_f) & (Not($isIndexed_f))) $tablePtr:=Table($currentTable_l) $fieldPtr:=Field($currentTable_l;$currentField_l) $tableName_t:=Table name($tablePtr) $fieldName_t:=Field name($fieldPtr) $indexName_t:="["+$tableName_t+"]"+$fieldName_t+" indexed for uniqueness (kb#77023)" ARRAY POINTER($fieldsArray_p;1) $fieldsArray_p{1}:=$fieldPtr CREATE INDEX($tablePtr->;$fieldsArray_p;Standard BTree Index;$indexName_t;*) End if End if End for End if End for |
Note: This is no longer needed once the database is brought up to 4D v14 because an index is automatically created for all unique fields in 4D v14. This process is completed upon database startup (i.e. a database restart should be completed after setting the unique property).