PHP MVC CodeIgniter – Troubleshooting Slower Ajax Response

codeignitermvcPHP

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.

Best Answer

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.