I'm using Firebird 2.1 and I'd need some help optimizing this query: (maybe by replacing IN-s with JOINS or something to speed it up, as it is very slow)
SELECT ClientID, ClientType, ClientName
FROM Clients
WHERE
(
AccessRights = 0 OR
OwnerUserID = :uid OR
(
AccessRights = 2 AND
ClientID IN (SELECT ClientID
FROM ClientRights
WHERE UserID = :uid)
)
)
AND ClientID IN (SELECT CC.ClientID
FROM CaseClients CC
WHERE CC.CaseID IN (SELECT DISTINCT CaseID
FROM TimeSheet
WHERE IsBilled = 0)
AND CC.ClientToBill = 1
AND (SELECT BillingType
FROM Cases
WHERE CaseID = CC.CaseID) = 2
);
Thanks!