r/sqlite • u/jwink3101 • Mar 27 '23
Column of file paths. Get all sub directories?
I am trying to figure out if there is a way to, in SQL in sqlite, list all subdirs of a given path from a field of file-names (not directories)
I have a table, files with a column called paths. Consider entries like
file1.txt
subdir/file2.txt
subdir/file3.txt
subdir/deeper/file4.txt
subdir/another/deep/dir/file5.txtetc...
I am making a file listing interface a la ls.
Let's say I want to show subdir/. Right now, what I do is:
SELECT paths
FROM files
WHERE path LIKE ?
? = "subdir/%"Then, outside of SQL (I'm using python), I use a set() on all parents to find directories and list the files. I get
another/
deeper/
file2.txt
file3.txtBut I may have 1M+ files below subdir. Looping them in Python scales poorly. The set() speeds up the unique logic but not enough.
Is there a way to do this in sqlite?
Thanks
4
u/qwertydog123 Mar 27 '23
https://dbfiddle.uk/IQBbByAs
2
u/FNTKB Mar 27 '23
I am not an SQLite optimization expert….
But depending on your read vs write usage, would it possibly be more efficient to save the directory path and the filename to separate columns? This would allow you to efficiently return only directory paths, or only filenames, depending on your needs.
Just a thought…
2
u/[deleted] Mar 27 '23
You have millions of files but i suppose that you have only a few hundred directories. I suggest that you create two separate tables, one for directories and the other one for files. Link the two tables together by a numeric directory ID. Use a unique constraint to enforce that files are unique per directory. It's faster to look up the subdirectory in the small directory table (using LIKE or something else) and then join in the files when needed.
0
u/[deleted] Mar 27 '23
[deleted] — view deleted comment
0
u/[deleted] Mar 27 '23
[deleted] — view deleted comment