Skip to content

Commit

Permalink
Improve performance when filtering by tags
Browse files Browse the repository at this point in the history
  • Loading branch information
SharkyKZ committed Dec 23, 2019
1 parent e15a20a commit b877703
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 45 deletions.
60 changes: 36 additions & 24 deletions administrator/components/com_content/models/articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected function getListQuery()
$query->select(
$this->getState(
'list.select',
'DISTINCT a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' .
'a.id, a.title, a.alias, a.checked_out, a.checked_out_time, a.catid' .
', a.state, a.access, a.created, a.created_by, a.created_by_alias, a.modified, a.ordering, a.featured, a.language, a.hits' .
', a.publish_up, a.publish_down, a.note'
)
Expand Down Expand Up @@ -353,36 +353,48 @@ protected function getListQuery()
$query->where('a.language = ' . $db->quote($language));
}

// Filter by a single or group of tags.
$hasTag = false;
$tagId = $this->getState('filter.tag');
$tag = $this->getState('filter.tag');

if (is_numeric($tagId))
// Run simplified query when filtering by one tag.
if (\is_array($tag) && \count($tag) === 1)
{
$hasTag = true;

$query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId);
}
elseif (is_array($tagId))
{
$tagId = ArrayHelper::toInteger($tagId);
$tagId = implode(',', $tagId);

if (!empty($tagId))
{
$hasTag = true;

$query->where($db->quoteName('tagmap.tag_id') . ' IN (' . $tagId . ')');
}
$tag = $tag[0];
}

if ($hasTag)
if ($tag && \is_array($tag))
{
$query->join('LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
. ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
. ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_content.article')
$tag = ArrayHelper::toInteger($tag);

$subQuery = $db->getQuery(true)
->select('DISTINCT ' . $db->quoteName('content_item_id'))
->from($db->quoteName('#__contentitem_tag_map'))
->where(
array(
$db->quoteName('tag_id') . ' IN (' . implode(',', $tag) . ')',
$db->quoteName('type_alias') . ' = ' . $db->quote('com_content.article'),
)
);

$query->join(
'INNER',
'(' . $subQuery . ') AS ' . $db->quoteName('tagmap')
. ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
);
}
elseif ($tag = (int) $tag)
{
$query->join(
'INNER',
$db->quoteName('#__contentitem_tag_map', 'tagmap')
. ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
)
->where(
array(
$db->quoteName('tagmap.tag_id') . ' = ' . $tag,
$db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_content.article'),
)
);
}

// Add the list ordering clause.
$orderCol = $this->state->get('list.ordering', 'a.id');
Expand Down
56 changes: 35 additions & 21 deletions administrator/components/com_newsfeeds/models/newsfeeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected function getListQuery()
$query->select(
$this->getState(
'list.select',
'DISTINCT a.id, a.name, a.alias, a.checked_out, a.checked_out_time, a.catid,' .
'a.id, a.name, a.alias, a.checked_out, a.checked_out_time, a.catid,' .
' a.numarticles, a.cache_time, a.created_by,' .
' a.published, a.access, a.ordering, a.language, a.publish_up, a.publish_down'
)
Expand Down Expand Up @@ -253,33 +253,47 @@ protected function getListQuery()
}

// Filter by a single or group of tags.
$hasTag = false;
$tagId = $this->getState('filter.tag');
$tag = $this->getState('filter.tag');

if (is_numeric($tagId))
// Run simplified query when filtering by one tag.
if (\is_array($tag) && \count($tag) === 1)
{
$hasTag = true;

$query->where($db->quoteName('tagmap.tag_id') . ' = ' . (int) $tagId);
$tag = $tag[0];
}
elseif (is_array($tagId))
{
$tagId = implode(',', ArrayHelper::toInteger($tagId));

if (!empty($tagId))
{
$hasTag = true;

$query->where($db->quoteName('tagmap.tag_id') . ' IN (' . $tagId . ')');
}
if ($tag && \is_array($tag))
{
$tag = ArrayHelper::toInteger($tag);

$subQuery = $db->getQuery(true)
->select('DISTINCT ' . $db->quoteName('content_item_id'))
->from($db->quoteName('#__contentitem_tag_map'))
->where(
array(
$db->quoteName('tag_id') . ' IN (' . implode(',', $tag) . ')',
$db->quoteName('type_alias') . ' = ' . $db->quote('com_newsfeeds.newsfeed'),
)
);

$query->join(
'INNER',
'(' . $subQuery . ') AS ' . $db->quoteName('tagmap')
. ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
);
}

if ($hasTag)
elseif ($tag = (int) $tag)
{
$query->join('LEFT', $db->quoteName('#__contentitem_tag_map', 'tagmap')
$query->join(
'INNER',
$db->quoteName('#__contentitem_tag_map', 'tagmap')
. ' ON ' . $db->quoteName('tagmap.content_item_id') . ' = ' . $db->quoteName('a.id')
. ' AND ' . $db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_newsfeeds.newsfeed')
);
)
->where(
array(
$db->quoteName('tagmap.tag_id') . ' = ' . $tag,
$db->quoteName('tagmap.type_alias') . ' = ' . $db->quote('com_newsfeeds.newsfeed'),
)
);
}

// Add the list ordering clause.
Expand Down

0 comments on commit b877703

Please sign in to comment.