> Marcelo Carvalho wrote:
> > Since I started to work with Interbase, I'm a bit lost about indexes.
> > In Paradox/dBase we defined manually each one, thinking about the
searches
> > we would do or if we would need some kind of filtering or ordering using
> > tables.
> > But IB has the dynamic indexes it uses when it needs, and I don't know
when
> > an index can be useful or even reduce speed...
> Indices give you one and only one important feature: Selectivity. When
> you need to decide if a particular index will be helpful for a
> particular operation, you need to know the selectivity of the index and
> the nature of the operation.
> What is selectivity? It's a measure of how uniquely the index can
> identify a particular record. For example, the selectivity of a primary
> key index is always 1, since the primary key always uniquely identifies
> a particular record in the table. In other words, since no two records
> can have the same primary key value, any value in the index will always
> correspond to exactly one record.
> The worst case selectivity is an index on a column in a table where
> every record in the table has exactly the same value in that column. In
> this case, the index doesn't help you find a particular record at all,
> and the selectivity of the index = the number or records in the table.
> Indices are very useful for WHERE clauses, because a very selective
> index will significantly lower the number of records which must be
> evaluated. Imagine the following WHERE clause:
> WHERE
> COLUMN1 = 'Foo'
> AND
> COLUMN2 = 'Bar';
> Imagine further that there are 100,000 records in the table, and that
> there is an index on COLUMN1 with a selectivity of 5 -- on average,
> there are 5 records in the table for every value in the index.
> Without using the index, InterBase would have to read all 100,000
> records and see if each one meets the conditions in the WHERE clause.
> Using the index, InterBase can immediately locate the records which
> match the condition on COLUMN1 (there are probably around 5 of them) and
> then test them to see if they also meet the conditions on COLUMN2.
> This, obviously, is much faster.
> On the other hand, imagine if the selectivity on the COLUMN1 index was
> 50,000. In this case, using the index would probably slow down the
> search. Why? Because it's faster for InterBase to scan records in
> NATURAL order (that is, the order in which the records are stored on
> disk) because moving the disk drive read head takes a lot of time,
> relatively speaking. If the index is selective enough that using it
> significantly lowers the number of records that must be scanned, then
> it's worth reading the records out of order since we have to read so
> much fewer of them. But if we're going to read most or all of the
> records anyway, it's faster to read them in whatever order they happen
> to be in on disk than to read them in index order.
> This is why indices slow down ORDER BY clauses. It turns out to be
> faster to read the entire result set in storage order and sort the
> records in memory than to read them out of storage order. Even if the
> records must be written to a temp file to perform the sort, it's still
> faster than reading the records out of storage order, because InterBase
> is fairly intelligent in the way it reads and writes temp files.
> The exception to this is when you only intend to retrieve the first few
> records from the result set. In this case, using the index is faster.
> HTH,
> -Craig
> --
> Craig Stuntz (TeamB) Vertex Systems Corp. Columbus, OH
> We're hiring: http://www.vertexsoftware.com/careerops.htm#sd
> Delphi/InterBase WebLog: http://delphi.weblogs.com