title: Data retrieval - Database VS Programming language
body: <p>I have been working with databases recently and before that I was developing standalone components that do not use databases.
  With all the DB work I have a few questions that sprang up.
  Why is a database query faster than a programming language data retrieval from a file.</p>
  
  <p>To elaborate my question further -</p>
  
  <p>Assume I have a table called Employee, with fields Name, ID, DOB, Email and Sex. For reasons of simplicity we will also assume they are all strings of fixed length and they do not have any indexes or primary keys or any other constraints.</p>
  
  <p>Imagine we have 1 million rows of data in the table. At the end of the day this table is going to be stored somewhere on the disk. When I write a query Select Name,ID from Employee where DOB="12/12/1985", the DBMS picks up the data from the file, processes it, filters it and gives me a result which is a subset of the 1 million rows of data.</p>
  
  <p>Now, assume I store the same 1 million rows in a flat file, each field similarly being fixed length string for simplicity. The data is available on a file in the disk.
  When I write a program in C++ or C or C# or Java and do the same task of finding the Name and ID where DOB="12/12/1985", I will read the file record by record and check for each row of data if the DOB="12/12/1985", if it matches then I store present the row to the user.</p>
  
  <p>This way of doing it by a program is too slow when compared to the speed at which a SQL query returns the results.</p>
  
  <p>I assume the DBMS is also written in some programming language and there is also an additional overhead of parsing the query and what not.</p>
  
  <p>So what happens in a DBMS that makes it faster to retrieve data than through a programming language?</p>
  
  <p>If this question is inappropriate on this forum, please delete but do provide me some pointers where I may find an answer.</p>
  
  <p>I use SQL Server if that is of any help.</p>