Советы по Delphi

Удаление и восстановление индексов


Slava Kostin пишет:

Часто приходится удалять индексы из таблицы для ускорения процесса закачки туда данных, потом приходится переиндексировать. Данный класс TDBWork призван облегчить этот процесс. procedure DropIndexes; - удаляет все индексы таблицы, предварительно сохраняя их в файле <имя таблицы>.set в каталоге программы procedure CreateIndexes; - создает все индексы, читая информацию о них из файла <имя таблицы>.set. После успешного завершения операции данный файл удаляется. Пример использования:

    procedure TForm1.Button1Click(Sender: TObject);
var i: TDBWork;
begin
i:=TDBWork.Create;

i.OpenTable(DM.Table1); i.DropIndexes; i.Free; end;

procedure TForm1.Button2Click(Sender: TObject);
var i: TDBWork;
begin
i:=TDBWork.Create; i.OpenTable(DM.Table1); i.CreateIndexes; i.Free; end;

    (*
Часто приходится удалять индексы из таблицы для ускорения процесса
закачки туда данных, потом приходится переиндексировать.
Данный класс TDBWork призван облегчить этот процесс.

procedure DropIndexes; - удаляет все индексы таблицы, предварительно
сохраняя их в файле <имя таблицы>.set в каталоге программы procedure CreateIndexes; - создает все индексы, читая информацию о них
из файла <имя таблицы>.set. После успешного завершения операции данный файл удаляется.
Пример использования:

procedure TForm1.Button1Click(Sender: TObject);
var i: TDBWork;
begin
i:=TDBWork.Create; i.OpenTable(DM.Table1); i.DropIndexes; i.Free; end;

procedure TForm1.Button2Click(Sender: TObject);
var i: TDBWork;
begin
i:=TDBWork.Create; i.OpenTable(DM.Table1); i.CreateIndexes; i.Free; end;

*)

unit DBWork;

interface
uses
Forms,Sysutils,Db,dbtables,inifiles;

type
TIndexInfo=record Name: String; Fields: String; DescFields: String; Options: TIndexOptions; end;
type
TDBWork=class private prgpath,ininame: String; tbl: TTable; IndInfo: array of TIndexInfo; public constructor Create; overload; constructor Create(t: TTable); overload; procedure OpenTable(t: TTable); procedure ReadIndexInfo; procedure DropIndexes; procedure CreateIndexes; procedure SaveToFile; procedure LoadFromFile; end;
implementation

{ TDBWork }

{Автор: Slava Kostin}
constructor TDBWork.Create;
begin
prgpath:=ExtractFilePath(Application.ExeName); tbl:=Nil; end;

constructor TDBWork.Create(t: TTable);
begin
prgpath:=ExtractFilePath(Application.ExeName); OpenTable(t); end;

procedure TDBWork.CreateIndexes;
var i: Integer;
e,a: Boolean; begin
LoadFromFile; with tbl do begin a:=Active; if Active then Close; e:=Exclusive; Exclusive:=true; for i:=0 to Length(IndInfo)-1 do if(IndInfo[i].Name<>'') then AddIndex(IndInfo[i].Name,IndInfo[i].Fields, IndInfo[i].Options,IndInfo[i].DescFields); Exclusive:=e; Active:=a; end; DeleteFile(ininame); end;

procedure TDBWork.DropIndexes;
var i: Integer;
e,a: Boolean; begin
SaveToFile; with tbl do begin a:=Active; if Active then Close; e:=Exclusive; Exclusive:=true; for i:=0 to Length(IndInfo)-1 do if(IndInfo[i].Name<>'') then DeleteIndex(IndInfo[i].Name); Exclusive:=e; Active:=a; end; end;

procedure TDBWork.LoadFromFile;
var i,c: Integer;
ini: TMemIniFile; Section: String; begin
ini:=TMemIniFile.Create(ininame); c:=ini.ReadInteger('Common','DefsCount',0); SetLength(IndInfo,c); for i:=0 to c-1 do begin Section:='IndexDef #'+IntToStr(i); IndInfo[i].Name:=ini.ReadString(Section,'Name',''); IndInfo[i].Fields:=ini.ReadString(Section,'Fields',''); IndInfo[i].DescFields:=ini.ReadString(Section,'DescFields',''); IndInfo[i].Options:=[]; if ini.ReadBool(Section,'ixPrimary',false) then IndInfo[i].Options:=[ixPrimary]; if ini.ReadBool(Section,'ixUnique',false) then IndInfo[i].Options:=IndInfo[i].Options+[ixUnique]; if ini.ReadBool(Section,'ixDescending',false) then IndInfo[i].Options:=IndInfo[i].Options+[ixDescending]; if ini.ReadBool(Section,'ixCaseInsensitive',false) then IndInfo[i].Options:=IndInfo[i].Options+[ixCaseInsensitive]; if ini.ReadBool(Section,'ixExpression',false) then IndInfo[i].Options:=IndInfo[i].Options+[ixExpression]; if ini.ReadBool(Section,'ixNonMaintained',false) then IndInfo[i].Options:=IndInfo[i].Options+[ixNonMaintained]; end; ini.Free; end;

procedure TDBWork.OpenTable(t: TTable);
begin
tbl:=t; ReadIndexInfo; ininame:=prgpath+tbl.TableName+'.set'; end;

procedure TDBWork.ReadIndexInfo;
var i: Integer;
begin
with
tbl.IndexDefs do begin Update; SetLength(IndInfo,Count); for i:=0 to Count-1 do begin IndInfo[i].Name:=Items[i].Name; IndInfo[i].Fields:=Items[i].Fields; IndInfo[i].DescFields:=Items[i].DescFields; IndInfo[i].Options:=Items[i].Options; end; end; end;

procedure TDBWork.SaveToFile;
var i: Integer;
ini: TMemIniFile; Section: String; begin
ini:=TMemIniFile.Create(ininame); ini.WriteInteger('Common','DefsCount',Length(IndInfo)); for i:=0 to Length(IndInfo)-1 do begin Section:='IndexDef #'+IntToStr(i); ini.WriteString(Section,'Name',IndInfo[i].Name); ini.WriteString(Section,'Fields',IndInfo[i].Fields); ini.WriteString(Section,'DescFields',IndInfo[i].DescFields); ini.WriteBool(Section,'ixPrimary',ixPrimary in IndInfo[i].Options); ini.WriteBool(Section,'ixUnique',ixUnique in IndInfo[i].Options); ini.WriteBool(Section,'ixDescending',ixDescending in IndInfo[i].Options); ini.WriteBool(Section,'ixCaseInsensitive',ixCaseInsensitive in IndInfo[i].Options); ini.WriteBool(Section,'ixExpression',ixExpression in IndInfo[i].Options); ini.WriteBool(Section,'ixNonMaintained',ixNonMaintained in IndInfo[i].Options); end; ini.UpdateFile; ini.Free; end;

end.

[001962]



Содержание раздела