Advice needed on getting large page to load quicker

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Advice needed on getting large page to load quicker

Post by Mirge »

In addition to what others have suggested, I would recommend you make sure you are taking advantage of PostgreSQL INDEXes... see http://www.postgresql.org/docs/8.2/stat ... index.html for more information.
rburgens
Forum Newbie
Posts: 8
Joined: Thu Sep 24, 2009 12:57 pm

Re: Advice needed on getting large page to load quicker

Post by rburgens »

I agree with the earlier posts that you should looking into the SQL "JOIN" functionality. INDEXes will help the database to query faster, if that is the issue.

Also, when you say the page is taking a long time to load, are you saying that it taking PHP a long time to create the content, or that your web browser is taking a long time to render it?? pickle has the right idea for timing how long it takes the PHP interpreter to create the content. But if PHP is running fairly quickly, then it is probably your web browser.

If it is indeed your web-browser, you had the right hunch: use AJAX calls (e.g. innerHTML in JavaScript) to call the various data from the server as it is needed. In this manner you could load just the basics of the form, waiting to load some content until part of the form is completed, then make an AJAX call back to the server to fetch more content and render that....
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Advice needed on getting large page to load quicker

Post by barb woolums »

Thanks pickle, that's exactly the solution I was looking for. I implemented it on my page (though I had to alter it a little to allow for any posted values). It saved me approx 7 secs which is great.

The page is still taking 25secs to load, and I can see from watching the page loading, that most of the time is taken up rendering all the selects. I now think think I have an additional issue with all the javascript on the page making the selects editable.

I will look into this further.

Thanks for everyone's help up until now on this issue - it's much appreciated.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Advice needed on getting large page to load quicker

Post by pickle »

If it takes a long time to render the <selects>, I wonder if there would be any advantage to only rendering it once, then using Javascript to duplicate it in the DOM?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Advice needed on getting large page to load quicker

Post by barb woolums »

Yes I was thinking about only loading those selects that are populated with data, or if none are populated, then just a few and having the option for the user to add more as required using javascript.

I just haven't figured out how yet.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Advice needed on getting large page to load quicker

Post by barb woolums »

...thought about this a bit more, I should have done that before replying. I'll definitely look into ways of doing what you suggest, it's a much better solution than what I was considering - thanks.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Advice needed on getting large page to load quicker

Post by josh »

barb woolums wrote:Yes I was thinking about only loading those selects that are populated with data, or if none are populated, then just a few and having the option for the user to add more as required using javascript.

I just haven't figured out how yet.
Using jquery it would be easy, you'd output your code into a div with a tag like <div class="myMarkup" style="display:none;visibility:hidden;"><!--- markup here---></div>

Then with the jquery library you could do

var theCode = $( '.myMarkup' ).html();
$( '.targetDiv' ).html( theCode );

It would have the effect of "copying" data from the class="myMarkup" div to any div with a class of "targetDiv".

An alternative way to avoid using javascript would be to store it in a PHP variable, "render" it once but store it in a variable so you don't render it 20x
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Advice needed on getting large page to load quicker

Post by barb woolums »

How would I cater for the different select names using the jquery method?

I already have the duplicate option lists in a php variable.
ell0bo
Forum Commoner
Posts: 79
Joined: Wed Aug 13, 2008 4:15 pm

Re: Advice needed on getting large page to load quicker

Post by ell0bo »

If you want to do it javascript side, then you'd want to use a regular expression:

var theCode = $( '.myMarkup' ).html();
var regex = /old_name/gi;
$( '.targetDiv' ).html( theCode.replace(regex, new_name) );

I would do it on the php side however. Just write out the element to a string variable as you have, and then use php to do the replace. To each their own.

I believe you timed the execution of your script by using microtime. What area looked like it was the greatest slow down? I am just looking at the series of sql statement, and I believe that has to be where the majority of your slowdown is, because I'm rather sure you can optimize them, but I wasn't sure if you had data to support this.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Advice needed on getting large page to load quicker

Post by josh »

barb woolums wrote:How would I cater for the different select names using the jquery method?

I already have the duplicate option lists in a php variable.
Try to keep data in a structure you can work with, I really doubt rendering the <select> and <option> tags are your performance problem, its the querying. I would build an array 1 time and then just use that for the rest of the page to render all the selects
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Advice needed on getting large page to load quicker

Post by barb woolums »

Here's the times:

DB Connection duration: 0.00267887115479
Query duration: 0.0166177749634
Output generation duration: 7.30034899712
Total Time: 7.32838988304

I don't know what is going on with microtime on my local server but the times were totally wrong so I was reading them incorrectly. These times are from my remote server and actually correlate with what I am seeing.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Advice needed on getting large page to load quicker

Post by pickle »

Maybe do some more timing in your output generation stage - see how that breaks up.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
barb woolums
Forum Contributor
Posts: 134
Joined: Sun Feb 08, 2009 9:52 pm

Re: Advice needed on getting large page to load quicker

Post by barb woolums »

Yep definitely have to reduce the no. of selects :)
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Advice needed on getting large page to load quicker

Post by JNettles »

Not sure what database you're using but if it supports it then look into Materialized Query Tables (MQT). I had a situation where I needed to put together a training calendar from somewhere around 15 different tables which is a ridiculous amount of overhead when you have that many joins in one SQL statement - multiply that by 25 people every minute, every day, every year and you're spending a lot of processor cycles serving up the same calculations over and over again.

MQTs are excellent in that they are essentially pre-processed SQL statements, and can be set to update on a deferred basis or immediately after a record is modified (I've found its best to defer the updates until late at night, and then run one big update all at once when your server load is the lowest). So instead of having massive SQL statements in your code with a dozen joins you have a simple SELECT * FROM CALENDAR_MQT. Voila.
Post Reply