30

A product that I am working on collects several thousand readings a day and stores them as 64k binary files on a NTFS partition (Windows XP). After a year in production there is over 300000 files in a single directory and the number keeps growing. This has made accessing the parent/ancestor directories from windows explorer very time consuming.

I have tried turning off the indexing service but that made no difference. I have also contemplated moving the file content into a database/zip files/tarballs but it is beneficial for us to access the files individually; basically, the files are still needed for research purposes and the researchers are not willing to deal with anything else.

Is there a way to optimize NTFS or Windows so that it can work with all these small files?

Jon 'links in bio' Ericson
  • 20,880
  • 12
  • 98
  • 148
Lawrence Barsanti
  • 31,929
  • 10
  • 46
  • 68

14 Answers14

36

NTFS actually will perform fine with many more than 10,000 files in a directory as long as you tell it to stop creating alternative file names compatible with 16 bit Windows platforms. By default NTFS automatically creates an '8 dot 3' file name for every file that is created. This becomes a problem when there are many files in a directory because Windows looks at the files in the directory to make sure the name they are creating isn't already in use. You can disable '8 dot 3' naming by setting the NtfsDisable8dot3NameCreation registry value to 1. The value is found in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\FileSystem registry path. It is safe to make this change as '8 dot 3' name files are only required by programs written for very old versions of Windows.

A reboot is required before this setting will take effect.

Dan Finucane
  • 1,547
  • 2
  • 18
  • 27
  • 6
    Turning off 8 dot 3 is recommended above 300,000 files. http://technet.microsoft.com/en-us/library/cc778996(WS.10).aspx You can change the behavior from the command line on newer versions of windows, e.g. `fsutil 8dot3name set 1`. – Eric J. Oct 29 '14 at 22:03
  • 1
    Not sure what it said for WinXP, but now on Win10 the tool says: `This operation takes effect immediately (no reboot required)` – Paul Aug 02 '20 at 01:21
29

NTFS performance severely degrades after 10,000 files in a directory. What you do is create an additional level in the directory hierarchy, with each subdirectory having 10,000 files.

For what it's worth, this is the approach that the SVN folks took in version 1.5. They used 1,000 files as the default threshold.

Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
  • I know a lot of people recommended this approach but I chose this answer because it cites a reputable software project. Thanks for all the responses. – Lawrence Barsanti Sep 22 '08 at 18:07
  • 9
    Do you have a link explaining why performance severely degrades after 10,000 files? – Jon-Eric Feb 05 '13 at 17:51
  • 2
    With NTFS, you can handle tens of millions of files before needing to create subfolders http://stackoverflow.com/a/291292/141172 – Eric J. Oct 29 '14 at 22:01
  • 1
    @LawrenceBarsanti: SVN is not designed to run on NTFS only, but rather on a range of file systems. Older file systems ran into the problem of needing to create subfolders much faster than NTFS does. – Eric J. Oct 29 '14 at 22:04
  • 2
    Keep in mind, the original answer is 7 years old and hard drives are *significantly* faster these days. – Adam Tegen Oct 31 '14 at 01:20
  • Is this still true on Windows 10 1903? – Naikrovek Oct 02 '19 at 19:08
9

The performance issue is being caused by the huge amount of files in a single directory: once you eliminate that, you should be fine. This isn't a NTFS-specific problem: in fact, it's commonly encountered with user home/mail files on large UNIX systems.

One obvious way to resolve this issue, is moving the files to folders with a name based on the file name. Assuming all your files have file names of similar length, e.g. ABCDEFGHI.db, ABCEFGHIJ.db, etc, create a directory structure like this:

ABC\
    DEF\
        ABCDEFGHI.db
    EFG\
        ABCEFGHIJ.db

Using this structure, you can quickly locate a file based on its name. If the file names have variable lengths, pick a maximum length, and prepend zeroes (or any other character) in order to determine the directory the file belongs in.

mdb
  • 52,000
  • 11
  • 64
  • 62
  • 3
    It is better to use reverse split in directory names - it will improve search time inside the last directory by eliminating the similar names prefix, e.g.: GHI\DEF\ABCDEFGHI.db – ursa Jul 10 '15 at 13:42
5

I have seen vast improvements in the past from splitting the files up into a nested hierarchy of directories by, e.g., first then second letter of filename; then each directory does not contain an excessive number of files. Manipulating the whole database is still slow, however.

moonshadow
  • 86,889
  • 7
  • 82
  • 122
5

I have run into this problem lots of times in the past. We tried storing by date, zipping files below the date so you don't have lots of small files, etc. All of them were bandaids to the real problem of storing the data as lots of small files on NTFS.

You can go to ZFS or some other file system that handles small files better, but still stop and ask if you NEED to store the small files.

In our case we eventually went to a system were all of the small files for a certain date were appended in a TAR type of fashion with simple delimiters to parse them. The disk files went from 1.2 million to under a few thousand. They actually loaded faster because NTFS can't handle the small files very well, and the drive was better able to cache a 1MB file anyway. In our case the access and parse time to find the right part of the file was minimal compared to the actual storage and maintenance of stored files.

Jason Short
  • 5,205
  • 1
  • 28
  • 45
4

If you can calculate names of files, you might be able to sort them into folders by date, so that each folder only have files for a particular date. You might also want to create month and year hierarchies.

Also, could you move files older than say, a year, to a different (but still accessible) location?

Finally, and again, this requires you to be able to calculate names, you'll find that directly accessing a file is much faster than trying to open it via explorer. For example, saying
notepad.exe "P:\ath\to\your\filen.ame"
from the command line should actually be pretty quick, assuming you know the path of the file you need without having to get a directory listing.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
4

You could try using something like Solid File System.

This gives you a virtual file system that applications can mount as if it were a physical disk. Your application sees lots of small files, but just one file sits on your hard drive.

http://www.eldos.com/solfsdrv/

Ged Byrne
  • 801
  • 6
  • 10
  • This is a cool idea! The EldoS site is gone from the internet. A (trial?) version seems to be available on [Torry.net](https://torry.net/authorsmore.php?id=1060) (not verified or antivirus tested). – chronometric Jul 24 '20 at 04:00
3

One common trick is to simply create a handful of subdirectories and divvy up the files.

For instance, Doxygen, an automated code documentation program which can produce tons of html pages, has an option for creating a two-level deep directory hierarchy. The files are then evenly distributed across the bottom directories.

nsanders
  • 12,250
  • 2
  • 40
  • 47
2

Aside from placing the files in sub-directories..

Personally, I would develop an application that keeps the interface to that folder the same, ie all files are displayed as being individual files. Then in the application background actually takes these files and combine them into a larger files(and since the sizes are always 64k getting the data you need should be relatively easy) To get rid of the mess you have.

So you can still make it easy for them to access the files they want, but also lets you have more control how everything is structured.

Redbaron
  • 1,038
  • 1
  • 10
  • 13
2

Having hundreds of thousands of files in a single directory will indeed cripple NTFS, and there is not really much you can do about that. You should reconsider storing the data in a more practical format, like one big tarball or in a database.

If you really need a separate file for each reading, you should sort them into several sub directories instead of having all of them in the same directory. You can do this by creating a hierarchy of directories and put the files in different ones depending on the file name. This way you can still store and load your files knowing just the file name.

The method we use is to take the last few letters of the file name, reversing them, and creating one letter directories from that. Consider the following files for example:

1.xml
24.xml
12331.xml
2304252.xml

you can sort them into directories like so:

data/1.xml
data/24.xml
data/1/3/3/12331.xml
data/2/5/2/4/0/2304252.xml

This scheme will ensure that you will never have more than 100 files in each directory.

finalman
  • 774
  • 5
  • 14
1

Consider pushing them to another server that uses a filesystem friendlier to massive quantities of small files (Solaris w/ZFS for example)?

Brian Knoblauch
  • 20,639
  • 15
  • 57
  • 92
1

If there are any meaningful, categorical, aspects of the data you could nest them in a directory tree. I believe the slowdown is due to the number of files in one directory, not the sheer number of files itself.

The most obvious, general grouping is by date, and gives you a three-tiered nesting structure (year, month, day) with a relatively safe bound on the number of files in each leaf directory (1-3k).

Even if you are able to improve the filesystem/file browser performance, it sounds like this is a problem you will run into in another 2 years, or 3 years... just looking at a list of 0.3-1mil files is going to incur a cost, so it may be better in the long-term to find ways to only look at smaller subsets of the files.

Using tools like 'find' (under cygwin, or mingw) can make the presence of the subdirectory tree a non-issue when browsing files.

rcreswick
  • 16,483
  • 15
  • 59
  • 70
1

Rename the folder each day with a time stamp.

If the application is saving the files into c:\Readings, then set up a scheduled task to rename Reading at midnight and create a new empty folder.

Then you will get one folder for each day, each containing several thousand files.

You can extend the method further to group by month. For example, C:\Reading become c:\Archive\September\22.

You have to be careful with your timing to ensure you are not trying to rename the folder while the product is saving to it.

Ged Byrne
  • 801
  • 6
  • 10
1

To create a folder structure that will scale to a large unknown number of files, I like the following system:

Split the filename into fixed length pieces, and then create nested folders for each piece except the last.

The advantage of this system is that the depth of the folder structure only grows as deep as the length of the filename. So if your files are automatically generated in a numeric sequence, the structure is only is deep is it needs to be.

12.jpg -> 12.jpg
123.jpg -> 12\123.jpg
123456.jpg -> 12\34\123456.jpg

This approach does mean that folders contain files and sub-folders, but I think it's a reasonable trade off.

And here's a beautiful PowerShell one-liner to get you going!

$s = '123456'

-join  (( $s -replace '(..)(?!$)', '$1\' -replace '[^\\]*$','' ), $s )
John Rees
  • 1,553
  • 17
  • 24