EchoFool Posted November 20, 2010 Share Posted November 20, 2010 Hey, Currently i have a chat room which calls my database quite frequently... so im thinking i can lower the heavy load from my server by taking it off MYSQL methods and using PHP read/write/delete methods of a text file. Now i have two issues with this... first one is: <?php $myFile = "chatlist.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); $stringData = $Message.'<br/>'; fwrite($fh, $stringData); fclose($fh); ?> This writes the most recent line, how ever the next line that gets sent overwrites so theres only ever one line in the txt file =/ Secondly how on earth do you delete a specific line from the file efficiently ? Say for example a line which broke the rules some how like spam was posted etc. Hope you get shed light on this. Thanks Quote Link to comment Share on other sites More sharing options...
dontpanic Posted November 20, 2010 Share Posted November 20, 2010 // Change the fopen mode from 'w' to 'a+' $fh = fopen($myFile, 'a+') or die("can't open file"); and for future reference, see the manual. Secondly how on earth do you delete a specific line from the file efficiently ? Say for example a line which broke the rules some how like spam was posted etc. You don't. Thats why God invented databases. In all honesty MySQL will always be faster and safer than a flat file, and can handle tons of requests in a short amount of time. Try changing the way you access the database if it really is slowing you down. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted November 20, 2010 Author Share Posted November 20, 2010 Well the way i get the info is select the rows and use a while loop - can't see any more efficient way but i still get about a 2 second ish lag after submitting a message to the time appears on screen =/ Quote Link to comment Share on other sites More sharing options...
dontpanic Posted November 20, 2010 Share Posted November 20, 2010 Well, unfortunately its impossible to provide a solution without seeing all of the code and database, but I can say that using a flat file will definitely be slower and prone to corruption. Lets try to get some more info and hammer this out... Are you hosting your own server or are you going through a hosting company? Are your HTTP and MySQL servers running on the same machine? If not, what is their latency (ping)? Does your php code that accesses the database get executed more frequently then needed? Is it really necessary to post every new message as it arrives? Why not buffer some sql in a local text file that only gets executed after a set time? (This isn't the same as using text files as a database, appending is very fast) Those are just a few things I could think of randomly, and I'm sure theres plenty more effecting your script's execution time. My suggestion is to really study your code and see where the most work is being done. Litter it with time() calculations to see how long your SQL queries take, and stuff like that. Plenty of chat rooms out there use php and MySQL and can handle huge loads; know this, and know that your code just needs optimization. One last thing is the structure of your database, which can greatly effect the speed of your queries. I'm terrible at database design so don't ask me, but if you find that it may be the problem, hop on over to the MySQL forums and ask around. Quote Link to comment Share on other sites More sharing options...
EchoFool Posted November 20, 2010 Author Share Posted November 20, 2010 In order of bullet points: 1) Yes linux server 2) Yes same machine i only own one server 3) Umm currently i have "Post message" + "load chat logs" the latter only happens when ajax receives TRUE (aka a change has occured) 4)If i do not post message as it arrives it would not appear like a live chat (think like IRC) Quote Link to comment Share on other sites More sharing options...
dontpanic Posted November 20, 2010 Share Posted November 20, 2010 I'm leaving work right now so just a quick response: it seems like you're doing things right from a distance but, again, the answer is in your code. I just recently finished coding up something similar in function to a chat room using asynchronous techniques (I refuse to use the marketing term, ajax) and my script is making hundreds of queries every iteration without any noticeable latency. I used one table for raw data and one table for metadata, and initiated my script when the metadata was in the right state. Anyway, hopefully someone else can provide more insight than I or you find the reason soon! Now its time to leave work and become inebriated. TGIF. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.