Subject | Re: [ib-support] some index questions |
---|---|
Author | Helen Borrie |
Post date | 2001-11-19T11:35:17Z |
At 10:00 AM 19-11-01 +0000, Tom Deprez wrote:
Some relational databases - such as Paradox - don't support FOREIGN key relationships and rely on composite primary keys to form hierarchical relationships. Such is not the case with Firebird/InterBase, whose generator gives you a bomb-proof primary key and whose relationships can be made with gossamer-fine key linkages.
A column which is a key (primary or foreign) will have an ascending index created for it automatically. Don't create another ascending index for it, otherwise you will kill the optimizer. Create a descending index later, if you need one, but wait until you have finished creating all the FOREIGN keys that refer to that column or you may well end up back here in this list complaining about all the hair you have lost.
Also, watch out for index selectivity. Don't put foreign keys or indexes on columns where, in a table of thousands of rows, there are only a few possible values. Instead, make a composite index on that column + the primary key, in that order.
For example, in an invoice detail table you might have a column TRANSACTION_TYPE. There are only 5 possible transaction types, so don't make a foreign key on this column. (If you do, Firebird will create an index on it and this index over time will form huge duplicate chains and slow your invoice operations to a death march).
Instead, if you need, for instance, to improve the performance of a query that is sorted by transaction type, form a highly selective index on this column: pick up the primary key column (call it INV_DETAIL_ID) and form an index by adding it to the right-hand side of TRANSACTION_TYPE, e.g.
create index IX_TRANS_TYPE on InvoiceDetail(TRANSACTION_TYPE, INV_DETAIL_ID);
cheers,
Helen
All for Open and Open for All
InterBase Developer Initiative ยท http://www.interbase2000.org
_______________________________________________________
>Hi,Generally, yes. Primary keys should be atomic (have no meaning except to form the key) and they should be short. A simple primary key is also makes foreign key relationships much simpler and index sorting and matching faster.
>
>Is it always better to use a generated field as a primary key?.
>Let say that in a table you can create a primary key with the first 3No, it will bring a speed increase because of size and add a lot of complexity and overload to joins. It is also possible to clue up with an impossible index, since index lengths are limited to 253 bytes.
>or 4 fields, will this bring a speed reduction?
>Or is it just by experience that you better can use seperate (autoGenerators make fantastically marvellous keys. Use them wherever you can. It's OK to use smaller keys than Int64 on small, static sets such as control tables, where you might as well use smallint or char() keys of 1 to 3 characters, e.g. in a table of transaction types.
>generated) primary keys. I've read books/articles and one says you
>need to use a generated key and another one says that if you can
>avoid a generated key, you need that one...
Some relational databases - such as Paradox - don't support FOREIGN key relationships and rely on composite primary keys to form hierarchical relationships. Such is not the case with Firebird/InterBase, whose generator gives you a bomb-proof primary key and whose relationships can be made with gossamer-fine key linkages.
>Creating indexes:All indexes have a sorting order. The default order is ASC(ending). You only need to specify the sorting order if you want a DESC(ending) index.
>
>if you create an index. no sorting order, will it than be slower than
>the same key with a descending or ascending order?
>So, if I need a descending and a descending order key for that field,Don't confuse keys with indexes. We are talking about indexes here, not keys. A key is a constraint, a pure and abstract thing; an index is a live and writhing animal with all the concommitant bodily functions.
>then I create 2 keys (desc, asc) or 1 key with no order.
A column which is a key (primary or foreign) will have an ascending index created for it automatically. Don't create another ascending index for it, otherwise you will kill the optimizer. Create a descending index later, if you need one, but wait until you have finished creating all the FOREIGN keys that refer to that column or you may well end up back here in this list complaining about all the hair you have lost.
>Is Firebird smart enough to use a key or do you always have toYour question should be "Is Firebird smart enough to use an index?" Yes, Firebird is usually smart enough to use an index if it will be faster than using natural order. But don't make it hard for the optimizer by creating redundant indexes on primary and foreign key columns.
>explecit say that it has to use that key.
Also, watch out for index selectivity. Don't put foreign keys or indexes on columns where, in a table of thousands of rows, there are only a few possible values. Instead, make a composite index on that column + the primary key, in that order.
For example, in an invoice detail table you might have a column TRANSACTION_TYPE. There are only 5 possible transaction types, so don't make a foreign key on this column. (If you do, Firebird will create an index on it and this index over time will form huge duplicate chains and slow your invoice operations to a death march).
Instead, if you need, for instance, to improve the performance of a query that is sorted by transaction type, form a highly selective index on this column: pick up the primary key column (call it INV_DETAIL_ID) and form an index by adding it to the right-hand side of TRANSACTION_TYPE, e.g.
create index IX_TRANS_TYPE on InvoiceDetail(TRANSACTION_TYPE, INV_DETAIL_ID);
cheers,
Helen
All for Open and Open for All
InterBase Developer Initiative ยท http://www.interbase2000.org
_______________________________________________________