Is fwrite faster than using Mysqli? – PHP

  php

Q(Question):

Hi,

Which is faster writing to a file using fwrite or is using a
database to store data? I want to create a log file and it does not
matter if it is in a text file or on a database. I just want the one
that is easier on the server. Any advice is greatly appreciated.


Your friend,
Scott
http://sgaming.org

Sent to you from a Linux computer using Kubuntu Version 8.10
—-== Posted via Pronews.Com – Unlimited-Unrestricted-Secure Usenet News==—-
http://www.pronews.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
—= – Total Privacy via Encryption =—

A(Answer):

Scott escribió:

Which is faster writing to a file using fwrite or is using a
database to store data? I want to create a log file and it does not
matter if it is in a text file or on a database. I just want the one
that is easier on the server. Any advice is greatly appreciated.

If you’re worried about performance recommend you do your own
benchmarks. You can use microtime(TRUE) to measure times with
microseconds, it’s rather straightforward.

In any case, intuition says that appending a line at the end of a file
should be faster that going through the overload of a database
management system which, in the end, will write it into a file.
Databases are chosen when you have other considerations like storing
large amount of data, querying existing data o handling concurrent writings.

http://alvaro.es – Álvaro G. Vicario – Burgos, Spain
— Mi sitio sobre programación web: http://bits.demogracia.com
— Mi web de humor al baño María: http://www.demogracia.com

A(Answer):

And where you think are writing data, when you use database?
hint: database is storage in file.

PS. sorry for my language 😛

Scott pisze:

Hi,

Which is faster writing to a file using fwrite or is using a
database to store data? I want to create a log file and it does not
matter if it is in a text file or on a database. I just want the one
that is easier on the server. Any advice is greatly appreciated.

A(Answer):

On Nov 6, 11:39 pm, Scott <scle…@sgaming.orgwrote:

Hi,

Which is faster writing to a file using fwrite or is using a
database to store data? I want to create a log file and it does not
matter if it is in a text file or on a database. I just want the one
that is easier on the server. Any advice is greatly appreciated.


Your friend,
Scotthttp://sgaming.org

Sent to you from a Linux computer using Kubuntu Version 8.10

—-== Posted via Pronews.Com – Unlimited-Unrestricted-Secure Usenet News==—-http://www.pronews.comThe #1 Newsgroup Service in the World! >100,000 Newsgroups
—= – Total Privacy via Encryption =—

If you want to implement a log table in MySQL (and you are interested
in performance) you should use the <Archive storage engine>. It has
some limitation and it could also be not installed with your server
configuration, check the (MySQL) manual to have some information
more.
Otherwise you may want to consider SQlite.

Try them out, benchmark them and choose the right one for your use.

A(Answer):

Scott wrote:

Hi,

Which is faster writing to a file using fwrite or is using a
database to store data? I want to create a log file and it does not
matter if it is in a text file or on a database. I just want the one
that is easier on the server. Any advice is greatly appreciated.

Neither is overly onerous when you’re just adding data to the end. A
text file would be faster – but mysql isn’t that hard. But there are
other reasons for using a database – like multitasking.

Me thinketh though art prematurely optimizething.


==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

A(Answer):

On 6 Nov, 22:39, Scott <scle…@sgaming.orgwrote:

Hi,

Which is faster writing to a file using fwrite or is using a
database to store data? I want to create a log file and it does not
matter if it is in a text file or on a database. I just want the one
that is easier on the server. Any advice is greatly appreciated.


Your friend,
Scotthttp://sgaming.org

Sent to you from a Linux computer using Kubuntu Version 8.10

—-== Posted via Pronews.Com – Unlimited-Unrestricted-Secure Usenet News==—-http://www.pronews.comThe #1 Newsgroup Service in the World! >100,000 Newsgroups
—= – Total Privacy via Encryption =—

1) why don’t you test it for yourself and tell us
2) It really it doesn’t matter. You’re going to have lots of problems
using files this way if there is any chance of contention.

Really you should be comparing using syslog() and mysqli

C.

A(Answer):

On Nov 6, 10:39*pm, Scott <scle…@sgaming.orgwrote:

Hi,

* * * Which is faster writing to a file using fwrite or is using a
database to store data? *I want to create a log file and it does not
matter if it is in a text file or on a database. *I just want the one
that is easier on the server. *Any advice is greatly appreciated.


Your friend,
Scotthttp://sgaming.org

Sent to you from a Linux computer using Kubuntu Version 8.10

—-== Posted via Pronews.Com – Unlimited-Unrestricted-Secure Usenet News==—-http://www.pronews.comThe #1 Newsgroup Service in the World! >100,000 Newsgroups
—= – Total Privacy via Encryption =—

If you already have a connection open to a MySQL database then logging
data to a log table probably has minimal overhead compared to opening
a separate file, obtaining a lock, writing your log line, releasing
the lock and closing the file. If you were connecting to the database
with the sole intention of logging something then the file would be
the better choice. However, the overhead of either approach shouldn’t
be enough to be noticeable to an end user.

So my choice would be: Do you need to connect to a database anyway?

YES: Use a DB table
NO: Use a file

LEAVE A COMMENT