Changeset 2327


Ignore:
Timestamp:
May 3, 2008, 3:52:08 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:
trunk
Files:
3 edited

Legend:

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

    r2299 r2327  
    5555  $query = '
    5656SELECT
    57   id, name, permalink, representative_picture_id, comment, nb_images,
     57  id, name, permalink, representative_picture_id, comment, nb_images, uppercats,
    5858  date_last, max_date_last, count_images, count_categories
    5959  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
     
    9494    ON ic.category_id = c.id';
    9595    $query.= '
    96   WHERE uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
    97 '.get_sql_condition_FandF
     96  WHERE uppercats LIKE \''.$row['uppercats'].',%\''
     97  .get_sql_condition_FandF
    9898  (
    9999    array
     
    103103        'visible_images' => 'image_id'
    104104      ),
    105     'AND'
     105    "\n  AND"
    106106  ).'
    107107  ORDER BY RAND()
     
    120120  FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
    121121  ON id = cat_id and user_id = '.$user['id'].'
    122   WHERE uppercats REGEXP \'(^|,)'.$row['id'].'(,|$)\'
    123     AND representative_picture_id IS NOT NULL
    124 '.get_sql_condition_FandF
     122  WHERE uppercats LIKE \''.$row['uppercats'].',%\'
     123    AND representative_picture_id IS NOT NULL'
     124  .get_sql_condition_FandF
    125125  (
    126126    array
     
    128128        'visible_categories' => 'id',
    129129      ),
    130     'AND'
     130    "\n  AND"
    131131  ).'
    132132  ORDER BY RAND()
  • trunk/include/section_init.inc.php

    r2299 r2327  
    234234    {// flat categories mode
    235235      if ( isset($page['category']) )
    236       {
    237         $subcat_ids = get_subcat_ids( array($page['category']['id']) );
     236      { // get all allowed sub-categories
     237        $query = '
     238SELECT id
     239  FROM '.CATEGORIES_TABLE.'
     240  WHERE
     241    uppercats LIKE "'.$page['category']['uppercats'].',%" '
     242    .get_sql_condition_FandF(
     243      array
     244        (
     245          'forbidden_categories' => 'id',
     246          'visible_categories' => 'id',
     247        ),
     248      "\n  AND"
     249          );
     250        $subcat_ids = array_from_query($query, 'id');
     251        $subcat_ids[] = $page['category']['id'];
    238252        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
     253        // remove categories from forbidden because just checked above
     254        $forbidden = get_sql_condition_FandF(
     255              array( 'visible_images' => 'id' ),
     256              'AND'
     257          );
    239258      }
    240259      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  }
  • trunk/picture.php

    r2309 r2327  
    3636}
    3737
     38$page['rank_of'] = array_flip($page['items']);
     39
    3840// if this image_id doesn't correspond to this category, an error message is
    3941// displayed, and execution is stopped
    40 if (!in_array($page['image_id'], $page['items']))
     42if ( !isset($page['rank_of'][$page['image_id']]) )
    4143{
    4244  page_not_found(
     
    100102// |                            initialization                             |
    101103// +-----------------------------------------------------------------------+
    102 
    103 $page['rank_of'] = array_flip($page['items']);
    104104
    105105// caching first_rank, last_rank, current_rank in the displayed
     
    868868  include(PHPWG_ROOT_PATH.'include/picture_metadata.inc.php');
    869869}
    870 //------------------------------------------------------------ log informations
    871 pwg_log($picture['current']['id'], 'picture');
    872870
    873871include(PHPWG_ROOT_PATH.'include/page_header.php');
    874872trigger_action('loc_end_picture');
    875873$template->pparse('picture');
     874//------------------------------------------------------------ log informations
     875pwg_log($picture['current']['id'], 'picture');
    876876include(PHPWG_ROOT_PATH.'include/page_tail.php');
    877877?>
Note: See TracChangeset for help on using the changeset viewer.