Reading file over network slow due to extra reads

瘦欲@ 提交于 2019-12-04 05:47:32

You might be running into op lock issues over smb. Typically when reading/saving a file over the network windows will pull over the full file to the client work on it and send back changes. When you are working with flat file databases or files it can cause unnecessary reads across an smb file share.

I'm not sure if there is a way to just pull over the whole file, read the rows from that file on the local copy and then push back the changes or not.

You'll read some nightmares about oplocks and flat file databases.

http://msdn.microsoft.com/en-us/library/aa365433%28VS.85%29.aspx

Not sure if this solves your problem, but it might get you pointed in the right direction. Good luck!

I found the answer to this. Windows does file reads through the page cache so when I read 17 bytes, it first has to transfer a full page of 32K over and then can copy the 17 bytes I want out of the page cache. Nasty result on performance!

The same thing is actually happening the first time the reads are done on a local file since in that case it does still load a full page at a time into the page cache. But the second time I run the test locally, the files are all already in the page cache so I don't see it. And if SuperFetch is turned on and I've been doing these tests for a while, Windows will start loading the file into the cache before I even run my test application so again I don't see the page reads being done.

So the operating system is doing a lot of things behind the scenes that makes it tough to get good performance testing done!

I see this all the time, and it's out of your control: the network does what it wants.

If you know the file is going to be less than 1MB, just pull the whole thing into memory.

My guess is that the OS is doing it's own read-ahead of the file on the off chance you need the data at a later point. If it's not hurting you then it shouldn't matter.

Check out caching behavoir section of the CreateFile API.

You may like to try the 'FILE_FLAG_NO_BUFFERING' to see if it stops the extra reads. Be warned tho, using this flag may slow your application down. Normally you use this flag if you understand how to stream data off the disk as fast as you can and the OS caching is only getting in the way.

Also you may be able to get the same sort of behavior as the network file with local files if you use the 'FILE_FLAG_SEQUENTIAL_SCAN' flag. This flag hint's to the windows cache manager what you will be doing and will try to get the data for you ahead of time.

I think SMB always transfers a block, rather than a small set of bytes.

Some information on block size negotiation can be found here. http://support.microsoft.com/kb/q223140

So you are seeing a read to copy the relevant block, followed by the local read(s) of 17 bytes within the block. (If you look at the pattern, there are some pairs of 17 byte reads where two reads fall within the same block).

The fix obviously depends upon the control you have over the application and the size and structure of the database. (e.g. if the database had one column per file, then all the reads would be sequential. If you used a database server, you wouldn't be using SMB, etc.)

If it's any consolation, iTunes performs abysmally when using a network drive too.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!