Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Pause before loading

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
rivka
Smarty Rookie


Joined: 07 Oct 2008
Posts: 8

PostPosted: Wed Apr 14, 2010 8:59 pm    Post subject: Pause before loading Reply with quote

In comparing how our site loads with and without smarty, we're noticing a difference. We're talking about _very_ long pages of data with many SQL commands.

Without Smarty: starts loading the page almost immediately (i.e. you actually see text on the page and can begin reading), but then takes a very long time to load the entire thing.

With Smarty: there's a significant pause before loading (of between 2 to 4 seconds, depending on the amount of data), and then the page itself loads incredibly fast.

What causes the difference in how they load? While it's nice that the page itself gets loaded very quickly with Smarty, the pause is not acceptable to the client -- is there a way around that?
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7367
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 15, 2010 2:05 pm    Post subject: Reply with quote

It is because all of your SQL queries are happening before the page actually begins to render, thus the pause during SQL fetching.

My suggestion would be to:

1) Turn on SQL caching on the MySQL side (if you don't already)
2) Turn on caching in Smarty so SQL is not run every page load. (see caching in the docs)
3) Check your SQL queries with EXPLAIN, be certain that everything is indexed properly for fastest possible execution. If any one query takes more than a fraction of a second, I'd try to improve it.
Back to top
View user's profile Send private message Visit poster's website
bimal
Smarty Elite


Joined: 19 Apr 2007
Posts: 423

PostPosted: Thu Apr 15, 2010 8:06 pm    Post subject: big data? Reply with quote

Also, if you have assigned a very big data to smarty, then, think of:
assign_by_ref or assignByRef, instead of simple $smarty->assign().

You should index your database too, for speed, if your application does too many writes on the database. Find a way to shorten your database selection time.

Do not load the extra data that won't be shown in the page, if any.
Back to top
View user's profile Send private message Visit poster's website
rivka
Smarty Rookie


Joined: 07 Oct 2008
Posts: 8

PostPosted: Thu Apr 15, 2010 8:47 pm    Post subject: Reply with quote

- all tables are indexed. I've checked for slow queries using the slow query log, and there are none. There just a lot of queries, because this is a very long page with a lot of data (and no, it is NOT possible to break it up into pages)

- I tried assign_by_ref and it made no noticeable difference

- mysql caching is already on

- not sure what you mean by "extra data" -- what needs to be shown is being shown Smile

- I can not turn on smarty caching, because the data that is being shown is being controlled by variables sent by a form post. The form in question has multiple filters that control what data is or is not selected by the sql queries. The page that is being posted is called "iframe.php" and it's the only page that is ever called -- if I cache that page, it will show the same data regardless of the filters that are set in the form -- not acceptable. If there is some way around this (besides passing the numerous filters in through the URL so as to get a different URL based on the filters), I'm all ears.

I'm confused by your statement "your SQL queries are happening before the page actually begins to render" -- that would suggest that if I put some output before any of the SQL queries, that it would display that before doing the queries -- it does not do this, though.
Back to top
View user's profile Send private message
douglassdavis
Smarty Junkie


Joined: 21 Jan 2008
Posts: 541

PostPosted: Thu Apr 15, 2010 9:19 pm    Post subject: Reply with quote

everything, including the queries is done before it displays the page.


You are using smarty 2 I presume. Hey mohrt, does smarty2 include a way to fetch from and iterate over database results? Perhaps that, and using display() instead of fetch() would help. Instead of assigning the data, assign the result object.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7367
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 15, 2010 9:22 pm    Post subject: Reply with quote

You can make multiple caches for one URL by setting a unique cache_id. See the docs on caching. What may work well for a cache_id is something like md5(serialize($_POST));

As for the pause before rendering, it goes like this:

* start php script
* do SQL queries, assign to tpl
* call $smarty->display()

It isn't until you call display() that the page would begin to render. PHP does isn't own output buffering too, to keep the stream efficient. You may have to call flush() manually in places if you want to force the output right away.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7367
Location: Lincoln Nebraska, USA

PostPosted: Thu Apr 15, 2010 9:25 pm    Post subject: Reply with quote

douglassdavis wrote:
everything, including the queries is done before it displays the page.


You are using smarty 2 I presume. Hey mohrt, does smarty2 include a way to fetch from and iterate over database results? Perhaps that, and using display() instead of fetch() would help. Instead of assigning the data, assign the result object.


Yes you can, good idea.

http://www.smarty.net/forums/viewtopic.php?t=16633
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP