0

I am creating a web-based app for android and I came to the point of the account system. Previously I stored all data for a person inside a text file, located users/<name>.txt. Now thinking about doing it in a database (like you probably should), wouldn't that take longer to load since it has to look for the row where the name is equal to the input?

So, my question is, is it faster to read data from a text file, easy to open because it knows its location, or would it be faster to get the information from a database, although it would have to first scan line by line untill it reaches the one with the correct name?

I don't care about the safety, I know the first option is not save at all. It doesn't really matter in this case.

Thanks,

Merijn

  • The answer probably depends on a lot of things. The amount and complexity of the data are two of those things. – Dan Bracuk Jul 29 '14 at 12:08

4 Answers4

1

Abit of googling came up with this question: https://dba.stackexchange.com/questions/23124/whats-better-faster-mysql-or-filesystem

I think the answer suits this one as well.

The file system is useful if you are looking for a particular file, as operating systems maintain a sort of index. However, the contents of a txt file won't be indexed, which is one of the main advantages of a database. Another is understanding the relational model, so that data doesn't need to be repeated over and over. Another is understanding types. If you have a txt file, you'll need to parse numbers, dates, etc.

So - the file system might work for you in some cases, but certainly not all.

Community
  • 1
  • 1
Jimmy Knoot
  • 2,378
  • 20
  • 29
1

In any question about performance, the first answer is usually: Try it out and see.

In your case, you are reading a file line-by-line to find a particular name. If you have only a few names, then the file is probably faster. With more lines, you could be reading for a while.

A database can optimize this using an index. Do note that the index will not have much effect until you have a fair amount of data (tens of thousands of bytes). The reason is that the database reads the records in units called data pages. So, it doesn't read one record at a time, it reads a page's worth of records. If you have hundreds of thousands of names, a database will be faster.

Perhaps the main performance advantage of a database is that after the first time you read the data, it will reside in the page cache. Subsequent access will use the cache and just read it from memory -- automatically, I might add, with no effort on your part.

The real advantage to a database is that it then gives you the flexibility to easily add more data, to log interactions, and to store other types of data the might be relevant to your application. On the narrow question of just searching for a particular name, if you have at most a few dozen, the file is probably fast enough. The database is more useful for a large volume of data and because it gives you additional capabilities.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • I am not reading one file line by line, I have a directory with all names as files storing their information in their file. So even with thousands of people using it, you just have to read that one file. – Merijn Den Houting Jul 29 '14 at 12:15
  • @MerijnDenHouting . . . Depending on how often the file needs to be read, you may find that database servers handle multiple concurrent transactions better than file servers. Once again, though, you need to test in your environment. – Gordon Linoff Jul 29 '14 at 12:41
0

That's where database indexes come in.

You may wish to take a look at How does database indexing work? :)

Community
  • 1
  • 1
AKX
  • 152,115
  • 15
  • 115
  • 172
0

It is quite a simple solution - use database.

Not because its faster or slower, but because it has mechanisms to prevent data loss or corruption.

A failed write to the text file can happen and you will lose a user profile info. With database engine - its much more difficult to lose data like that.

EDIT:

Also, a big question - is this about server side or app side??

Because, for app side, realistically you wont have more than 100 users per smartphone... More likely you will have 1-5 users, who share the phone and thus need their own profiles, and for the majority - you will have a single user.

Anatoliy Kim
  • 768
  • 4
  • 13