Reading Writing File or Query Update Database - Which one is faster in .Net

Posted on 2011-04-26
Last Modified: 2012-05-11
At the moment we have log module which is written in .Net that logs messages errors in application to seperate file every day.

Some times frequency of writing messages into log is more than 200 messages per minute.

If i change the log module so that it writes into sql database  rather than file will this be faster than writing into file ?

Advise ?

Question by:NetSri
    LVL 39

    Assisted Solution

    by:Pratima Pharande
    yes, wrting to database is faster
    and also easy to handle and read
    LVL 8

    Accepted Solution

    Databases typically use files to store the data persisitently, however they also implement advanced algorithms to implement caching and data access etc. This makes database access much faster than standard file access.
    LVL 24

    Assisted Solution

    by:Jeff Certain
    In general, it should be faster to write to the database.

    The hardware is usually faster on a server. Although you're still writing to a disk, it's generally a 10K or 15K rpm drive, rather than the 7200rpm drives common on desktops. In addition, RAID controllers can make this even faster. However, if you're running a solid state drive on your desktop, that will change the balance.

    Network latency is a consideration. Ensure that you're not writing the log to the database synchronously, or you'll end up waiting for the communications to/from the server. On networks I've tested, this runs roughly 1/8th second to open the database connection. This is less of an issue if you're writing to a local database, obviously.

    Have a fallback plan, in case the database isn't available. Again, if it's a local database, not so much of an issue... although the local service that runs the database may be turned off for whatever reason.

    Be aware that databases write not only the data file, but also (depending on configuration) a log file of their own. These log files can get big quickly. If you happen to come close to filling your hard drive (which can happen as the result of the database server writing out temporary files to process queries dealing with large amounts of data) you'll end up crippling the machine, and nothing will be fast.

    You may want to consider the Windows event log as a spot to log the events to, especially if it's a local logging approach.

    And, finally, databases may have a slight edge because they're aware of data types. Numbers and dates, for example, are stored appropriately. So, a number like 2 000 000 000 takes 4 bytes (32 bits) to store in a database, but 10 or 20 bytes (depending on encoding) to store in a text file.


    Author Closing Comment

    Thanks All

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now