开发者

TXT File or Database?

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     | A开发者_开发技巧ge |
+----------+-----+
| Joshua   | 32  |
| Thomas   | 21  |
| James    | 34  |
| Daniel   | 12  |
+----------+-----+

TXT file

Joshua 32
Thomas 21
James 34
Daniel 12


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.


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.


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


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.


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


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.


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.


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).


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:

  • If you PDO, you can easily upgrade to MySQL or any other database. http://php.net/manual/en/book.pdo.php
  • If you want to really go back to text file, then just dump the data http://snippets.dzone.com/posts/show/2128

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

  • http://wiki.nginx.org/Main
  • http://wiki.nginx.org/PHPFcgiExample
  • http://www.google.com.au/search?aq=1&oq=apache+nginx&sourceid=chrome&ie=UTF-8&q=apache+nginx+benchmark


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜