tvrdak
asked on
Optimization problem on FB when use more indexes to retrieve data
Hi,
I have optimization problem on FB(1.5.2 on win). I have dynamicly built query of 2 tables - headers and rows. When optimizer decide to proceed first header then rows, then data from rows are taken via 2 indexes - first is based on foreign key from rows to headers (this is OK) and second is based on foreign key to other table with about 100 valuse. It is added based on another condition in query.
This second index has vary bad optimization influence - instead seconds query will take minutes.
Is there in FB any way to solve it ? I am meaning to tell fb to use only one index to retrieve data from table.
And in which case use of more indexes for fetching could bring some effect ?
thanks, tvr
I have optimization problem on FB(1.5.2 on win). I have dynamicly built query of 2 tables - headers and rows. When optimizer decide to proceed first header then rows, then data from rows are taken via 2 indexes - first is based on foreign key from rows to headers (this is OK) and second is based on foreign key to other table with about 100 valuse. It is added based on another condition in query.
This second index has vary bad optimization influence - instead seconds query will take minutes.
Is there in FB any way to solve it ? I am meaning to tell fb to use only one index to retrieve data from table.
And in which case use of more indexes for fetching could bring some effect ?
thanks, tvr
please post the query and plan
ASKER
Query is (as an example, it is dynamicly builded based on given conditions):
SELECT A.ID
FROM
StoreDocuments2 A
JOIN StoreDocuments SD ON SD.ID=A.Parent_ID
LEFT JOIN StoreCards SC ON SC.ID=A.StoreCard_ID
WHERE
A.RowType=3
AND
(
(SD.DocQueue_ID IN ('1F00000101','7E10000101' ,'8F000001 01','KC000 00101','LC 00000101', 'NC0000010 1') )
AND (SD.DocDate$DATE >= 38808 and SD.DocDate$DATE < 38899 )
AND (A.Store_ID IN ('1000000101','1200000101' ,'26000001 01','27000 00101','29 00000101', '300000010 1','310000 0101','370 0000101',' 3800000101 ','4000000 101','4100 000101','4 500000101' ,'50000001 01','51000 00101','52 00000101', '600000010 1','700000 0101','710 0000101',' 8000000101 ','8100000 101','9000 000101','9 100000101' ,'A1000001 01','B0000 00101','B1 00000101', 'C00000010 1','C10000 0101','D00 0000101',' F000000101 ','F200000 101','I100 000101','K 100000101' ,'L2000001 01','M0000 00101','M1 00000101', 'M20000010 1','N20000 0101','P00 0000101',' R100000101 ','U100000 101','Y100 000101','Z 100000101' ))
)
ORDER BY SC.Code ASC NULLS FIRST,A.StoreCard_ID ASC NULLS FIRST
========================== ========== ========== ========== ========== ========== =
Plan is (I removed repeated index items was there due to IN clause)
PLAN SORT (JOIN (JOIN (SD INDEX (STOREDOCUMENTS_DD,STOREDO CUMENTSDOC QUEUEFK),A INDEX (STOREDOCUMENTS2PARENTFK,S TOREDOCUME NTS2STOREF K)),SC INDEX (STORECARDSPK)))
------ Performance info ------
Prepare time = 0ms
Execute time = 47m 38s 625ms
Avg fetch time = 37,49 ms
Current memory = 17 036 304
Max memory = 17 417 108
Memory buffers = 2 048
Reads from disk to cache = 31 759 375
Writes from cache to disk = 0
Fetches from cache = 34 056 084
========================== ========== ========== ========== ========== ========== =
when PLAN is
PLAN (SORT (JOIN (JOIN (SD INDEX (STOREDOCUMENTS_DD),A INDEX (STOREDOCUMENTS2PARENTFK)) ,SC INDEX (STORECARDSPK))))
then result is (this result is just after start FB, otherways it take about 10 seconds):
------ Performance info ------
Prepare time = 0ms
Execute time = 54s 281ms
Avg fetch time = 0,71 ms
Current memory = 1 772 740
Max memory = 1 873 264
Memory buffers = 2 048
Reads from disk to cache = 31 904
Writes from cache to disk = 0
Fetches from cache = 639 412
========================== ========== ========== ========== ========== ========== =
So seams to me is very uneffective to use more indexes for fetching records from stream A (there are rows there)
A INDEX (STOREDOCUMENTS2PARENTFK,S TOREDOCUME NTS2STOREF K)
but don't know, where to take influence on it if it si even possible
btw - I don't want to discharge condition based on Store_ID from optimization - when there will be other conditions and optimizer will prepare plan starting from stream A, then it could be an advantage to have it in plan
SELECT A.ID
FROM
StoreDocuments2 A
JOIN StoreDocuments SD ON SD.ID=A.Parent_ID
LEFT JOIN StoreCards SC ON SC.ID=A.StoreCard_ID
WHERE
A.RowType=3
AND
(
(SD.DocQueue_ID IN ('1F00000101','7E10000101'
AND (SD.DocDate$DATE >= 38808 and SD.DocDate$DATE < 38899 )
AND (A.Store_ID IN ('1000000101','1200000101'
)
ORDER BY SC.Code ASC NULLS FIRST,A.StoreCard_ID ASC NULLS FIRST
==========================
Plan is (I removed repeated index items was there due to IN clause)
PLAN SORT (JOIN (JOIN (SD INDEX (STOREDOCUMENTS_DD,STOREDO
------ Performance info ------
Prepare time = 0ms
Execute time = 47m 38s 625ms
Avg fetch time = 37,49 ms
Current memory = 17 036 304
Max memory = 17 417 108
Memory buffers = 2 048
Reads from disk to cache = 31 759 375
Writes from cache to disk = 0
Fetches from cache = 34 056 084
==========================
when PLAN is
PLAN (SORT (JOIN (JOIN (SD INDEX (STOREDOCUMENTS_DD),A INDEX (STOREDOCUMENTS2PARENTFK))
then result is (this result is just after start FB, otherways it take about 10 seconds):
------ Performance info ------
Prepare time = 0ms
Execute time = 54s 281ms
Avg fetch time = 0,71 ms
Current memory = 1 772 740
Max memory = 1 873 264
Memory buffers = 2 048
Reads from disk to cache = 31 904
Writes from cache to disk = 0
Fetches from cache = 639 412
==========================
So seams to me is very uneffective to use more indexes for fetching records from stream A (there are rows there)
A INDEX (STOREDOCUMENTS2PARENTFK,S
but don't know, where to take influence on it if it si even possible
btw - I don't want to discharge condition based on Store_ID from optimization - when there will be other conditions and optimizer will prepare plan starting from stream A, then it could be an advantage to have it in plan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if stats are up to date try adding "and 1 = 1" to the join condition you don't want the index to be used.
If upgrading to firebird 2+ is an option this would probably fix it as much work has been done on the optimiser
If upgrading to firebird 2+ is an option this would probably fix it as much work has been done on the optimiser
Forced accept.
Computer101
EE Admin
Computer101
EE Admin