Performance tune my sql query removing OR statements
I need to p开发者_如何学编程erformance tune following SQL query by removing "OR" statements
Please help ...
SELECT a.id, a.fileType, a.uploadTime, a.filename, a.userId, a.CID, ui.username, company.name companyName, a.screenName
FROM TM_transactionLog a, TM_userInfo ui, TM_company company, TM_airlineCompany ac
WHERE
(
a.CID = 3049
)
OR
(
a.CID = company.ID
AND
ac.SERVICECID = 3049
AND
company.SPECIFICCID = ac.ID
)
OR
(
a.USERID = ui.ID
AND
ui.CID = 3049
);
Are you sure this is the query you want? You do know that the first OR condition does not even have a join defined so it will do a cartesian join on all the tables. I'm not sure if that's by design and you don't expect the join criteria to be met for that OR condition. Because it looks like your multi-table select columns suggest that you are expecting some sort of join and if that's the case you should start out with a query that looks like this..
SELECT a.id,
a.filetype,
a.uploadtime,
a.filename,
a.userid,
a.cid,
ui.username,
company.name companyname,
a.screenname
FROM tm_transactionlog a,
tm_userinfo ui,
tm_company company,
tm_airlinecompany ac
WHERE a.userid = ui.id
AND a.cid = company.id
AND company.specificcid = ac.id
AND ( a.cid = 3049
OR ac.servicecid = 3049
OR ui.cid = 3049 );
And as far as optimizing this query, I think there is very little optimization you can do without changing your data model. If you already don't have indexes, I would suggest creating a composite index on (a.userid,a.cid <- the column with the higher cardinality should go first), and indexes on ac.cservicecid,company.specificcid & ui.cid.
It would be really useful to see the table structure for this - I'm sure you could remove a lot of WHERE clause code by joining your tables.
精彩评论