while implementing an inline search function for our local file archive I’ve come up with a serious problem I have found no answer for. We have two tables:
fild_id | file_name
---------------------
1 | this_file
2 | that_file
3 | new_file
file_archive_id | file_archive_version | file_id
--------------------------------------------------
1 | 1 | 1
2 | 2 | 1
3 | 1 | 2
4 | 1 | 3
5 | 3 | 1
I want to join both tables via file_id, selecting only the one file_archive row with the biggest file_archive_version:
fild_id | file_name | file_archive_id | file_archive_version
--------------------------------------------------------------
1 | this_file | 5 | 3
2 | that_file | 3 | 1
3 | new_file | 4 | 1
Is there any possibility to do this via a single select statement?
Solution:
SELECT df.*,
(
SELECT dfa.file_archive_id
FROM dca_file_archive dfa
WHERE df.file_id = dfa.file_id
ORDER BY dfa.file_archive_version desc LIMIT 1
) as file_archive_id,
(
SELECT dfa.file_archive_version
FROM dca_file_archive dfa
WHERE df.file_id = dfa.file_id
ORDER BY dfa.file_archive_version desc LIMIT 1
) as file_archive_version
FROM dca_file df
Both tables having ~16k rows, this statement takes 0.9 seconds to perform, which is 120x faster than the first join solution.
Solution (without altering the indexes on my tables):
SELECT df.*, ( SELECT dfa.file_archive_id FROM dca_file_archive dfa WHERE df.file_id = dfa.file_id ORDER BY dfa.file_archive_version desc LIMIT 1 ) as file_archive_id, ( SELECT dfa.file_archive_version FROM dca_file_archive dfa WHERE df.file_id = dfa.file_id ORDER BY dfa.file_archive_version desc LIMIT 1 ) as file_archive_version FROM dca_file dfBoth tables having ~16k rows, this statement takes 0.9 seconds to perform, which is 120x faster than the first join solution.
I know this is not the finest you can do with SQL