CodeIgniter Forums
CodeIgniter 3 only connect to database if theres queries to run? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: CodeIgniter 3 only connect to database if theres queries to run? (/showthread.php?tid=81752)



CodeIgniter 3 only connect to database if theres queries to run? - Yaiko - 04-22-2022

I'm using CI3 and I noticed that every time that I enter to any Controller a connection to the database is made even if is not needed.

I used the Profiler ( $this->output->enable_profiler(TRUE); ) to see the performance and when I remove the database library from the autoload.php the pages that don't use any queries loads a lot faster. I've read the Docs but don't see any related to connect to the database only when a query is needed, for the moment I'm only importing the ( $this->load->database(); ) in each model instead of importing it in the autoloader and loading the models only when needed.
Is there any easier option to this behaviour with the Database Library?

Thanks.


RE: CodeIgniter 3 only connect to database if theres queries to run? - John_Betong - 04-22-2022

@Yaiko,

I have just checked an old CI3 application and noticed that I have the following set:

File: ./app/config/autolload.php
Code:
// $autoload['libraries'] = array('database', 'image_lib','table' );
$autoload['libraries'] = array
(
    'database',
    // 'user_agent',
); //'table', 'image_lib',
#    'session',

Try removing the database auto loading and see what errors/warnings are created.

Also check the online documentation for: "Manually Connecting to a Database"


RE: CodeIgniter 3 only connect to database if theres queries to run? - ignitedcms - 04-23-2022

Just to add to this, rather than it being a connection issues, check the pages that use the database have queries that are optimized.


RE: CodeIgniter 3 only connect to database if theres queries to run? - Yaiko - 04-25-2022

(04-22-2022, 10:19 PM)John_Betong Wrote: @Yaiko,

I have just checked an old CI3 application and noticed that I have the following set:

File: ./app/config/autolload.php
Code:
// $autoload['libraries'] = array('database', 'image_lib','table' );
$autoload['libraries'] = array
(
'database',
// 'user_agent',
); //'table', 'image_lib',
# 'session',

Try removing the database auto loading and see what errors/warnings are created.

Also check the online documentation for: "Manually Connecting to a Database"

Yes i tried removing database from the autoloader so the pages where no queries are ran load faster. but what I'm trying to do is that when having the database in the autoloader do not connect to the database until a first query is done. I didn't find any information in the documentation so I guess I will try create a custom function...