Forum Moderators: coopster

Message Too Old, No Replies

How to split up page load?

         

brandon0401

6:08 am on Nov 29, 2007 (gmt 0)

10+ Year Member



I was wondering how I can send the header/menu before the body part of page, which selects stories from a db containing 1.8 million copies and lays out about 200. Right now it takes about 4 seconds and then the whole page completely loads, which is all text. I want to make the load more flowing, I have tried output buffering but had problems with getting header errors.

Any help would be greatly appreciated, thanks again.

PHP_Chimp

10:22 am on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What was your problem with buffering?
As this should allow you to send the headers at any point.

If the page is taking ages to load, could you not lower the amount of stories? Is your database slowing down the processing of the page i.e. is it taking 3 seconds to get the info from the database then 1 second for the page to load?

Are you selecting everything in the database then using php to select random stories, as this will take a long time to get 1.8 million in to php, for php to then echo 200, use SQL to limit the stories to 200.

brandon0401

6:38 am on Nov 30, 2007 (gmt 0)

10+ Year Member



It is about 16 boxes, 20 stories each..

So a while loop through 16 records, then list 20 stories per record.

For the 20 records using query
$res1=mysql_query("select * from stories where id=" . $id . " order by id desc limit 0, 20)

so there is a limit on what its bringing back.

So I guess my question is, how can I speed this up? Let me know if you would like anymore info, thanks..

henry0

3:20 pm on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should probably look at indexing, optimization etc..
Check that thread [webmasterworld.com]
And do a search here, there are many info about it

whoisgregg

5:31 pm on Nov 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How many total mysql_query calls do you have in the page? 16? or 16*20?

If you have 16*20, that's going to contribute to slow page loads because every query has an overhead regardless of complexity.

brandon0401

10:43 pm on Dec 4, 2007 (gmt 0)

10+ Year Member



We have 2 mysql queries, but two whiles

res1=MYSQLQUERY(select * from postion where user=$username)
while(res1!EOF) = 16 records from table of 10k records
res2=MYSQLQUERY(select * from link where id=$id order by id desc limit 0, 25)
while(res2!EOF) = 25 records - total table is 1.8million records
print results....

This is the basic concept with some php spitting out between...

Thanks for your help, we have indexed applied I believe as many places as we can, thanks.

henry0

11:39 pm on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Brandon,
I do not know what the exact object of your query is
But defining what is queried instead of " * " and going full circle may also do you some good

PHP_Chimp

11:41 pm on Dec 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What were your issues with output buffering?
As if you have looked at the optimization links then buffering should speed things up.

Also do you need to SELECT * or could you narrow down the number of columns returned? This would not speed it up much, but will help a bit if you dont actually need all of the columns.

brandon0401

1:02 am on Dec 5, 2007 (gmt 0)

10+ Year Member



I am going to try to stay away from output buffering cause I have seen problems with header errors, not sure on these.

No I dont need every single column from those.....for both my queries should I only put select id, info from instead of select *?

Does that help in speed usually? Thought it was quicker to do *? Thanks!

PHP_Chimp

9:39 am on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will be a little quicker to get the columns that you are after, as there will be less information returned. So the only information that php is processing is the information that you actually need.
This wont help a huge amount, but it all makes a little bit of difference.

Dont know what problems you get with output buffering that involves headers...that is half of the reason that people use output buffering. To get around trying to send headers half way though there script.

joelgreen

11:13 am on Dec 5, 2007 (gmt 0)

10+ Year Member



I think output buffering would not work because browsers are working differently. For example Firefox displays html as soon as it receives it, while IE tries to load whole page than display it. I may be wrong here.

Try putting header/menus in separate containers (like div). This could possibly help.

Example 1. This seems to force whole page on IE as everything is in one container (table).

<body>
<table>
<tr><td>My header and menus here</td></tr>
<tr><td>My content here</td></tr>
</table>
</body>

Example 2. In this example header is in its own container (div). Browser can show header container first, than load content container.

<body>
<div>My header and menus here</div>
<div>My content here</div>
</body>

henry0

12:11 pm on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OB, works fine on IE, FF and Safari
one should make sure that OB is turned-on in the php.ini

PHP_Chimp

12:43 pm on Dec 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Joelgreen -
when you use ob buffering nothing other than headers is sent to the browser until you tell it to be sent by calling ob_*_flush, or end the script. So you decide when information is sent to the browser, not the browser deciding what information it wants.
I havent tested ob buffered pages with every browser, but FF, IE ans safari all handle them fine. I cant see what the type of browser has to do with if ob buffering will work or not. You decide what is passed to the browser, so if the browser gets html then it will display it.

There are all sorts of issues with trying to get a correct content length header. As I had pages taking about 20 seconds to load, when there was about 2k (buffered) of information being sent. However I was sending a content length header of about 8k, so the browsers were just timing out waiting for the remaining 6k. Problem sorted when I put the code in the correct order, so the content length was actually the compressed length not the uncompressed length...I should have read through the user comments in the manual as it is all in there, my that was my mistake not a problem with ob buffering, php or the browsers.

brandon0401

5:21 am on Dec 6, 2007 (gmt 0)

10+ Year Member



what is faster for counts?

select count(*) as tt from link_feeds where feed_url=blah

or

select count(id) as tt from link_feeds where feed_url=

PHP_Chimp

8:38 am on Dec 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Although I haven't tested it I would have to assume the count(id) would be faster. As any time you call * the database looks for everything. So if you tell it exactly what you are looking for i.e. count(id) then it will know where to count.

If you want to test the time it takes to run scripts then have a look at the comments under the microtime [uk3.php.net] section in the manual. As there are a load of timer examples, then you can test with your set-up on your server to find out what is faster for you.

brandon0401

9:16 pm on Dec 6, 2007 (gmt 0)

10+ Year Member



Thanks this thread has been a ton of help, I have been able to reduce my main pages load a ton by adding those instead of * combined with rest of hints in here..

One problem I am having is a most popular page takes forever to load! I have tried it all not sure what else to do..

Just selects by date and hits and displays, 3 times, day, week, month time periods

here are 3$time=mktime(date("H")-24,date("i"),date("s"),date("m"),date("d"),date("y"));
$res1=mysql_query("select id,title, pub_date, detail from table where pub_date>" . $time . " and (id) in (select id from category where postedby='admin' and linktype='text') order by hits desc limit 0,5");
echo mysql_error();
while($row1=mysql_fetch_array($res1))
{
spit out stuff
}
$time=mktime(date("H"),date("i"),date("s"),date("m"),date("d")-7,date("y"));
$res1=mysql_query("select id,title, pub_date, detail from table where pub_date>" . $time . " and (id) in (select id from category where postedby='admin' and linktype='text') order by hits desc limit 0,5");
echo mysql_error();
while($row1=mysql_fetch_array($res1))
{
spit out stuff
}
$time=mktime(date("H"),date("i"),date("s"),date("m")-1,date("d"),date("y"));
$res1=mysql_query("select id,title, pub_date, detail from table where pub_date>" . $time . " and (id) in (select id from category where postedby='admin' and linktype='text') order by hits desc limit 0,5");
echo mysql_error();
while($row1=mysql_fetch_array($res1))
{
spit out stuff
}

These 3 process 1st in 1 second, 2nd in 6 seconds, and 3rd in a wopping 21 seconds.

I cant see anywhere else to do and index are placed on all..

Thanks in advance

brandon0401

2:50 am on Dec 10, 2007 (gmt 0)

10+ Year Member



bump thanks

PHP_Chimp

12:10 pm on Dec 10, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$time=mktime(date("H"),date("i"),date("s"),date("m")-1,date("d"),date("y"));

You could use -

$time = [url=http://uk3.php.net/manual/en/function.strtotime.php]strtotime[/url]('-1 month');

...not that this will help with the page loading, but its a lot easier to read :)

Although I dont know if this will help a huge amount but it may be worth calling the mysql_free_result [uk3.php.net] after each while loop. As you are not using the results again and they will be held in memory. So freeing the results you dont need may speed things up for you.