Foxpert Software Development & Consulting

Menu

Whitepapers
Downloads
Knowlbits
Guineu

2007-08Aug-20

Kill your index with REINDEX

When you read about repairing indexes in FoxPro forums, you frequently get the advice to avoid REINDEX. The most common reasoning is that REINDEX depends on the header which might as well be corrupt. That's true, but like most developers, I haven't seen a corrupt index header in years. The header is only updated when you add a tag. As this requires exclusive access to the table, there's little chance of introducing corruption due to caching, multi-user issues, and the like.

REINDEX is a bad idea, nonetheless. In order to create a brand new CDX file, Visual FoxPro has to move the old CDX file out of the way. For a short moment the table exists without a CDX file. A moment short enough, though, to causes malfunctioning. When REINDEX cancels out due to an error, it does not restore the previous CDX file. As the following sample demonstrates, you end up with an indexless table:

Create Cursor curDemo (cID C(1))
Insert into curDemo Values ("A")
Insert into curDemo Values ("B")
Index on GetID(cID) Tag cID CANDIDATE
? ">", Key(1)
Reindex
? ">", Key(1)
plKill = .T.
Reindex
? ">", Key(1)

Procedure GetID(tcID)
	If Vartype(m.plKill) == "L"
		Return "A"
	Else
		Return m.tcID
	EndIf
EndProc

Run this program and ignore the error message. You can see that KEY(1) returns a valid expression the first two times, but nothing the last time. If you had used a table you would notice that the CDX file is gone. That's only a problem when you encounter an error during the index operation. Aside from problems with memory and network connections, you might encounter errors on CANDIDATE and PRIMARY indexes when you

  • added an index without letting check VFP existing data,
  • ran into some sort of index corruption that allowed VFP to add multiple records with the same key, or
  • have an index on a function that is causing an error.
In any case, it's probably better to avoid these problems and just don't use REINDEX in a production application.

Previous KnowlBits

RSS

February 2011 (2)

December 2010 (1)

October 2009 (2)

September 2009 (1)

August 2009 (4)

July 2009 (2)

June 2009 (2)

May 2009 (1)

April 2009 (1)

March 2009 (1)

August 2008 (1)

July 2008 (2)

May 2008 (1)

April 2008 (2)

January 2008 (2)

December 2007 (2)

November 2007 (2)

October 2007 (1)

September 2007 (1)

August 2007 (5)

July 2007 (4)

May 2007 (6)

March 2007 (3)

February 2007 (7)

January 2007 (6)

November 2006 (1)

October 2006 (3)

September 2006 (10)

June 2006 (2)

May 2006 (6)

April 2006 (1)


Impressum Kontakt Contact