Subject | Two different plans used - Why?!? |
---|---|
Author | rjschappe |
Post date | 2005-03-03T17:50:58Z |
My query is taking far too long and from the plan I can see why...
FB 1.5 - SuperServer
it is performing a Natural scan on my Customer table and my CustOrder
is using the Customer index before my InvType (HS - highly selective
index InvType + tablePK)
******************************************************
Here is the "bad one" - see blow for the "good" one...
//bad index usage - 45 seconds to run
SELECT CustOrder.ID, CustOrder.InvoiceNo, CustOrder.ShipDate,
Address.Company, Customer.Identifier, CustOrder.Descript,
CustOrder.PO, CustOrder.InvType, Customer.ActiveCust,
Customer.Distributor, CustOrder.TempSystem
FROM
Customer
JOIN CustOrder on CustOrder.CUSTOMER = Customer.ID
JOIN Address ON Customer.Address = Address.ID
WHERE (CustOrder.InvType IN (1,2,4,5))
AND Upper(CustOrder.PO) LIKE 'S%'
//plan used
//PLAN JOIN (CUSTOMER NATURAL,ADDRESS INDEX (PK_ADDRESS),CUSTORDER
INDEX
(FK_CUSTORDER_CUSTOMER,I_CUSTORDER_INVTYPE_ID_HS,I_CUSTORDER_INVTYPE_ID_HS,I_CUSTORDER_INVTYPE_ID_HS,I_CUSTORDER_INVTYPE_ID_HS))
******************************************************
Here is the "good" one... I had to mess around with the InvType... but
I do _not_ want to have to play such games as this... I want to to
just use CustOrder.InvType IN (1,2,4,5) as everyone would expect!
//good index usage - 2.5 seconds
SELECT CustOrder.ID, CustOrder.InvoiceNo, CustOrder.ShipDate,
Address.Company, Customer.Identifier, CustOrder.Descript,
CustOrder.PO, CustOrder.InvType, Customer.ActiveCust,
Customer.Distributor, CustOrder.TempSystem
FROM CustOrder
JOIN Customer ON CustOrder.Customer = Customer.ID
JOIN Address ON Customer.Address = Address.ID
WHERE ((CustOrder.InvType<6) and (CustOrder.InvType<>3))
AND Upper(CustOrder.PO) LIKE 'S%'
//plan used
//PLAN JOIN (CUSTORDER INDEX (I_CUSTORDER_INVTYPE_ID_HS),CUSTOMER
INDEX (PK_CUSTOMER),ADDRESS INDEX (PK_ADDRESS))
Notice, no Customer Natural scan and the highly selective InvType
index is used
What am I doing wrong that I cannot just use the normal
CustOrder.InvType IN (1,2,4,5) in my WHERE statement?
Thanks for any help
--Raymond
FB 1.5 - SuperServer
it is performing a Natural scan on my Customer table and my CustOrder
is using the Customer index before my InvType (HS - highly selective
index InvType + tablePK)
******************************************************
Here is the "bad one" - see blow for the "good" one...
//bad index usage - 45 seconds to run
SELECT CustOrder.ID, CustOrder.InvoiceNo, CustOrder.ShipDate,
Address.Company, Customer.Identifier, CustOrder.Descript,
CustOrder.PO, CustOrder.InvType, Customer.ActiveCust,
Customer.Distributor, CustOrder.TempSystem
FROM
Customer
JOIN CustOrder on CustOrder.CUSTOMER = Customer.ID
JOIN Address ON Customer.Address = Address.ID
WHERE (CustOrder.InvType IN (1,2,4,5))
AND Upper(CustOrder.PO) LIKE 'S%'
//plan used
//PLAN JOIN (CUSTOMER NATURAL,ADDRESS INDEX (PK_ADDRESS),CUSTORDER
INDEX
(FK_CUSTORDER_CUSTOMER,I_CUSTORDER_INVTYPE_ID_HS,I_CUSTORDER_INVTYPE_ID_HS,I_CUSTORDER_INVTYPE_ID_HS,I_CUSTORDER_INVTYPE_ID_HS))
******************************************************
Here is the "good" one... I had to mess around with the InvType... but
I do _not_ want to have to play such games as this... I want to to
just use CustOrder.InvType IN (1,2,4,5) as everyone would expect!
//good index usage - 2.5 seconds
SELECT CustOrder.ID, CustOrder.InvoiceNo, CustOrder.ShipDate,
Address.Company, Customer.Identifier, CustOrder.Descript,
CustOrder.PO, CustOrder.InvType, Customer.ActiveCust,
Customer.Distributor, CustOrder.TempSystem
FROM CustOrder
JOIN Customer ON CustOrder.Customer = Customer.ID
JOIN Address ON Customer.Address = Address.ID
WHERE ((CustOrder.InvType<6) and (CustOrder.InvType<>3))
AND Upper(CustOrder.PO) LIKE 'S%'
//plan used
//PLAN JOIN (CUSTORDER INDEX (I_CUSTORDER_INVTYPE_ID_HS),CUSTOMER
INDEX (PK_CUSTOMER),ADDRESS INDEX (PK_ADDRESS))
Notice, no Customer Natural scan and the highly selective InvType
index is used
What am I doing wrong that I cannot just use the normal
CustOrder.InvType IN (1,2,4,5) in my WHERE statement?
Thanks for any help
--Raymond