I would like to optimize this SQL statement:
SELECT hyobjects_id, hyobjects_globalpriority
FROM hyobjects, hyobjectypes, actionlltypes
WHERE hyobjects_hyobjectypes = hyobjectypes_id
AND hyobjectypes_actionlltypes = actionlltypes_id
AND actionlltypes_id = 1
UNION
SELECT hyobjects_id, hyobjects_globalpriority
FROM hyobjects, actionstatuses, actionlltypes
WHERE actionstatuses_id = hyobjects_actionstatuses
AND actionstatuses_actionlltypes = actionlltypes_id
AND actionlltypes_id = 1
EXCEPT
SELECT hyobjects_id, hyobjects_globalpriority
FROM hyobjects, actionstatuses, actionlltypes
WHERE hyobjects_actionstatuses = actionstatuses_id
AND actionstatuses_actionlltypes = actionlltypes_id
AND actionlltypes_id = 2
UNION
SELECT hyobjects_id, hyobjects_globalpriority
FROM hyobjects, hyobjectypes, actionstatuses, actionlltypes
WHERE hyobjects_hyobjectypes = hyobjectypes_id
AND hyobjectypes_actionlltypes = actionlltypes_id
AND actionlltypes_id = 1
AND hyobjects_actionstatuses = 1
ORDER BY hyobjects_globalpriority DESC
where by the table actionlltypes
has actionlltypes_id = 1
which means “ACTIVE” and actionlltypes_id = 2
which means “INACTIVE”.
And where column hyobjectypes_actionlltypes
references actionlltypes
and column hyobjects_actionstatuses
references actionlltypes
. I need result where actionstatuses_id
may be 1 or having actionlltypes_id = 1
.