473,386 Members | 1,819 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,386 software developers and data experts.

Forum efficiency

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.

I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.

My question: is there a more efficient way to initialise my forum or is
it better to begin from scratch?

Here's the code:

function Forum($id) {
// initialize the threads-attribute
$this->threads = array();

// get the information for this forum from the database
$con = dbConnect();
if ($con) {
$sql = "SELECT * FROM forums WHERE forum_id = '$id'";
$query = mysql_query($sql);
$res = mysql_fetch_object($query);
$this->forum_id = $res->forum_id;
$this->name = $res->name;
$this->desc = $res->description;

// get the contents of the threads-attribute from the database
$sql = "SELECT * FROM threads WHERE forum_id = '$id'";
$query = mysql_query($sql);
while ($res = mysql_fetch_object($query)) {
$thread_id = $res->thread_id;
$subject = $res->subject;
// get the messages for this thread
$messages = array();
$sql2 = "SELECT * FROM messages WHERE thread_id =
'$res->thread_id'";
$query2 = mysql_query($sql2);
while ($res2 = mysql_fetch_object($query2)) {
$msg_id = $res2->msg_id;
$author_id = $res2->author_id;
$thread_id = $res2->thread_id;
$post_date = $res2->post_date;
$contents = $res2->contents;
$msg = new Message($msg_id, $author_id, $thread_id, $post_date,
$contents);
$messages[] = $msg;
}
$thread = new Thread($thread_id, $subject, $messages);
$this->threads[] = $thread;
}
}
}

--
Daan Stolp
Jul 17 '05 #1
6 1911
Daan wrote:
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.


I haven't really looked at your code, but this is what I would, and
already do, use:

Each topic has a header record:
id - int
subject - varchar(x)
...

Each message has a record:
topicid - int |
id - int | unique(topicid + id)
parent - int - relates to message.id or null/0 for first post
ts - timestamp
body - text
...
...

Then your display query is:
select * from messages
where topicid=$topicid
order by parent,ts

But you will have to load all the records and output them recursively to
get the thread sorted properly.
Jul 17 '05 #2
On Tue, 17 Feb 2004 13:28:27 +0100, "Daan"
<da*******@NOSPAM.newmail.net> wrote:
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.

I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.

My question: is there a more efficient way to initialise my forum or is
it better to begin from scratch?


If you look at what most other Forum scripts do, they have two tables
- one for topics, and one for posts.

It has a number of advantages - like staging your statistics (views
etc) in the topics table rather than working them out.

Welcome to anti-textbook coding - writing fast code generally means
finding any trick you can regardless of it's difficult to then read
the code...
Jon Beckett
Jul 17 '05 #3
Daan:
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.

I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.


I'd recommend to use the MySQL explain command to see if any of your queries
are performing less than optimal: http://www.mysql.com/doc/en/EXPLAIN.html

If your queries are performing at their max, it's time to start looking at
smarter ways to store and retrieve the data

André Nĉss
Jul 17 '05 #4
Hello,

On 02/17/2004 09:28 AM, Daan wrote:
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.

I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.

My question: is there a more efficient way to initialise my forum or is
it better to begin from scratch?


Sure. Note down this thought:

- The way to write fast database applications is to avoid accessing
database as much as possible.

What I mean is that usually database accesses are the slowest factor in
database driven sites. So, you need to only access databases for the
essential.

In most content sites, including forums, you have much more database
accesses for retrieving data than to change that data.

So, what you can do to minimize the database reading accesses is to
cache the pages that you generate from reading the database data in
local disk files.

Then, whenever the data is changed, just removed the cached page files
to force that it is regenerated next time it is needed for display.

In this case, you may want to try this class that is meant to cache
generic data. You can use it to cache whole pages or just excerpts of
each page.

In your case, whenever somebody accesses a page that display a forum
thread, just use the cache class to verify whether the cache class
exists and is still valid.

If it does not exist, just regenerate it and pass its HTML (or just the
forum thread listing excerpt) to the class so it can store it safely
even when you have a busy site with many simultaneous accesses.

If the cache file exists and is upto date, just ask the class to
retrieve the HTML data from the cache files and display it.

Whenever a user updates the thread, just call the function of the class
that voids the cache data file, to force the thread listing page caches
to be regenerated from database next time these pages are accessed.

The speedup can be huge. So you have an idea, this technique is so
efficient that the site where this class is hosted is now holding more
than 6000 cached excerpts of pages.

http://www.phpclasses.org/filecache

--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Jul 17 '05 #5
On Tue, 17 Feb 2004, Daan wrote:
I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.


You can get away without the nested DB queries, if you are prepared to cap
the maximum number of branches and total depth of the tree for a thread.
Have an indexed varchar column (sort_key, say) that's pretty wide, and glue
pieces together so that it sorts properly. Basically you take the parent's
sort_key and tack on, say, 'aaa' for the first child, 'aab' for the second
and so forth. For a varchar(900) with each child consuming 3 characters you
could get 300 deep and 62^3 (for mixed-case alphanumeric) wide at each
level. You'll want to twiddle those numbers to fit your real situation.

Then you just ask have one query to get all the postings back in the right
order:

SELECT things FROM postings WHERE ... ORDER BY sort_key;

This was a nice hack in the ArsDigita Community System back in the day.
You'll turn some stuff up if you search the Web for 'sort_key bboard' with
sql, data-model, design and such like.

--
ash
a-k
.... "Hey, imp!" shouted Vlad, the imp hailer.

Jul 17 '05 #6
Hi Daan!

On Tue, 17 Feb 2004 13:28:27 +0100, "Daan"
<da*******@NOSPAM.newmail.net> wrote:
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.

I think the problem has to do with the fact that I have several SQL
calls in nested loops, so if a large thread list has to be loaded, there
are a lot of SQL queries to be executed. I have posted the code that
initialises the forum below, so you can see what I mean.

My question: is there a more efficient way to initialise my forum or is
it better to begin from scratch?

Maybe have a look at nested sets.

HTH, Jochen

Jul 17 '05 #7

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

Similar topics

2
by: ^MisterJingo^ | last post by:
Hi all, I'm just curious to how forums store the body of messages. Is it just as text in a DB? This seems to me as if the DB would take on gigantic proportions if a lot of people posted...
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...
31
by: mark | last post by:
Hello- i am trying to make the function addbitwise more efficient. the code below takes an array of binary numbers (of size 5) and performs bitwise addition. it looks ugly and it is not elegant...
92
by: Dave Rudolf | last post by:
Hi all, Normally, I would trust that the ANSI libraries are written to be as efficient as possible, but I have an application in which the majority of the run time is calling the acos(...)...
1
by: Tomás | last post by:
dynamic_cast can be used to obtain a pointer or to obtain a reference. If the pointer form fails, then you're left with a null pointer. If the reference form fails, then an exception is thrown....
335
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
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;
9
by: OldBirdman | last post by:
Efficiency I've never stumbled on any discussion of efficiency of various methods of coding, although I have found posts on various forums where individuals were concerned with efficiency. I'm...
5
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.