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

  • SEARCH
  • Home
  • 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 7845091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:06:44+00:00 2026-06-02T17:06:44+00:00

I have a database table that represents a hierarchy of files and directories, with

  • 0

I have a database table that represents a hierarchy of files and directories, with the following structure (simplified):

ItemId        int
Path          text
Type          int        (0 for files, 1 for directories)
ParentId      int
BackupTime    datetime

Currently the BackupTime column is only used for files, it is set to null for directories.

Now I need to fill this column for directories as well: it must be the minimum BackupTime of all descendants (files and directories).

This (naive and inefficient) query illustrates what I want to do:

update Items i
set BackupTime = (select min(BackupTime)
                  from Items d
                  where d.Path like i.Path || '%'
                  and d.Type = 0)
where i.Type = 1

My problem is that I can’t seem to find an efficient approach. The query above takes much too long on large volumes of data (this table often contains more than 100K rows)

It would probably be faster to search the min(BackupTime) only on direct children:

update Items i
set BackupTime = (select min(BackupTime)
                  from Items d
                  where d.ParentId = i.ItemId)
where i.Type = 1

But for this to work, I must ensure that descendants will be updated before their ancestors, so I must walk the hierarchy recursively from bottom up. The problem is that I have no easy way of knowing which items are the deepest in the hierarchy. I’m using SQLite, so I can’t use hierarchical queries.

Any idea on how to do this efficiently?

Ideally, I’d prefer to be able to do it in a single UPDATE query, but if it’s not possible I’m open to other options, as long as they’re efficient

  • 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-06-02T17:06:45+00:00Added an answer on June 2, 2026 at 5:06 pm

    This is a shot in the dark, but it might work. It’s an attempt to handle the bottom-up issue manually. (I don’t know sqlite’s limitations, but this is probably standard SQL-92 and hopefully ok.)

    Step 1: Decide how you want to handle empty directories. I think the solution here works only if there are no empty directories or if empty directories are initially updated so they have an artificial non-NULL BackupTime. (What that artificial BackupTime should be may be important, depending on how you maintain the BackupDate column when there are changes to your data. Using the current date or an artificial future date might work, but you should think about it.)

    Step 2. Execute the following query repeatedly until no more rows are affected:

      update Items i set
        BackupTime = (
          select min(BackupTime)
          from Items d
          where d.ParentId = i.ItemId
        )
      where i.Type = 1
      and i.BackupTime is null
      and not exists (
        select *
        from Items d
        where d.ParentId = i.ItemId
        and d.Type = 1
        and d.BackupTime is null
      )
    

    In other words, update the BackupTime for directories when you need to and also have all the information: when their BackupTime is null and they contain no subdirectories whose BackupTime value is also null.

    So the first time you run this, it will set the BackupTime for all directories that contain no subdirectories, only files. The second time, it will set the BackupTime for directories that contain subdirectories, but no sub-subdirectories.

    You might be able to handle the empty directory problem by setting BackupTime to coalesce((select…),current_timestamp).

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

Sidebar

Related Questions

Suppose I have an array that mimics a database table. Each array element represents
I have a table in my database that represents datafields in a custom form.
I have a database table that represents Events. The table has 2 main fields
Say I have a schema that represents a fixed-depth hierarchy like this: CREATE TABLE
I have a table in a database that represents dates textually (i.e. 2008-11-09) and
I have a database table that has a Unique Key constraint defined to avoid
I have a database table that is a dictionary of defined terms -- key,
that my problem: I have database table like that: id (AI) market_id 1 6
I have a large database table that I need to display on a Windows
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName

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.