Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL optimization issue

I have two tables. One table contains information about Assets, another Table about their relation. How could I optimize current query and getting similar results.

SELECT a1.ID FROM Asset a1
WHERE a1.AssetId =
(SELECT r.DestinationAssetId
FROM Relation r
INNER JOIN Asset a2 ON a2.AssetId = r.SourceAssetId
WHERE a2.ID = '1112174' and r.RelationshipType = 'Video File')

Results: 13412331 (ID of Asset which is related to a2.ID = '1125574')

Personally I don't like this stupid sub query, is it any way I can avoid it and optimize this query.

Thanks!

like image 648
Wild Goat Avatar asked Jan 30 '26 22:01

Wild Goat


1 Answers

You can lose the subquery:

SELECT dest.ID 
FROM 
Asset src
JOIN Relation r ON src.AssetId = r.SourceAssetId
JOIN Asset dest ON dest.AssetID = r.DestinationAssetID
WHERE src.ID = '1112174' and r.RelationshipType = 'Video File'

Its not much of an optimization performance wise, but it is a little neater.

like image 135
Jon Egerton Avatar answered Feb 02 '26 12:02

Jon Egerton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!