Changeset 2326


Ignore:
Timestamp:
May 3, 2008, 3:51:50 AM (16 years ago)
Author:
rvelices
Message:

just some optimizations (especially for large dbs)

  • replace some REGEXP with LIKE in sql
  • optimized queries for the combination of large data sets with picture_url_style file
Location:
branches/branch-1_7
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/branch-1_7/include/category_cats.inc.php

    r2202 r2326  
    5858  $query = '
    5959SELECT
    60   id, name, permalink, representative_picture_id, comment, nb_images,
     60  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
    6161  date_last, max_date_last, count_images, count_categories
    6262  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
     
    9696    ON ic.category_id = c.id';
    9797    $query.= '
    98   WHERE uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
    99 '.get_sql_condition_FandF
     98  WHERE uppercats LIKE \''.$row['uppercats'].',%\''
     99  .get_sql_condition_FandF
    100100  (
    101101    array
     
    105105        'visible_images' => 'image_id'
    106106      ),
    107     'AND'
     107    "\n  AND"
    108108  ).'
    109109  ORDER BY RAND()
     
    122122  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
    123123  ON id = cat_id and user_id = '.$user['id'].'
    124   WHERE uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
    125     AND representative_picture_id IS NOT NULL
    126 '.get_sql_condition_FandF
     124  WHERE uppercats LIKE \''.$row['uppercats'].',%\'
     125    AND representative_picture_id IS NOT NULL'
     126  .get_sql_condition_FandF
    127127  (
    128128    array
     
    130130        'visible_categories' => 'id',
    131131      ),
    132     'AND'
     132    "\n  AND"
    133133  ).'
    134134  ORDER BY RAND()
  • branches/branch-1_7/include/section_init.inc.php

    r2301 r2326  
    238238    {// flat categories mode
    239239      if ( isset($page['category']) )
    240       {
    241         $subcat_ids = get_subcat_ids( array($page['category']['id']) );
     240      { // get all allowed sub-categories
     241        $query = '
     242SELECT id
     243  FROM '.CATEGORIES_TABLE.'
     244  WHERE
     245    uppercats LIKE "'.$page['category']['uppercats'].',%" '
     246    .get_sql_condition_FandF(
     247      array
     248        (
     249          'forbidden_categories' => 'id',
     250          'visible_categories' => 'id',
     251        ),
     252      "\n  AND"
     253          );
     254        $subcat_ids = array_from_query($query, 'id');
     255        $subcat_ids[] = $page['category']['id'];
    242256        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
     257        // remove categories from forbidden because just checked above
     258        $forbidden = get_sql_condition_FandF(
     259              array( 'visible_images' => 'id' ),
     260              'AND'
     261          );
    243262      }
    244263      else
     
    503522SELECT id,file
    504523  FROM '.IMAGES_TABLE .'
    505   WHERE id IN ('.implode(',',$page['items']).')
    506   AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
    507 ;
     524  WHERE file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"';
     525    if ( count($page['items']) < 500)
     526    {// for very large item sets do not add IN - because slow
     527      $query .= '
     528  AND id IN ('.implode(',',$page['items']).')
     529  LIMIT 0,1';
     530    }
    508531    $result = pwg_query($query);
    509     if (mysql_num_rows($result)>0)
    510     {
    511       list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
     532    switch (mysql_num_rows($result))
     533    {
     534      case 0: break;
     535      case 1:
     536        list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
     537        break;
     538      default: // more than 1 file name match
     539        while ($row = mysql_fetch_row($result) )
     540        {
     541          if ( in_array($row[0], $page['items']) )
     542          {
     543            list($page['image_id'], $page['image_file']) = $row;
     544            break;
     545          }
     546        }
    512547    }
    513548  }
  • branches/branch-1_7/picture.php

    r2310 r2326  
    3939}
    4040
     41$page['rank_of'] = array_flip($page['items']);
     42
    4143// if this image_id doesn't correspond to this category, an error message is
    4244// displayed, and execution is stopped
    43 if (!in_array($page['image_id'], $page['items']))
     45if ( !isset($page['rank_of'][$page['image_id']]) )
    4446{
    4547  page_not_found(
     
    103105// |                            initialization                             |
    104106// +-----------------------------------------------------------------------+
    105 
    106 $page['rank_of'] = array_flip($page['items']);
    107107
    108108// caching first_rank, last_rank, current_rank in the displayed
Note: See TracChangeset for help on using the changeset viewer.