0

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.

  • Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Dec 23 '13 at 7:38
  • This would totally depend on a lot of details. Many people nowadays have 'service only' servers sending only JSON data and the view part is solely done by browsers and mobile devices. Unless you send data that needs very complex processing it is very likely that you do something else wrong here if an Ajax call has such an effect on your page load. – thorsten müller Dec 23 '13 at 7:39
  • @gnat : I believe I was not clear on my question. I have edited it. guide me if I am wrong.! – Mohammed Ashiq Dec 23 '13 at 7:54
  • Did you measure that to see where exactly things are slow? There are many places and it's unlikely that generating html from about 100 rows of data is really the bottleneck. Is the problem server side (it takes long to respond? Then why would you want to bring even more work to the server). Does the browser get the data fast but then takes long to show it on the screen? Is this only this small thing or does the page as a whole render slow? – thorsten müller Dec 23 '13 at 9:06
3

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.

|improve this answer|||||
  • Thanks.. you shed a great light on what I was worrying. Thanks for your time. I am trying your suggestions. replacing joins with procedures and caching. Thanks a lot. – Mohammed Ashiq Dec 24 '13 at 13:09
  • your very welcome :-) I hope you succeed in solving your problem. – shawty Dec 24 '13 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.