473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Efficiency of file vs database

Ok, I have a program that reads the contents of a file (1 line, 5 '|'
seperated items). Every so often (between twice a day, and 200 times a
day), it rewrites the contents of that file. I also do a few database
update queries whenever the file is written. I only open up the
database connector if I need to update the database. The script itself
runs every 30 seconds. My question, is that is it faster and more
efficient to read the file every time, or open the database connector
and make a query? The file name never changes, and I read the whole
file at one time (679 bites).

Dec 18 '06 #1
10 3272
ircmaxell wrote:
Ok, I have a program that reads the contents of a file (1 line, 5 '|'
seperated items). Every so often (between twice a day, and 200 times a
day), it rewrites the contents of that file. I also do a few database
update queries whenever the file is written. I only open up the
database connector if I need to update the database. The script itself
runs every 30 seconds. My question, is that is it faster and more
efficient to read the file every time, or open the database connector
and make a query? The file name never changes, and I read the whole
file at one time (679 bites).
Hi,

Only one way to find out: timestamp both operations and see how long they
take to complete.
But the results may vary a lot based on machineload and needed diskIO (for
other processes).

In general: If your application is running fine with a small file, leave it
that way.
A database will typically be faster if your file is big, because opening a
big file and loading it into memory will take diskIO time, which is often a
bottleneck on modern machines (fast CPU, relatively slow disk IO).

More general: If you don't have a problem, don't fix it. :-)
You should of course be carefull when writing to a file and take precautions
no other process is doing the same (filelocking).

just my 2 cent.

Regards,
Erwin Moller
Dec 18 '06 #2
Following on from ircmaxell's message. . .
>Ok, I have a program that reads the contents of a file (1 line, 5 '|'
seperated items). Every so often (between twice a day, and 200 times a
day), it rewrites the contents of that file. I also do a few database
update queries whenever the file is written. I only open up the
database connector if I need to update the database. The script itself
runs every 30 seconds. My question, is that is it faster and more
efficient to read the file every time, or open the database connector
and make a query? The file name never changes, and I read the whole
file at one time (679 bites).
Well since you have both methods at your disposal why don't you compare
them? If you can't detect a speed difference then it doesn't matter.

In my experience this sort of choice will probably depend on other
things anyway.

I have a feeling that you're making a mountain out of a molehill of
machine cycles.

--
PETER FOX Not the same since the porcelain business went down the pan
pe******@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Dec 18 '06 #3
Well, the problem I have, is that durring the peak times, the server is
at its limit of resouces (CPU and Ram) and getting around 100 requests
per second (images included). So if I could save a tiny bit, it would
make a difference... Even at its peak, the script runs every 30
seconds, however if I could reduce the memory footprint created by this
program, it would reduce Apache's child process memory footprints...
Just a question... Thanks!

On Dec 18, 6:20 am, Peter Fox
<peter...@eminent.demon.co.uk.not.this.bit.no.html wrote:
Following on from ircmaxell's message. . .
Ok, I have a program that reads the contents of a file (1 line, 5 '|'
seperated items). Every so often (between twice a day, and 200 times a
day), it rewrites the contents of that file. I also do a few database
update queries whenever the file is written. I only open up the
database connector if I need to update the database. The script itself
runs every 30 seconds. My question, is that is it faster and more
efficient to read the file every time, or open the database connector
and make a query? The file name never changes, and I read the whole
file at one time (679 bites).Well since you have both methods at your disposal why don't you compare
them? If you can't detect a speed difference then it doesn't matter.

In my experience this sort of choice will probably depend on other
things anyway.

I have a feeling that you're making a mountain out of a molehill of
machine cycles.

--
PETER FOX Not the same since the porcelain business went down the pan
peter...@eminent.demon.co.uk.not.this.bit.no.html
2 Tees Close, Witham, Essex.
Gravity beer in Essex <http://www.eminent.demon.co.uk>
Dec 18 '06 #4

Erwin Moller wrote:
In general: If your application is running fine with a small file, leave it
that way.
A database will typically be faster if your file is big, because opening a
big file and loading it into memory will take diskIO time, which is often a
bottleneck on modern machines (fast CPU, relatively slow disk IO).
Building upon that... The file may be small enough right now for it
not to matter. But if it starts to get bigger in the future and you've
already gone heavily down this path, you may need to do significant
backtracking to switch over to a database.

Just something to keep in mind.

Dec 18 '06 #5
ircmaxell wrote:
Well, the problem I have, is that durring the peak times, the server is
at its limit of resouces (CPU and Ram) and getting around 100 requests
per second (images included). So if I could save a tiny bit, it would
make a difference... Even at its peak, the script runs every 30
seconds
If the script runs every 30 seconds, but you get 100 requests per second,
that means that only 0.3% of requests are for this particular script.
Which means that you might be better off optimising the other 99.7% of the
requests!

Although Apache is a lovely server, there are some others which might run
faster for you if you don't need some of the extra features that Apache
provides. Take a look at, say, "thttpd" -- your PHP will likely run slower
(as it runs PHP through CGI instead of mod_php) but the speed-up you get
for other files may more than make up for it.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

Dec 18 '06 #6
Well, let me clarify... The site is comprised of a complete php
frontend (Joomla). There are 4 HTML files and an XML file that recieve
90% of the traffic (the other 10% accounts for enough traffic to keep
php running fast). The script that we are talking about rewrites those
5 files (plus a tiny one) on every run. I need to use apache for
mod_rewrite and other functions on other domains hosted.
The site : www.bagsofcrap.com
One HTML file http://www.bagsofcrap.com/woot.html
XML file http://www.bagsofcrap.com/woot.xml
Small txt file http://www.bagsofcrap.com/wootdata.txt

I would post the update script, but that's pointless...

On a side note. Would it be worth it to not write 4 HTML files, but
have them hit a small php file instead that queries a database for the
data? Or is my way most efficient?
Toby Inkster wrote:
ircmaxell wrote:
Well, the problem I have, is that durring the peak times, the server is
at its limit of resouces (CPU and Ram) and getting around 100 requests
per second (images included). So if I could save a tiny bit, it would
make a difference... Even at its peak, the script runs every 30
seconds

If the script runs every 30 seconds, but you get 100 requests per second,
that means that only 0.3% of requests are for this particular script.
Which means that you might be better off optimising the other 99.7% of the
requests!

Although Apache is a lovely server, there are some others which might run
faster for you if you don't need some of the extra features that Apache
provides. Take a look at, say, "thttpd" -- your PHP will likely run slower
(as it runs PHP through CGI instead of mod_php) but the speed-up you get
for other files may more than make up for it.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Dec 19 '06 #7
Usually when in need to compare which is faster, I tend to think that
in the very end databases are just flat files. The only difference
between real flat files and database is the database software, which
has been built to optimize, among other things, the speed of quering of
the data.

This of course might not be the case in your problem, since reading
just a simple file might be faster. However if the database software is
in the same server with the php, the connection should be very fast.

Just babbling, try it out. As someone mentioned, if your file grows
you'll might be at risk in the future. Databases are just so much more
scalable.

jussi

Dec 19 '06 #8
On Tue, 19 Dec 2006 11:59:29 -0800, ju*****@gmail.com wrote:
Usually when in need to compare which is faster, I tend to think that in
the very end databases are just flat files. The only difference between
real flat files and database is the database software, which has been
built to optimize, among other things, the speed of quering of the data.
If you disregard all of the fundamentals of database design...
normalization, index optimization, etc.

A flat file can be very fast as long as you're talking about a very small
file. At the point the overhead of the database server matches the access
time of the flat file based on its size the database server will always be
faster... assuming your database isn't designed like a flat file (which it
shouldn't be).
Dec 19 '06 #9
Well, I wound up redesigning the program anyway, and what a speed
difference!!! I went to an almost completely database system (I still
write a bunch of files, but I don't read any of them). The page load
went from .93 seconds to .24 seconds... The file read portion went
from .36 seconds to (are you ready?) a measly .00023 seconds using the
database... WOW!!! You can say that is a little difference...

On Dec 19 2006, 3:29 pm, Ivan Marsh <anno...@you.nowwrote:
On Tue, 19 Dec 2006 11:59:29 -0800, juss...@gmail.com wrote:
Usually when in need to compare which is faster, I tend to think that in
the very end databases are just flat files. The only difference between
real flat files and database is the database software, which has been
built to optimize, among other things, the speed of quering of the data.If you disregard all of the fundamentals of database design...
normalization, index optimization, etc.

A flat file can be very fast as long as you're talking about a very small
file. At the point the overhead of the database server matches the access
time of the flat file based on its size the database server will always be
faster... assuming your database isn't designed like a flat file (which it
shouldn't be).
Jan 22 '07 #10
On 22 Jan 2007 02:20:59 -0800, in comp.lang.php "ircmaxell"
<ir*******@gmail.com>
<11**********************@11g2000cwr.googlegroups. comwrote:
>| On Dec 19 2006, 3:29 pm, Ivan Marsh <anno...@you.nowwrote:
| On Tue, 19 Dec 2006 11:59:29 -0800, juss...@gmail.com wrote:
| Usually when in need to compare which is faster, I tend to think that in
| the very end databases are just flat files. The only difference between
| real flat files and database is the database software, which has been
| built to optimize, among other things, the speed of quering of the data.
| If you disregard all of the fundamentals of database design...
| normalization, index optimization, etc.
| >
| A flat file can be very fast as long as you're talking about a very small
| file. At the point the overhead of the database server matches the access
| time of the flat file based on its size the database server will always be
| faster... assuming your database isn't designed like a flat file (which it
| shouldn't be).
|
| Well, I wound up redesigning the program anyway, and what a speed
| difference!!! I went to an almost completely database system (I still
| write a bunch of files, but I don't read any of them). The page load
| went from .93 seconds to .24 seconds... The file read portion went
| from .36 seconds to (are you ready?) a measly .00023 seconds using the
| database... WOW!!! You can say that is a little difference...
Just to add to this discussion...

What would be wrong, in this instance of a single variable under 1k,
being placed into a $_GLOBAL variable?

{Please no flames as I think that it is an interesting problem and
maybe a solution to a limited set of file/database problems).
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Jan 22 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Daan | last post by:
Hello all, I have attempted to write my own forum using PHP and MySQL. It works, but the efficiency is far from ideal. It takes a long time to load e.g. the list with threads in a certain forum....
1
by: Gooseman | last post by:
Hi, I'm constructing a page that contains a list of files that can be downloaded - The file details (name, description, date) are stored on a database (mySQL). When displaying the file details...
1
by: Nathan Bloomfield | last post by:
Hi, I am a new database developer and would appreciate some advice on improving the efficiency of my database. The code below is an example of one of the more complex queries in the database...
2
by: Sara | last post by:
Hi - I've been reading the posts for a solution to my query, and realize that I should ask an "approch" question as well. We receive our production data from a third party, so my uers import...
13
by: MLH | last post by:
I have a RDBMS app consisting of 3 primary mdb's... 1) a front-end with a few STATIC tables and the other menagerie of objects 2) a back-end with most of my DYNAMIC tables. I'll call it my main...
2
by: Wysiwyg | last post by:
I was hoping to get some opinions on the efficiency of various methods of reusing the same dropdown list data. Here is the situation: Multiple panels on maintenance pages with TAB menus across...
12
by: PD | last post by:
I am currently making a dating website. I want to have some information on how to structure the database and the php files so that I can achieve speed and efficiency. Can some one please give...
19
by: vamshi | last post by:
Hi all, This is a question about the efficiency of the code. a :- int i; for( i = 0; i < 20; i++ ) printf("%d",i); b:- int i = 10;
2
by: pedalpete | last post by:
Hey mysql gurus, I've got a bit of an efficiency question, and just trying to figure out how best to manage this. For my site HearWhere.com, I have a table which holds artists shows. When i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.