which will be fasterp? php fwrite faster vs. mysql database?

Discussion in 'PHP' started by spaceman12, Mar 4, 2011.

  1. #1
    Hello,
    I'm developing a browser based chat clients(like the one facebook and gtalk uses) which requires an instant generation of of blocks of html code on clicks.

    So, what i'am basically trying to do is first writes it down the generated code on a text file using php fwrite function and load it immediately into the site as soon as it is done.
    Some disadvantages using this system of techniques is the various calculations involved which i believe will delay in the deleivery of the code generated.
    On the otherhand, this is not so in the case of mysql database as all the values are uniquely stored and determined but the whole lot process of querying and reteieving is another factor that i believe will slow down the performance.

    please tell me which gonna be a faster way...thanks,
     
    spaceman12, Mar 4, 2011 IP
  2. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There are many drawbacks for using File
    1. Insecure, if someone finds out the way you are naming it, they can downloaded / viewed, easily.
    2. Scratches your HardDisk thus, redundancy issues.

    If I were you, I would strongly prefer MySQL with MYSAMI engine or if I grow big, it would be INNODE_DB
     
    eleetgeek, Mar 4, 2011 IP
  3. ACME Squares

    ACME Squares Peon

    Messages:
    98
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Speed isn't really the issue here. Sooner or later you'll get locking and concurrency issues with files, so go with the database.
     
    ACME Squares, Mar 4, 2011 IP
  4. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #4
    thank u both for the response. However, i'd like to make a clarification that the data stored on the text files are almost invisble as it is auto cleared by the server itself on the event of succesful javascript generation. Tht is, it is immediately appended into the browser as soon as the desired blocks of codes are generated. As, this is chiefly a browser related stuff, i necesarily dont find it useful to use database,most importantly on the ground that it might utilize heavy resources and workload as compared against lightweight text.

    Any suggestion welcome
     
    spaceman12, Mar 4, 2011 IP
  5. ACME Squares

    ACME Squares Peon

    Messages:
    98
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    In that case, memcache is definitely the way to go. This will completely avoid disk writes, but will still allow you to assign unique values to the entries.
     
    ACME Squares, Mar 4, 2011 IP
  6. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can you explain point 2? Pretty sure that makes little to no sense ??
     
    Mike Griffiths, Mar 4, 2011 IP
  7. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #7
    hello ACME, ur input was appreciative but its almost imposible for me to adapt to another language at this moment of time as the site has to be up by latest the 1st week of May. And one more important thing that i miss out:

    i used advanced ajax and jquery and in a way it is as such that the whole proces of code generating and outputing is not carried out alone by a single php file but rather it is passed down from 1 php to another as soon as the desired code has been generated thus without necesarily having to execute all the code inside a single php files,..i,e,..those codes are executed behind the server only after echoing out the output to the browser. Also, the writings by php to the text file is usualy a small piece of code. However, it involves creating many numerous new txt files and so my concern is if this proces might cause server stres? Btw, this techniques seems to works super fast in my local host server. Any sugestion welcome.


    EDIT:
    the number of txt files the server has to create for the smooth operation purpose of the script for a single loged-in user amounts to about 15
    .thnx
     
    Last edited: Mar 4, 2011
    spaceman12, Mar 4, 2011 IP
  8. ACME Squares

    ACME Squares Peon

    Messages:
    98
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The answer is still memcache. It's not a language like SQL, but more like a shared array. You can set expiry times so clean-up is automatic.

    // Write data.
    $memcache->add('key1', '<your data here>', true, 300);
    // Read data.
    $my_data = $memcache->get('key1');

    This would be the scalable solution. Robust too, since by storing a serialized object in memcache, you can unserialize to a PHP object with NO parsing of text files. :cool:
     
    ACME Squares, Mar 5, 2011 IP
  9. eleetgeek

    eleetgeek Peon

    Messages:
    129
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ' Can you explain point 2? '
    I would put huge smile and do it!

    ' Pretty sure that makes little to no sense ?? '
    When someone questions my intelligence, I get annoyed .

    Still, I would explain you ... It requires some BASIC level knowledge. Note: BASIC != basic
    "Good" programmers/developers keep WORM - Write One Read Many concept in their mind while designing their software.
    Database is a structured file system for the hard disk and memory / RAM.
    Whatever you fetch from the database is stored in RAM and then, further PUSHed or POPed
    On the other hand, file system would be 'called' each and everytime pointing to the HardDisk for the appropriate addresses.

    If you need further explanation be kind enough by using words like 'please' and I will be glad to do it :) For now, in summary, no brief.
     
    eleetgeek, Mar 5, 2011 IP
  10. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #10
    thank u all for all ur time and suggestions. Almost, 50percent of all the work has been done by now and as soon as it is completed, i wil host online and release it as a beta version. :)
     
    spaceman12, Mar 6, 2011 IP
  11. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    So you're saying that when writing to a database you don't touch the harddrive? Not sure about that.

    Very few databases run solely in RAM, for the obvious reasons.
     
    Mike Griffiths, Mar 6, 2011 IP
  12. dgreenhouse

    dgreenhouse Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Most hosting sites have the SQLite library already compiled into PHP.
    You can use that for your chat queue.
    It should be faster than MySQL for most db operations in the scenario you're envisioning.
     
    dgreenhouse, Mar 6, 2011 IP