KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Removing all indexes
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: February 19, 2010

In 4D v11 SQL, unlike previous versions, indexes are not required on fields for certain commands or constraints. For example a relation can be in place on a field without an index (it is faster with an index, but it is not required). Another example of this is the Unique constraint for fields. Whether the field is indexed or not, a Unique field does not allow duplicates.

This can cause major slowdowns when importing data. It is common to remove indexes to import data to improve speed, so each record is not indexed as it is imported. In the case of unique fields though you would not want to remove the index. Each record still needs to be checked for uniqueness, and that will be faster if the field is indexed. This has changed in 4D v11 SQL because now you can have unique, un-indexed fields.

The following code will remove all indexes from all fields in a database except for those on Unique fields. It uses both index name and table/field number because not all indexes have names.

ARRAY TEXT($fieldnames;0)
ARRAY TEXT($indexids;0)

$isUnique:=False

Begin SQL
  select Index_Id, Index_Name from _User_Indexes
  where (table_ID=:$tablenumber) and (Uniqueness = :$isUnique)
  into :$indexids, :$fieldnames;

End SQL

For ($loop;1;Size of array($fieldnames))
  If ($fieldnames{$loop}#"")
    DELETE INDEX($fieldnames{$loop})
  Else
    $textpara:=$indexids{$loop}
    $tablenr:=0
    $fieldnr:=0
    Begin SQL
      select table_id, column_Id from _User_Ind_Columns
      where Index_ID = :$textpara limit 1 into :$tablenr, :$fieldnr;

    End SQL
    If (($tablenr#0) & ($fieldnr#0))
      $fieldptr:=Field($tablenr;$fieldnr)
      DELETE INDEX($fieldptr;*)
    End if
  End if
End for