Optimization of queries to a database in Joomla!. Part 1
Tags: joomla, optimization, database
Let’s install Joomla! 1.0.12 on a server and set in Back End:
Administration / Site / Global Configuration / Site / Debug site: Yes
Then we save changes and leave from Back End. Now we come on a site. Result—36 queries to a database.
Now we switch on caching in Back End:
Administration / Site / Global Configuration / Cache / Caching: Yes
We come on a site and press “Update” in a browser, to check up, whether the number of queries has decreased. Result—33 queries to a database. Not much better.
Now again we come in Back End and in menu Modules / Site Modules set caching (Enable Cache: Yes) for following modules:
- Main Menu
- User Menu
- Other Menu
- Syndicate
- Polls
- Newsflash
- Latest News
- Popular
- Top Menu
- Search
Also it can be made for Archive, Related Items, and, probably, for some other installed modules.
Again we come on a site and press some times “Update” in a browser, to check up, whether the number of queries has decreased. Result—13 queries to a database. It is already much better.
Let’s look, what are these queries and whether it is possible to get rid of them since they will make a significant part from the general number of queries.
Here these queries:
1st
SELECT folder, element, published, params
FROM jos_mambots
WHERE published >= 1
AND access <= 0
AND folder = ’system’
ORDER BY ordering
It arises in a file /index.php in line
// load system bot group $_MAMBOTS->loadBotGroup('system');
The purpose of the query—to load of mambots of ’system’ group. If those are absent (and in some cases this is so indeed)—it is possible to comment in that file this, and also next lines:
$_MAMBOTS->trigger('onStart');
and
$_MAMBOTS->trigger('onAfterStart');
2nd
SELECT id, link
FROM jos_menu
WHERE menutype = ‘mainmenu’
AND published = 1
ORDER BY parent, ordering
LIMIT 1
It arises in a file /index.php in the block
if ($option == '') { if ($Itemid) { // skipped } }
The purpose—to determine a default component (which should be started at visit to the address of www.yoursite.com). If this component and its Itemid are known (to default it is com_frontpage and Itemid=1—these values can be determined under the link which specifies the first menu item in the module mainmenu) it is possible to get rid of the query, having replaced the above-stated block on
if ($option == '') { $option = 'com_frontpage'; $Itemid = 1; }
3rd
SELECT template
FROM jos_templates_menu
WHERE client_id = 0
AND (menuid = 0 OR menuid = 1)
ORDER BY menuid DESC
LIMIT 1
It arises in a file /includes/joomla.php by a call of function _setTemplate of a class mosMainFrame.
The purpose—to determine a template corresponding to current menu item. If on all pages you have the same template (for example, ‘my_template’) it is possible a line
$this->_setTemplate($isAdmin);
to replace on
if ($isAdmin) $this->_setTemplate($isAdmin); else $this->_template = 'my_template';
But in this case you always will have a same template and, hence, e.g. pda-mambot will not work. If you wish to use it, it is possible to use another hack instead of the aforesaid: to replace the block
$assigned = ( !empty( $Itemid ) ? " OR menuid = " . (int) $Itemid : '' ); $query = "SELECT template" . "n FROM #__templates_menu" . "n WHERE client_id = 0" . "n AND ( menuid = 0 $assigned )" . "n ORDER BY menuid DESC" ; $this->_db->setQuery( $query, 0, 1 ); $cur_template = $this->_db->loadResult();
on
$cur_template = 'my_template';