Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6544365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:27:24+00:00 2026-05-25T11:27:24+00:00

while implementing an inline search function for our local file archive I’ve come up

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:27:25+00:00Added an answer on May 25, 2026 at 11:27 am

    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 df
    

    Both 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have met an interesting problem while implementing the Observer pattern with C++ and
I herewith a problem that i am facing while implementing PDF file in iPhone.
I have a problem while implementing the marker controlled watershed in Matlab. The input
While implementing XML file reading/writing in my application I saw that when I call
While implementing a FIFO I have used the following structure: struct Node { T
I am getting following exception while implementing mail feature for my local machine please
While implementing my own IFilter I found that most likely some consumers will require
I am trying to come up with good design while implementing Store Kit. Trying
We found this issue while implementing a reconnect logic for a WS endpoint using
While implementing a WCF client endpoint, my config file had the following endpoint entry:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.