Things are all right for me. Usually they are faster than the Union. Put a few variations of your query and compare the package in Plan. Only one (insignificant) information Change
WHERE isnull(CASE pa.KplusTable_Id WHEN 1 THEN sp.BROJ_TIKETA
WHEN 2 THEN fw.BROJ_TIKETA
WHEN 3 THEN s.tiket
WHEN 4 THEN id.BROJ_TIKETA END, '')<>''
for this
WHERE CASE pa.KplusTable_Id WHEN 1 THEN sp.BROJ_TIKETA
WHEN 2 THEN fw.BROJ_TIKETA
WHEN 3 THEN s.tiket
WHEN 4 THEN id.BROJ_TIKETA END is not null
Other sol (with UNION):
SELECT pa.Deal_Id, MAX(Q.Trans_Id) AS TransId_CurrentMax
INTO
FROM
(SELECT 1 A KplusTable_Id, Trans_Id, sp_id AS Deal_Id FROM kplus_sp
UNION
SELECT 2 AS KplusTable_Id, Trans_Id, fw_id AS Deal_Id FROM kplus_fw
UNION
SELECT 3 AS KplusTable_Id, Trans_Id, sw_Id AS Deal_Id FROM dev_sw
UNION
SELECT 4 AS KplusTable_Id, Trans_Id, ia_id AS Deal_Id FROM kplus_ia) AS Q
INNER JOIN
GROUP BY pa.Deal_Id
Check each query option in the plan and choose faster
source
share