Slower Ajax Response

Does putting the view logic inside the controller increase page load speed? Because I have a controller action calling a view which I am loading through ajax and I am getting a very slow response. Why is that happening?

Does putting the view logic inside controller like…

echo "<div>View echoed</div>"

a good way to code? does this increase efficiency?

I have writted a controller action like

class Ajax extends CI_Controller{ 
  foo ($id){
   $data = $this->model->get_data();//has joins in query
   $this->load->view($data);
 }
}

In view,

I have used two foreach loops nested to get the table having about 100 rows

and I load the view through a jquery ajax request to the url ajax/foo/1

My question clearly is whether it is a good practice to put view-logic into the controller for ajax calls? will it increase the speed my page will load.

I am new to coding things very professionally. Is this a good and a professional method or any other alternative available.

4

Taking a stab at this for what has been asked, rather than trying to change your direction 🙂

NO

but then again many people will disagree with me.

If there is ONE thing I’ve learned over the years, it’s this…

“Design patterns, such as MVC and others are NOT a religion, they are there to serve as pointers on how things have worked well in the past, and how you might want to apply them in the future.”

There are not, nor should they ever be, a dedicated rule book that states this is how you MUST do this project, or how you MUST do projects of type X.

Using an AJAX based response is designed to allow your page to load, then to allow the other parts to catch up with it as it does so.

If you think about it for a moment. What would you do, if you saw a blank white page that took ages to load, versus what you would do if you saw a page with SOME content load then wait while it loaded other sub-content, esp if said page had some kind of “Loading data please wait…” message on it.

Looking at your description, I’d be very confident that your slow down is actually in your Database code, esp as if you say your doing a lot of joins and such like.

It’s very easy to determine this, just change your model code to something temporary that returns maybe 10 simple rows with no joins or anything, and see what happens.

Ajax calls by their very nature are not really that special, they are no different to regular calls to a web server in terms of making a HTTP request, that is, it’s still the same server, it’s still the same network connection and it’s still the same latency.

Weather or not you put the view logic into your controller or not is entirely up to you, myself personally I don’t have a problem with it, but view logic is all I’ll put in, other’s opinion will vary.

As for speeding up your database access, well there are a lot of things you can do, but since I don’t know anything about your project I can’t really tell you whats the best idea, all I can do is give you some pointers.

Some things you can try:

Reduce your joins… joins are a performance killer in most cases, esp if your using something like codeigniter to generate them. Instead what you might want to do is look at using a stored procedure in the database, or even better use a view to flatten your rows into a single DTO then from PHP all you have to worry about is that flat model, if your crunching a LOT of data, then try to get the database to do as much of the heavy lifting for you as possible, that’s what it’s designed for.

Use a faster database server, either software or up the spec of the actual machine if you can…

Use caching… if your not data change critical, and you can afford for your web UI to be a few seconds behind it’s writes, then use memcache or some other caching solution, even if you cache only for a few seconds, on a busy server that can make a huge amount of difference.

Try to make your database calls asynchronous, that is fire off the request to the database, then instead of sitting waiting for it to respond to you, go back to doing other things and set up some kind of a call back so that the database tells you when it’s ready. environments such as NodeJS and F# excel at this type of model of operation, so that your not waiting for I/O operations all the time.

I can’t tell you 100% how to solve this, as there are far too many unknowns, but the advice I’ve given should hopefully get you thinking about different approaches and angles to explore.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật