INFORMIX-SQL 4.10.DC1 (SE Engine), on DOS 6.22, on Microsoft Virtual PC 2007, on Windows 7!
EDIT: Looking for Jonathan Leffler wisdom on this one!
I have a child table called a transaction and a parent table called a client.
Client.pk_id SERIAL = transaction.fk_id INTEGER joins these tables.
The transaction table has a clustered index at fk_id, so each client transaction is physically grouped together with the data corresponding to the index. The reason I chose the cluster index for fk_id is because most requests are executed by client name, and this quickly returns all the transactions belonging to the requested client.
The transaction table also has a column named transaction_number SERIAL with a unique index.
When a new transaction is added for any client, a new row with the highest transaction number is added to the EOF. Note. Informix does not support physical clustering in the data file after index creation.
Therefore, when I want to print out the client’s receipt of the last transaction, I make the following request:
SELECT *
FROM customer c,
transaction t
WHERE c.pk_id = t.fk_id
AND t.trx_num = (SELECT MAX(trx_num)
FROM transaction)
EDIT: The following INDICES exist:
UNIQUE INDEX ON customer(pk_id) {SERIAL}
UNIQUE CLUSTER INDEX ON customer(last_name,sur_name,first_name,middle_name) {CHAR(30's)}
UNIQUE INDEX ON customer(ident_type,ident_num,ident_state,ident_country) {CHARS(n)}
UNIQUE CLUSTER INDEX ON transaction(fk_id) {INT}
UNIQUE INDEX ON transaction(trx_num) {SERIAL}
EDIT: EXPLAIN results for the above query:
QUERY:
SELECT *
FROM customer c,
transaction t,
WHERE c.pk_id = t.fk_id
AND t.trx_num = (SELECT MAX(trx_num)
FROM transaction)
Estimated Cost: 14
Estimated
1) f.transaction: INDEX PATH
(1) Index Keys: trx_num
Lower Index Filter: f.transaction.trx_num = <subquery>
2) f.customer: INDEX PATH
(1) Index Keys: pk_id
Lower Index Filter: f.customer.pk_id = f.transaction.fk_id
Subquery:
Estimated Cost: 3
Estimated
1) f.trx_num: INDEX PATH
(1) Index Keys: trx_num
It seems that the query optimizer performs a full table scan on the transaction to find the highest transaction number, the new line of which is always placed in the EOF, but since the transactions are grouped by fk_id, the remaining transaction numbers are scattered throughout the table.
Is there a better way to get a faster response time for a request?
Did you CREATE UNIQUE INDEX trxnumidx ON transaction(transaction_number) DESCENDINGfind MAX transaction_number quickly?
EDIT: , , , , :
SELECT *
FROM customer c,
transaction t
WHERE c.pk_id = t.fk_id
AND t.trx_num = $trxnum {ace input variable, type INT}
, , , ( = 2), , MAX ( = 14). , , , , , !
EDIT: DBINFO('sqlca.sqlerrd1') ?