Welcome Guest, Not a member yet? Register   Sign In
Slow performance when retrieving data
#1

[eluser]bigmac90[/eluser]
Hey Guys,

I'm using Codeigniter to create a web application to retrieve information from an SQL server database. I'm developing the application in WAMP and I'm trying to populate a dropdown list from the database. The data consists of about 800 rows and every time I run the application, it takes some time for the application to show with the populated dropdown list. Why is the application taking it's time to load and populate the data in the dropdown list. Is it because I'm developing the application on my localhost and the database is on another server. Or is it another reason why Codeigniter's performance is lacking???

#2

[eluser]Stefan Hueg[/eluser]
There are four performance aspects here:

Database Connection (Usually takes 1-2 seconds, depending on the location)
You may speed it up using persistent connections or cache the results locally. Be aware that persistent connections may cause your database server to not respond anymore (connection limit)

Database Layout
Did you set up keys and indexes properly?

Database Query
Do you have any expensive queries / subqueries or joins on tables that dont have any indexes?

Rendering Speed / user-side
Populating 800 option list entries is not a big deal but rendering is.

Please provide some code / queries / database schemes and we might find your problem.
#3

[eluser]bigmac90[/eluser]
Hey

Thanks for the quick reply. At the moment I have two databases configured in CodeIgniter. One database is on WAMP and the second database is stored on another server and has all the relevant and needed keys, etc.

I actually have four queries to retrieve data and populate that data into a dropdown list in my view page.

My Controller Page looks like:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

function __construct() {
  parent::__construct();
  $this->is_logged_in();
}

// Make sure the user is logged in
function is_logged_in() {
  $is_logged_in = $this->session->userdata('is_logged_in');

  if (!isset($is_logged_in) || $is_logged_in != true) {
   echo "You don't have permission to access this page";
   echo "<br>";
   echo "<a href = 'http://localhost/ProjectSage'>Click Here to Log In</a>";
   die();
  }
}

function members_area() {
  // Load the model get_clients_model and run function
  $this->load->model('get_clients_model');
  $data['clients'] = $this->get_clients_model->get_clients();

  // Get array from status_array function
  $data['status'] = $this->status_array();

  $this->load->model('get_type_model');
  $data['type'] = $this->get_type_model->get_types();

  $this->load->model('get_supplier_model');
  $data['supplier'] = $this->get_supplier_model->get_suppliers();

  // Load the view and pass $data to that view
  $this->load->view('members_area', $data);

  
}

function status_array() {
  $status_array = array (
   'assigned' => 'Assigned',
   'approved' => 'Approved',
   'held' => 'Held',
   'rejected' => 'Rejected'
  );

  return $status_array;
}
}

One of my models retrieving the data:

Code:
&lt;?php

class Get_supplier_model extends CI_Model {

function get_suppliers() {
  $otherdb = $this->load->database('otherdb', TRUE);
  $otherdb->select('SupplierID, SName');

  $query = $otherdb->get('Supplier');

  if ($query->result()) {
   foreach ($query->result() as $supplier) {
    $finalResult[$supplier->SupplierID] = $supplier->SName;
   }
   return $finalResult;
  } else {
   return FALSE;
  }
}
}

?&gt;

My View code looks like:

Code:
<!doctype html>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;ONESG - OneInvoice Exporter&lt;/title&gt;

&lt;!-- CSS/LESS Code Links --&gt;
&lt;link rel = "stylesheet" type = "text/css" href = "&lt;?php echo base_url();?&gt;css/site_style.css"/&gt;
&lt;/head&gt;

&lt;body&gt;
<div class = "login_wrapper">
  <img class = "onesg_logo" src = "&lt;?php echo base_url();?&gt;img/ONESGLOGO.png"/>  
  <div class = "top_form">
   <p>Filter By:</p>

   &lt;?php
    echo form_open();
     echo form_label('Client:', 'client');
     echo form_dropdown('clients', $clients, '', 'id = "client"');

     echo form_label('Site:', 'site');
     echo form_dropdown();

     echo form_label('Status:', 'status');
     echo form_dropdown('status', $status, '', 'id = "status"');

     echo form_label('Type:', 'type');
     echo form_dropdown('type', $type, '', 'id = "type"');

     echo "<p></p>";

     echo form_label('Supplier:', 'supply');
     echo form_dropdown('supplier', $supplier, '', 'id = "supplier"');
    
     echo form_label('Reference:', 'reference');
     echo form_input('reference', '', 'placeholder = "Enter Reference"');

     echo "<p>Filter invoice by date:</p>";

     echo form_label('To:', 'to');
     echo form_input();

     echo form_label('From:', 'from');
     echo form_input();

     echo "<br>";

     echo "<p>Filter invoice by approval date:</p>";

     echo form_label('To:', 'to');
     echo form_input();

     echo form_label('From:', 'from');
     echo form_input();
    echo form_close();
   ?&gt;
  </div>
</div>
&lt;/body&gt;
&lt;/html&gt;

Is part of the reason why codeigniter is running slow because I'm sending to much data to the view in an array from the controller???
#4

[eluser]Stefan Hueg[/eluser]
First of all (unrelated)
You should use

Code:
if ($query->num_rows()) {....

instead of

Code:
if ($query->result()) {....

Sencond, I haven't found anything suspicious despite the external database thing. It's not only the connection time but also delay during the actual query and return the data. There are three Query-related steps here:

-Connection
-Query sent to the server
-Results returned to the client.

Each of these steps takes time. And due to the nature of the TCP Protocol (error correction), it takes some time.

You may set two benchmarking points (before the connect and after everything is finished) using

Code:
$before = microtime(TRUE);
...
...
echo "The Query took " . (microtime(TRUE) - $before) . " seconds"

...or enable the profiler in your controller:

Code:
$this->output->enable_profiler();

My suggestion: Cut the expensive external database query out of the normal processing flow and load the data via AJAX, using another controller method and jQuery.

It is a good practice to load such things seperately.
#5

[eluser]bigmac90[/eluser]
Hey

Thanks for the help. Tell me if I'm being stupid. Is it possible to retrieve the data from the database output it into an XML file and then populate the dropdown using the XML file. Is this a viable solution or am i just being an idiot and just making work for myself???
#6

[eluser]noideawhattotypehere[/eluser]
It will get better when your project is stored in the same server as db
#7

[eluser]bigmac90[/eluser]
@noideawhattotypehere

So it is the fact that I'm developing the application on my localhost trying to access a database that is on another server that is slowing down the application????
#8

[eluser]noideawhattotypehere[/eluser]
Just a wild guess, it SHOULD be better, I dont see anything suspicious in your code. Anyway, why dont you make a dump of your db, set it up on your localhost and try it out?
#9

[eluser]bigmac90[/eluser]
Thanks for the help guys. Imported the database into my localhost and it's now the application is quicker than the road runner. Again thanks for all your help!!!




Theme © iAndrew 2016 - Forum software by © MyBB