Skip to content

Commit

Permalink
[Performance] Category manager / Tag Manager, get all counters via si…
Browse files Browse the repository at this point in the history
…ngle query, reduce 20, 50, 100 queries (page limit) to 1 (#22117)

* Categoy manager, get all counters via single query

* Replace countItems() and countTagItems() methods with a reusuable counterRelations() method in ContentHelper class

* CS

* Since deploy version

* Better ordering for states to property names array

* Coding style (#6)

* Removed unneeded  parameter from counting methods

* CS
  • Loading branch information
ggppdk authored and Michael Babker committed Nov 1, 2018
1 parent ebdd957 commit 8b48522
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 373 deletions.
49 changes: 8 additions & 41 deletions administrator/components/com_banners/helpers/banners.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,54 +188,21 @@ public static function getClientOptions()
/**
* Adds Count Items for Category Manager.
*
* @param stdClass[] $items The banner category objects
* @param stdClass[] &$items The category objects
*
* @return stdClass[]
*
* @since 3.5
*/
public static function countItems(&$items)
{
$db = JFactory::getDbo();

foreach ($items as $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select('state, count(*) AS count')
->from($db->qn('#__banners'))
->where('catid = ' . (int) $item->id)
->group('state');
$db->setQuery($query);
$banners = $db->loadObjectList();

foreach ($banners as $banner)
{
if ($banner->state == 1)
{
$item->count_published = $banner->count;
}

if ($banner->state == 0)
{
$item->count_unpublished = $banner->count;
}

if ($banner->state == 2)
{
$item->count_archived = $banner->count;
}

if ($banner->state == -2)
{
$item->count_trashed = $banner->count;
}
}
}
$config = (object) array(
'related_tbl' => 'banners',
'state_col' => 'state',
'group_col' => 'catid',
'relation_type' => 'category_or_group',
);

return $items;
return parent::countRelations($items, $config);
}
}
119 changes: 20 additions & 99 deletions administrator/components/com_contact/helpers/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,61 +57,28 @@ public static function addSubmenu($vName)
/**
* Adds Count Items for Category Manager.
*
* @param stdClass[] $items The contact category objects
* @param stdClass[] &$items The category objects
*
* @return stdClass[]
*
* @since 3.5
*/
public static function countItems(&$items)
{
$db = JFactory::getDbo();

foreach ($items as $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select('published AS state, count(*) AS count')
->from($db->qn('#__contact_details'))
->where('catid = ' . (int) $item->id)
->group('published');
$db->setQuery($query);
$contacts = $db->loadObjectList();

foreach ($contacts as $contact)
{
if ($contact->state == 1)
{
$item->count_published = $contact->count;
}

if ($contact->state == 0)
{
$item->count_unpublished = $contact->count;
}

if ($contact->state == 2)
{
$item->count_archived = $contact->count;
}

if ($contact->state == -2)
{
$item->count_trashed = $contact->count;
}
}
}
$config = (object) array(
'related_tbl' => 'contact_details',
'state_col' => 'published',
'group_col' => 'catid',
'relation_type' => 'category_or_group',
);

return $items;
return parent::countRelations($items, $config);
}

/**
* Adds Count Items for Tag Manager.
*
* @param stdClass[] $items The banner tag objects
* @param stdClass[] &$items The tag objects
* @param string $extension The name of the active view.
*
* @return stdClass[]
Expand All @@ -120,64 +87,18 @@ public static function countItems(&$items)
*/
public static function countTagItems(&$items, $extension)
{
$db = JFactory::getDbo();
$parts = explode('.', $extension);
$section = null;

if (count($parts) > 1)
{
$section = $parts[1];
}

$join = $db->qn('#__contact_details') . ' AS c ON ct.content_item_id=c.id';

if ($section === 'category')
{
$join = $db->qn('#__categories') . ' AS c ON ct.content_item_id=c.id';
}

foreach ($items as $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select('published as state, count(*) AS count')
->from($db->qn('#__contentitem_tag_map') . 'AS ct ')
->where('ct.tag_id = ' . (int) $item->id)
->where('ct.type_alias =' . $db->q($extension))
->join('LEFT', $join)
->group('published');

$db->setQuery($query);
$contacts = $db->loadObjectList();

foreach ($contacts as $contact)
{
if ($contact->state == 1)
{
$item->count_published = $contact->count;
}

if ($contact->state == 0)
{
$item->count_unpublished = $contact->count;
}

if ($contact->state == 2)
{
$item->count_archived = $contact->count;
}

if ($contact->state == -2)
{
$item->count_trashed = $contact->count;
}
}
}
$parts = explode('.', $extension);
$section = count($parts) > 1 ? $parts[1] : null;

$config = (object) array(
'related_tbl' => ($section === 'category' ? 'categories' : 'contact_details'),
'state_col' => 'published',
'group_col' => 'tag_id',
'extension' => $extension,
'relation_type' => 'tag_assigments',
);

return $items;
return parent::countRelations($items, $config);
}

/**
Expand Down
120 changes: 20 additions & 100 deletions administrator/components/com_content/helpers/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,61 +91,28 @@ public static function filterText($text)
/**
* Adds Count Items for Category Manager.
*
* @param stdClass[] $items The banner category objects
* @param stdClass[] &$items The category objects
*
* @return stdClass[]
*
* @since 3.5
*/
public static function countItems(&$items)
{
$db = JFactory::getDbo();

foreach ($items as $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select('state, count(*) AS count')
->from($db->qn('#__content'))
->where('catid = ' . (int) $item->id)
->group('state');
$db->setQuery($query);
$articles = $db->loadObjectList();

foreach ($articles as $article)
{
if ($article->state == 1)
{
$item->count_published = $article->count;
}

if ($article->state == 0)
{
$item->count_unpublished = $article->count;
}

if ($article->state == 2)
{
$item->count_archived = $article->count;
}

if ($article->state == -2)
{
$item->count_trashed = $article->count;
}
}
}
$config = (object) array(
'related_tbl' => 'content',
'state_col' => 'state',
'group_col' => 'catid',
'relation_type' => 'category_or_group',
);

return $items;
return parent::countRelations($items, $config);
}

/**
* Adds Count Items for Tag Manager.
*
* @param stdClass[] $items The content objects
* @param stdClass[] &$items The tag objects
* @param string $extension The name of the active view.
*
* @return stdClass[]
Expand All @@ -154,65 +121,18 @@ public static function countItems(&$items)
*/
public static function countTagItems(&$items, $extension)
{
$db = JFactory::getDbo();
$parts = explode('.', $extension);
$section = null;

if (count($parts) > 1)
{
$section = $parts[1];
}

$join = $db->qn('#__content') . ' AS c ON ct.content_item_id=c.id';
$state = 'state';

if ($section === 'category')
{
$join = $db->qn('#__categories') . ' AS c ON ct.content_item_id=c.id';
$state = 'published as state';
}

foreach ($items as $item)
{
$item->count_trashed = 0;
$item->count_archived = 0;
$item->count_unpublished = 0;
$item->count_published = 0;
$query = $db->getQuery(true);
$query->select($state . ', count(*) AS count')
->from($db->qn('#__contentitem_tag_map') . 'AS ct ')
->where('ct.tag_id = ' . (int) $item->id)
->where('ct.type_alias =' . $db->q($extension))
->join('LEFT', $join)
->group('state');
$db->setQuery($query);
$contents = $db->loadObjectList();

foreach ($contents as $content)
{
if ($content->state == 1)
{
$item->count_published = $content->count;
}

if ($content->state == 0)
{
$item->count_unpublished = $content->count;
}

if ($content->state == 2)
{
$item->count_archived = $content->count;
}

if ($content->state == -2)
{
$item->count_trashed = $content->count;
}
}
}
$parts = explode('.', $extension);
$section = count($parts) > 1 ? $parts[1] : null;

$config = (object) array(
'related_tbl' => ($section === 'category' ? 'categories' : 'content'),
'state_col' => ($section === 'category' ? 'published' : 'state'),
'group_col' => 'tag_id',
'extension' => $extension,
'relation_type' => 'tag_assigments',
);

return $items;
return parent::countRelations($items, $config);
}

/**
Expand Down
Loading

0 comments on commit 8b48522

Please sign in to comment.