views:

263

answers:

11

Hey folks!

What should I use in this case (Apache + PHP)? Database or just a TXT file? My priority #1 is speed.

Operations

  • Adding new items
  • Reading items
  • Max. 1 000 records

Thank you.

Database (MySQL)

+----------+-----+
| Name     | Age |
+----------+-----+
| Joshua   | 32  |
| Thomas   | 21  |
| James    | 34  |
| Daniel   | 12  |
+----------+-----+

TXT file

Joshua 32
Thomas 21
James 34
Daniel 12
+7  A: 

For speed and optimized memory usage, I'd say go with a database hands down. Putting an index on the name column alone will probably boost performance in a way never achievable with a text file.

A database has also other advantages like some sanitation (no breaking of delimiters, newlines etc.) and less danger of access conflicts when multiple instances try to read from the table - and different from a file-based approach, writing conflicts are constrained to the record in question only.

Pekka
+1  A: 

Using a database will be more efficient. You'll be able to query and do operations to select data much more easily than if it is in a text file. The text file would have to be constantly opened, closed and the contents exploded to achieve to iterate data or find rows - this will be slow and a pain to code.

There are classes and ready made scripts out there which would allow you to store such data in text files but at the end of the day this is what databases were designed for so I'd use one.

JoeR
+2  A: 

Have you explored other DHTs?

Project Voldemort

Memcached + MySQL

Update 1

If you don't have memcached and Voldemort on your servers, then you can go for embedded key-value databases like BDB

zengr
Very good tips, but both require installation of additional software / modules.
Pekka
A: 

With a maximum of 1000 records containing only name and age columns, I doubt there is any significant difference in speed between your options; likely other factors like server load will dominate your speed.

For easy, reliable access, MySQL might be a bit simpler, but with a text file you don't have to set up a MySQL server.

I get the feeling we don't have enough information to give you an educated answer.

kerkeslager
A: 

As some have said above, there are alternatives to this problem. The power of the database could be useful (after all, you don't want to code those features yourself when you could be doing something else) but you don't want to go overkill.

SQLite may be worth looking at as it can be treated as a file (no server to worry about) but gives you the power of SQL querying.

You could also try something like Memcached as a cache on top of whatever data storage layer you choose.

Geoff Adams
A: 

If speed is really the only thing that is important, I might suggest using var_export to make a .php file that can be loaded with PHP. As long as the number of reads-to-writes is high enough and if APC is installed, the PHP file will be able to exploit the opcode cache of APC. This will be way faster than ANYTHING else because the data will be loaded natively in as PHP instead of having to go through an intermediate layer (memcache, mysql, etc).

Kendall Hopkins
A: 

SQLite. Small fast database. http://www.sqlite.org/

Integrated with PHP installs these days. http://php.net/manual/en/book.sqlite.php

Gives you options:

And if you really want speed consider nginx with fast-cgi php.

JavaRocky
A: 

I would definitely recommend a database over plain text files any day. For one, databases do a lot of optimization on your access to the data, so you won't have to worry as much about performance. Secondly, you have so many more capabilities. You can use the SQL language to do things like:

  • Select all customers whose age is greater than or equal to 18
  • Get the number of products you've sold
  • Get the total revenue from your products sold
  • Get the average price of all your products
  • Get a list of the customers who live in a certain area

...and much, much more, with simple SQL statements. Trying to code all that for a text file would be counter-productive and a lot less flexible. With a database you get all that for free.

Jake Petroules
+2  A: 

The proposed text file is in essence a database. Writing the operations yourself to manipulate it is just reinventing the wheel, so just use something that already exists. 1,000 records is rather small scale so using something simple like SQLite would be fine.

Andrew E. Falcon
A: 

It's very risky but you could dynamically generate a PHP file. If you will be mostly reading from this file then it will be the fastest method for small records with a limit of 1000.

Earlz
A: 

Just keep it in memory if there are only 1000 records.

Jacob