How to create index in firebird for improvement of

2019-08-30 06:18发布

I am newbie in using firebird. I have 3 tables.

for T_TABLE1 the structure is shown below:

enter image description here

for T_TABLE2 the structure is shown below:

enter image description here

and for T_TABLE3 the structure is like this:

enter image description here

the primary key is the ID from T_TABLE1 to TBL1_ID in T_TABEL2 and TBL1_ID in T_TABLE3. How can I select data join with that 3 tables and I want to use index for best select query but I don't know how to create index because I am newbie in using firebird and I want to learn more in using firebird. Hope my explanation is clear.

1条回答
在下西门庆
2楼-- · 2019-08-30 06:57

The syntax for creating an index is documented in The Firebird 2.5 Language Reference, CREATE INDEX, it also contains additional information.

The syntax for creating an index is:

CREATE [UNIQUE] [ASC[ENDING] | [DESC[ENDING]] INDEX indexname
   ON tablename
   { (<col> [, <col> ...]) | COMPUTED BY (expression) }

<col>  ::=  a column not of type ARRAY, BLOB or COMPUTED BY

So for creating an (ascending) index on T_TABEL2.TBL1_ID you would do:

CREATE INDEX ix_tabel2_tbl1_id ON T_TABEL2 (TBL1_ID)

But as I commented earlier today that is not necessary if there is a foreign key on this column, as foreign keys in Firebird automatically get an index (the same applies to primary keys).

It is not clear from your question if you have foreign keys, but I'd advise you to create them instead of an index: it gives you the index and on top of that enforces that the value of TBL1_ID actually exists in T_TABLE1.

Just keep in mind that creating an index does not automatically improve performance. The optimizer for example might decide that using an index is not worth the effort (or that a specific index is not relevant for the query).

查看更多
登录 后发表回答