CodeIgniter Forums
Codeigniter session class doing a ton of identical queries every page load - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Codeigniter session class doing a ton of identical queries every page load (/thread-52325.html)



Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-06-2012

[eluser]@li[/eluser]
My controllers are loading slowly. In order to investigate I turned on the profiler. In the profiler's output, I noticed that at the very start of the page, these queries are being run:

Code:
UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'



UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'



UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'



UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'



UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'



UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'



UPDATE `ci_sessions` SET `last_activity` = 1338996582, `user_data` = 'a:7:{(omitted);}' WHERE `session_id` = '093d065eb9253da9454efd2362cee404'

This is adding about 2 seconds of load time to all my pages. Why is it running these queries about 7 times? Where can I change it so it only runs them once and that's enough?


Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-06-2012

[eluser]@li[/eluser]
bump


Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-11-2012

[eluser]getSurreal[/eluser]
What's in your access_log (or also error_log) for a single page load?

I have something similar with my application because a single page call is also making a few AJAX queries, but it is nowhere near slowing my page load to 2 seconds.


Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-11-2012

[eluser]InsiteFX[/eluser]
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date : 5/6/2012
* Time : 11:19:06 PM
* The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class MY_Session
*
* @package  Package  CodeIgniter
* @subpackage Subpackage session
* @category category sessions
* @author  Raymond L King Sr.
* @link  http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/

/**
* ------------------------------------------------------------------------
* CI Session Class Extension for AJAX calls.
* ------------------------------------------------------------------------
*
* ====- Save as application/libraries/MY_Session.php -====
*/

class MY_Session extends CI_Session {

// --------------------------------------------------------------------

/**
  * sess_update()
  *
     * Do not update an existing session on ajax or xajax calls
     *
     * @access    public
     * @return    void
     */
    public function sess_update()
{
  $_ci = get_instance();

  if ( ! $_ci->input->is_ajax_request())
  {
   parent::sess_update();
  }
    }

// --------------------------------------------------------------------

/**
  * sess_destroy()
  *
     * Clear's out the user_data array on sess::destroy.
     *
     * @access    public
     * @return    void
     */
public function sess_destroy()
{
  $this->userdata = array();

  parent::sess_destroy();
}

}

/* ------------------------------------------------------------------------
* End of file MY_Session.php
* Location: ./application/libraries/MY_Session.php
* ------------------------------------------------------------------------
*/



Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-29-2012

[eluser]erikig[/eluser]
The previous poster was right, if you have any Ajax code (or any assets - images or js) that are being generated via CodeIgniter, you'll run into this since each request .

What's worked really well for me was turning on MySQL's query cache. It cache's commonly requested queries and speeds up the response rate by reducing latency at the database level.
http://www.cyberciti.biz/tips/enable-the-query-cache-in-mysql-to-improve-performance.html

Try it out and let us know if it helped.

E


Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-29-2012

[eluser]WanWizard[/eluser]
The session library calls sess_write() whenever you update a session variable, so it's quite logical that a request leads to multiple session update queries.

It has nothing to do with ajax, it's by design.


Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-30-2012

[eluser][email protected][/eluser]

Parse error: syntax error, unexpected T_VARIABLE in D:\wamp\www\CodeIgniter_2.1.0\application\controllers\chat.php on line 90

plz solve this problams



<?php


class chat extends CI_controller

{
function chat()
{
parent::__construct();

$this->load->model('chat_model');
}

function index()
{

$this->view_data['chat_id'] = 1;

if(! $this->session->userdata('logged_in')) {

redirect('user/login');
}

$this->view_data['user_id'] = $this->session->userdata('user_id');

$this->this->session->set_userdata('last_chat_message_id_'. chat_id, 0);

$this->view_data['page_title'] = 'chatting a web base chat app';

$this->view_data['page_content'] = 'view_chat';

$this->load_view('view_main', $this->view_data);


function ajax_add_chat_message()
{


/*thing that need to be past'ed to this function
*
*
*chat_id
*user_id
*chat_message_content
*
*
*
*
*/
$chat_id=$this->input->post('chat_id');

$user_id=$this->input->post('user_id');

$chat_message_contant=$this->input->post('chat_id', TRUE);

$this->chat_model->add_chat_message_content($chat_id, $user_id, $chat_message_content);

echo $this->_get_chat_messages($chat_id);
}
function ajax_get_chat_messages()
{
$chat_id = $this->input->post('chat_id');

echo $this->_get_chat_messages($chat_id);



function _get_chat_messages()

(int)last_chat_message_id = $this->session-> userdata ('last_chat_message_id_' . $chat_id);
{
$chat_message = $this->chat_model->get_chat_message('$chat_id, $last_chat_message_id');

if ($chat_messages->num_rows()> 0)

//store the last message id

$last_chat_message_id = $chat_messages->row(- 1)->chat_message_id;

$this->this->session->set_userdata('last_chat_message_id_'. chat_id, $last_chat_message_id);

//we have some chat let's return it

$chat_messages_html = '<ul>';

foreach ($chat_messages->result() as $chat_message)

$li_class = ($this->swssion->userdata('user_id') == $chat_message->user_id) ? 'class="by_curreent_deta"' : '';

$chat_messages_html .= '<li' . $li_class . '>' . '<span class="chat_message_header">' . $chat_message->chat_message_timestamp. ' by ' $chat_message->name . '</span><p.class="message_content">' . $chat_message->chat_messge_content . '</p></li>';

$chat_messages_html .= '</ul>';

$result = array('status' => 'ok', 'content'=> $chat_messages_html);

return json_encode($result);

exit();
}
else{
//there was an error do something

$result = array('status' => 'ok', 'content' => '');

return json_encode($result);

exit();
}

}

?&gt;



Codeigniter session class doing a ton of identical queries every page load - El Forum - 06-30-2012

[eluser]WanWizard[/eluser]
Duplicate post...