Changeset 2087


Ignore:
Timestamp:
Sep 12, 2007, 5:37:01 AM (17 years ago)
Author:
rvelices
Message:

bug 376: improved get_available_tags sql queries for large databases

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_tag.inc.php

    r1900 r2087  
    88// | last update   : $Date$
    99// | last modifier : $Author$
    10 // | revision      : $Revision$
    1110// | revision      : $Revision$
    1211// +-----------------------------------------------------------------------+
     
    4039{
    4140  // we can find top fatter tags among reachable images
    42   $tags_query = '
    43 SELECT id, name, url_name, count(*) counter
    44   FROM '.IMAGE_TAG_TABLE.'
    45     INNER JOIN '.TAGS_TABLE.' ON tag_id = id';
    46 
    47   $where_tag_img =
    48     get_sql_condition_FandF
     41  $query = '
     42SELECT tag_id, COUNT(DISTINCT(it.image_id)) counter
     43  FROM '.IMAGE_CATEGORY_TABLE.' ic
     44    INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF
    4945    (
    5046      array
     
    5248          'forbidden_categories' => 'category_id',
    5349          'visible_categories' => 'category_id',
    54           'visible_images' => 'image_id'
     50          'visible_images' => 'ic.image_id'
    5551        ),
    56       'WHERE'
    57     );
    58 
    59   if (!empty($where_tag_img))
    60   {
    61     // first we need all reachable image ids
    62     $images_query = '
    63 SELECT DISTINCT image_id
    64   FROM '.IMAGE_CATEGORY_TABLE.'
    65   '.$where_tag_img.'
    66 ;';
    67     $image_ids = array_from_query($images_query, 'image_id');
    68     if ( empty($image_ids) )
    69     {
    70       return array();
    71     }
    72     $tags_query.= '
    73   WHERE image_id IN ('.
    74       wordwrap(
    75         implode(', ', $image_ids),
    76         80,
    77         "\n"
    78         ).')';
    79   }
    80 
    81   $tags_query.= '
    82   GROUP BY tag_id
    83 ;';
    84 
    85   $result = pwg_query($tags_query);
     52      '
     53  WHERE'
     54    ).'
     55  GROUP BY tag_id';
     56  $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
     57
     58  if ( empty($tag_counters) )
     59  {
     60    return array();
     61  }
     62
     63  $query = '
     64SELECT id, name, url_name
     65  FROM '.TAGS_TABLE;
     66  $result = pwg_query($query);
    8667  $tags = array();
    8768  while ($row = mysql_fetch_assoc($result))
    8869  {
    89     array_push($tags, $row);
    90   }
    91 
     70    $counter = @$tag_counters[ $row['id'] ];
     71    if ( $counter )
     72    {
     73      $row['counter'] = $counter;
     74      array_push($tags, $row);
     75    }
     76  }
    9277  return $tags;
    9378}
Note: See TracChangeset for help on using the changeset viewer.