# Ajax call really slow

**URL:** <https://www.ruby-forum.com/t/ajax-call-really-slow/91649>\
**Category:** Rails\
**Created:** [April 11, 2007, 5:58am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649 "2007-04-11T05:58:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 5:58am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/1 "2007-04-11T05:58:44Z")

</div>

Hey Guys,

I’m fairly new to Rails, so my guess is that I’m just doing this  
wrong. I have a site that I’m building that uses ajax to update a  
list of items depending on what tab a user clicks. Unfortunately, it  
isn’t live yet so I can’t send a link directly to the site, but I have  
it all working using straight HTML/JS/XML. Where the problems start  
is when I try to use Rails to generate the update.

At first I tried to use RJS to push a partial template that loops  
through each item to generate the html block that I need to push to  
the list. I figured this would be easy and straight forward, but even  
though I only have 2 items in the DB currently (will eventually be  
hundreds) the list was taking 2+ seconds to update even locally. I  
even tried changing my technique to use :update on a link\_to\_remote  
instead of the RJS template with no increase performance.

Next I tried leaving the javascript that I was using before in place,  
but used a partial template to build the xml file that was requested.  
This seemed to help the performance, but I still wasn’t able to  
decrease the load time to an acceptable amount.

Lastly, I’m trying to use an rxml template to build the xml instead of  
looping within a partial template:

xml.items do  
for p in @products  
xml.item(:tags =\> p.tags, :id =\> p.id) do  
xml.title(p.title)  
xml.subtitle(truncate(p.description, 100))  
xml.summary(truncate(p.description, 100))  
xml.description(p.description)  
xml.postedby(“Lance 43”, :id=\>“12343”)  
xml.postedtimestamp(“1144537625”)  
xml.images do  
xml.image(:id=\>1) do  
xml.fullsize(p.image)  
xml.small(p.image\_small)  
xml.thumb(p.image\_thumb)  
end  
end  
xml.trashed(“990”)  
xml.reported(“34”)  
xml.views(“1000”)  
xml.score(p.overall\_score)  
xml.points(“40”)  
xml.stars(“5”)  
xml.links do  
xml.link(“link 1”, :id=\>“1”, :href=\>“[http://www.link1.com](http://www.link1.com)”)  
xml.link(“link 2”, :id=\>“2”, :href=\>“[http://www.link2.com](http://www.link2.com)”)  
end  
xml.comments do  
xml.comment(:id=\>“1”) do  
xml.postedby(“Ross C.”, :id=\>“123”)  
xml.postedtimestamp(“1144537625”)  
xml.content(“This item works well elit, sed diam nonummy  
nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.  
Ut wisi enim ad minim veniam, quis”)  
end  
xml.comment(:id=\>“2”) do  
xml.postedby(“Peter J.”, :id=\>“356”)  
xml.postedtimestamp(“1144537172”)  
xml.content(“Made some fixes to the wording and did some  
other cleaning up. Man people sure spell bad. We should get a grade 10  
class to go through this as a challenge.”)  
end  
end  
end  
end  
end

Most of the above is still static as I haven’t updated the database  
schema yet to include a lot of the fields. I want to try to get this  
up and running properly before I go any further on other development.

This seems to get an initial load time of about a second, but then it  
caches and goes blazingly fast. I’m currently on Dreamhost, and am  
wondering if the slowness may be a factor of the host and not the  
code.

Please guys, any help would be GREATLY appreciated as I’m getting  
close to ditching rails for this project all together and moving back  
to PHP.

Matt

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 6:05am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/2 "2007-04-11T06:05:44Z")

</div>

What kind of server are you running this on? 2+ seconds sounds like some  
flavor of CGI.

Matt G-2 wrote:

> This seemed to help the performance, but I still wasn’t able to  
> xml.summary(truncate(p.description, 100))  
> xml.trashed(“990”)  
> xml.comment(:id=\>“1”) do  
> other cleaning up. Man people sure spell bad. We should get a grade 10
> 
> >

–  
View this message in context:  
[http://www.nabble.com/Ajax-call-really-slow](http://www.nabble.com/Ajax-call-really-slow)…-tf3557187.html#a9932877  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 6:16am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/3 "2007-04-11T06:16:45Z")

</div>

It’s a standard Dreamhost package so Apache with FastCGI and MySQL  
4.x. Do you think it is a problem with the host then? I’m loath to  
try a new host in case it is just a simple code issue. I seem to be  
able to do much more complex queries in a fraction of a second using  
PHP on the same server.

Matt

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 6:30am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/4 "2007-04-11T06:30:46Z")

</div>

Host shouldn’t be a problem. What do your logs tell you about where the  
time  
is going. Can you tail your production.log and watch as you hit it?  
You’re  
describing “hello world” Ajax that’s taking waaaaay too long. There has  
to  
be something more.

Matt G-2 wrote:

> On Apr 10, 9:04 pm, “s.ross” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > wrong. I have a site that I’m building that uses ajax to update a  
> > > even tried changing my technique to use :update on a link\_to\_remote  
> > > xml.items do  
> > > xml.fullsize(p.image)  
> > > xml.links do  
> > > end  
> > > end  
> > > Please guys, any help would be GREATLY appreciated as I’m getting
> 
> >

–  
View this message in context:  
[http://www.nabble.com/Ajax-call-really-slow](http://www.nabble.com/Ajax-call-really-slow)…-tf3557187.html#a9933046  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 6:54am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/5 "2007-04-11T06:54:47Z")

</div>

Here’s the latest from my logs. It seems to be a little faster right  
now, but I figure that’s due to a lack of traffic on the hosting  
server, or with my connection:

Completed in 0.05424 (18 reqs/sec) | Rendering: 0.01482 (27%) | DB:  
0.00679 (12%) | 200 OK

Sorry, I don’t have much experience looking through the logs, so I’m  
not sure if those are high or on par with what they should be.  
Looking at firebug, the “GET” for the XML can take between 4000+ to  
9000+ms when I first hit it.

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 7:12am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/6 "2007-04-11T07:12:50Z")

</div>

That’s the kicker. When I leave this as a static page loading static  
XML files (whith 10 or more entries instead of the 2 that are being  
generated) the calls are almost instant. The page loads with no  
slowness, and the tabs switch really fast. Blazingly fast when  
compared to the Rails render.

Can you notice anyting that I’m doing with my code that’d be causing  
this slowness? The code that I first posted is in the callxml.rxml  
file, and the following is in the controller:

def callxml  
@order\_by = ‘overall\_score desc’  
@products = Product.find(:all, :order=\>@order\_by, :limit =\> ‘3’)  
end

At this point, I’m thinking of trying a host with a money back clause  
to try if they are any better with the sped for generating the XML.  
If it does show improvement, I’ll just keep my site on that host and  
continue my development. Otherwise I’ll cancel it and convert  
everything to PHP and call 'er a day.

Matt

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 7:00am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/7 "2007-04-11T07:00:49Z")

</div>

Not blindingly fast, but not alarmingly slow. While these log entries  
are not  
precise, they suggest a far faster turnaround of a request than what you  
are  
experiencing. Is your initial pageload fast? Do static pages load fast?  
Doesn’t this seem like latency?

Matt G-2 wrote:

> not sure if those are high or on par with what they should be.
> 
> > be something more.
> > 
> > > > > through each item to generate the html block that I need to push to  
> > > > > requested.  
> > > > > xml.title(p.title)  
> > > > > end  
> > > > > end  
> > > > > xml.postedby(“Peter J.”, :id=\>“356”)
> > 
> > > > > code.

–  
View this message in context:  
[http://www.nabble.com/Ajax-call-really-slow](http://www.nabble.com/Ajax-call-really-slow)…-tf3557187.html#a9933195  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/8 "2007-04-11T15:50:01Z")

</div>

Ok, I’ve made the change to the htaccess file, and that looks to have  
sped things up considerably. I’m going to let a couple of others test  
it out to see if they notice an improvement as well.

Unfortunately, I don’t think I have enough control over the server to  
setup mongrel on Dreamhost, but if you know a way, I’d love to try it  
out!

Thank you for all of your help! I’ll let you know if I’m still having  
any issues.

Matt

---

<div class="post-metadata">

**Author:** ![Matt\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Matt\_G](https://www.ruby-forum.com/u/Matt_G)\
**Post date:** [April 11, 2007, 8:11am UTC](https://www.ruby-forum.com/t/ajax-call-really-slow/91649/9 "2007-04-11T08:11:55Z")

</div>

Can you paste your .htaccess?

I betcha it says:

RewriteRule ^(.\*)$ dispatch.cgi [QSA,L]

To use FCGI, you need:

RewriteRule ^(.\*)$ dispatch.fcgi [QSA,L]

If this is the case, then problem solved, but you should consider using  
Apache to balance mongrels or a proxy setup that will allow you to use  
mongrel\_cluster (pound or pen).

Matt G-2 wrote:

> file, and the following is in the controller:  
> everything to PHP and call 'er a day.
> 
> > > > You’re
> > > > 
> > > > > able to do much more complex queries in a fraction of a second using
> > 
> > > > > > > it all working using straight HTML/JS/XML. Where the problems  
> > > > > > > hundreds) the list was taking 2+ seconds to update even locally.  
> > > > > > > decrease the load time to an acceptable amount.  
> > > > > > > xml.subtitle(truncate(p.description, 100))  
> > > > > > > end  
> > > > > > > :href=\>“[http://www.link2.com](http://www.link2.com)”)  
> > > > > > > end  
> > > > > > > end
> > 
> > > > back
> 
> >

–  
View this message in context:  
[http://www.nabble.com/Ajax-call-really-slow](http://www.nabble.com/Ajax-call-really-slow)…-tf3557187.html#a9933710  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).
