1

What are the restrictions on the length of a flatfile db? How are the number of entries on a flatfile page controlled?

Mat
  • 202,337
  • 40
  • 393
  • 406
Webiter
  • 119
  • 1
  • 10

2 Answers2

1

You will only be limited by the amount of free-space on the servers hard drive and the file system used on the drive. For example if it's ext3 with a 4KiB block size then you'd be limited to 16TiB. When doing a database in a flat-file, you are the designer of how the database is formatted. Most people do entries by line, some do spaces. It will be up to you and your needs. Keep in mind though that for a larger database, any SQL server (e.g. MySQL, MSSQL, PostgreSQL, etc.) is going to give you much better performance.

Here is an example of using a flat-file as a database: http://www.designdetector.com/archives/04/10/FlatFileDatabaseDemo.php#demo

Drew Chapin
  • 7,779
  • 5
  • 58
  • 84
  • If you are expecting your database to be rather large, then a flat-file is not the way to go. There will be a direct relationship between the size of the file and the time it takes to load the page. – Drew Chapin Nov 12 '11 at 15:49
  • Database I require is only for a small requirement in a local town area. I was just concerned about the issue of size. You provided a great answer and a link to a good reference point. Great work - Thanks. – Webiter Nov 12 '11 at 16:00
  • No problem, I would also like to suggest to you the option of using XML since it will be a small database. XML will make the database more easily readable when looking at the raw file. It will also do a lot of the work for you. For example, say you use a pipe character to separate your columns... Well, if the user enters a pipe character, you have to escape that somehow or it will mess up your database. XML will take care of that for you. Example: [http://www.w3schools.com/php/php_xml_simplexml.asp](http://www.w3schools.com/php/php_xml_simplexml.asp) – Drew Chapin Nov 12 '11 at 16:18
  • I don't know if I would use an XML file as a `Database` per say. An XML file is great for small amounts of data and using things like `XPath` to get at parts of it, but once the data get too large you'll have to scrap it and start over. Consider using `SqlLite` since its a file based database. The advantage of doing that is that when you do want to migrate to a full-fledged database you can migrate pretty easily to `Mysql`, `PostgreSQL` or whatever PDO supported database backend. That's my 2 cp. – Yzmir Ramirez Nov 12 '11 at 17:54
  • Again this brings up the size issue. How much is small amounts of data? Most applications out in small community towns may not need a full blown MySql. Flatfile can serve a purpose for me economically speaking, I think! Might small be sometimes beautiful. – Webiter Nov 12 '11 at 19:21
  • An XML file will not take much more time to parse than a regular flat-file. As for the database size issues. I would not put a database that is going to take up more than a Megabyte in a flat-file. – Drew Chapin Nov 12 '11 at 19:32
  • This might be pushing StackOverflow beyond its limits.....The following line of data from a txt file accounts for 233 bytes of data. Friday, November 11, 2011, 7:47 pm [IST], ffffff ffffffff, ffff@mmmmmmmm.jh, 55555555555, 127.0.0.1, http://localhost/Poll/DRBPoll/booth.php, Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23. How many of such lines would be in a Megabyte and that would reflect the number of visitor submits in a raffle! – Webiter Nov 12 '11 at 21:22
  • A Megabyte = 1048576bytes. Dividing one megabyte by 233 bytes gives a line capacity on the flat file of about 4500 lines of data. – Webiter Nov 13 '11 at 16:23
  • You should also note that will be a rough estimate. It looks like you're storing their user agent string from their web-browser. That string will be different lengths depending on what browser they are using. Also, do you really need to store `127.0.0.1, localhost/Poll/DRBPoll/booth.php` ? Is that HTTP Referrer? or the IP and path of your server and script? – Drew Chapin Nov 13 '11 at 16:30
1

Limits for flat file databases depend on the server's file system. Nowadays, you can use huge flat-file databases that go gigabyte in size. But remember that there are "locking" issues when you read-and-write to such "databases" while others are reading and/or writing to them too. You wouldn't be the first one to lose all data in such a flat-file database due to a "race condition" gone wrong.

SQLite2/SQLite3 and MySQL all handle such problems in their own way and most of the time they handle it more safe than ye average fread/fwrite implementation using locking functions like PHP's "flock" (as it's only "advisory" on some linux systems and might simply not work). That's why people opt-in for such database servers. It spares you the trouble of worrying about locking issues and provides you with a whole set of database functions to learn and use! ;)