0

Not sure if this can be done without UNION. I think it is the UNION which makes it slow. The two parts are quite fast on their own, but combined with the UNION this code takes> 5 seconds to complete. Can it be optimized?

SELECT rl.Updated, c.CultureName, 
"CollectionName" 
as CollectionName, k.KeyName , rl.Value , s.StatusName ,
rl.Comment , rl.Comment as ActionInfo, a.ActionName, u.UserName 
FROM ResourceLog rl 
INNER JOIN `Status` s ON rl.StatusId = s.StatusId 
INNER JOIN Culture c ON rl.CultureId = c.CultureId 
INNER JOIN `Key` k ON rl.KeyId = k.KeyId 
LEFT JOIN `Action` a ON rl.ActionId = a.ActionId 
LEFT JOIN `User` u ON rl.UserId = u.UserId 
UNION SELECT imp.Created as Updated, "Imported" as CultureName, 
"None" as CollectionName, "Imported" as KeyName, 
"Imported" as Value, "Imported" as StatusName, 
"Imported" as Comment, "ActionInfo" as ActionInfo, 
"Imported" as ActionName, UserId 
FROM Import imp 
ORDER BY Updated DESC, KeyName, CultureName limit 50
Ola Ström
3011 gold badge3 silver badges10 bronze badges
asked Feb 11, 2019 at 17:57
2
  • May be UNION ALL if you don't expect duplicates? Commented Feb 11, 2019 at 18:22
  • UNION = Remove duplicates = Sort combined recordset = Filesort = Slow. Commented Feb 11, 2019 at 19:24

1 Answer 1

1

I suspect the combined query is not doing what you think it is doing.

Add parentheses to make it clear whether the second ORDER BY belongs to the second SELECT or to the UNION. Ditto for the LIMIT.

( SELECT ...
)
UNION
( SELECT ... 
 -- Do the ORDER BY and LIMIT belong here?
) 
-- Or here (for the UNION)?

For further discussion, Please provide

EXPLAIN ...
SHOW CREATE TABLE ...
how big are the tables
answered Feb 12, 2019 at 4:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.