Nucleus

NP_MultipleCategoriesの高速化

2006年12月15日

 このブログではNP_MultipleCategories0.38jに若干改造を加えたものを用いているが、カテゴリリストの表示に時間がかかるのが難点であった。アクセスの時刻にもよるが、ページ全体の表示に3.5秒ほどかかり、その時間のほとんどがMultipleCategories::showCategoryList()関数の実行に費やされている。スピード化を試みた。

 ソースを見てみると、SQLクエリ中で『RegEx』が多用されている。これがスピードが遅くなる原因であろう。しかも、SQLクエリが入れ子構造になっているので、その解消を行った。まず、

echo TEMPLATE::fill($this->getOption('catheader'),
                    array(
                        'blogid' => $blogid,
                        'blogurl' => $blogurl,
                        'self' => $CONF['Self']
                    ));
/* begin modification by kat */
$items=array();
$catdata=array();
$scatdata=array();
$query = 'SELECT inumber,icat FROM '.sql_table('item').
    ' WHERE iblog='.(int)$blogid.
    ' AND itime<='.mysqldate($b->getCorrectTime()).
    ' AND idraft=0';
$res = sql_query($query);
while ($row=mysql_fetch_row($res)) {
    $items[$row[0]]=true;
    $catdata[$row[1]][$row[0]]=true;
    
}
$query = 'SELECT item_id, categories, subcategories FROM '.sql_table('plug_multiple_categories');
$res = sql_query($query);
while ($row=mysql_fetch_row($res)) {
    if (!$items[$row[0]]) continue;
    foreach(explode(',',$row[1]) as $cat) if ($cat) $catdata[$cat][$row[0]]=true;
    foreach(explode(',',$row[2]) as $scat) if ($scat) $scatdata[$scat][$row[0]]=true;
}
/* end modification by kat */

とし、すべてのデータを配列にキャッシュする。続けて、

/* begin modification by kat */
/*
$cq = 'SELECT count(*) as result FROM '.sql_table('item').' as i';
$cq .= ' LEFT JOIN '.sql_table('plug_multiple_categories').' as p ON  i.inumber=p.item_id';
$cq .= ' WHERE ((i.inumber=p.item_id and (p.categories REGEXP "(^|,)'.$data['catid'].'(,|$)" or i.icat='.$data['catid'].')) or (p.item_id IS NULL and i.icat='.$data['catid'].'))';
$cq .= ' and i.itime<=' . mysqldate($b->getCorrectTime()) . ' and i.idraft=0';

$data['catamount'] = quickQuery($cq);
*/
$data['catamount']=count($catdata[$data['catid']]);
/* end modification by kat */

及び、

/* begin modification by kat */
/*
$ares = sql_query(
    'SELECT count(i.inumber) FROM '
    . sql_table('item').' as i, '
    . sql_table('plug_multiple_categories').' as p'
    . ' WHERE i.idraft=0 and i.itime<='.mysqldate($b->getCorrectTime())
    . ' and i.inumber=p.item_id'
    . ' and p.subcategories REGEXP "(^|,)'.$sdata['subcatid'].'(,|$)"'
);
if ($ares && $row = mysql_fetch_row($ares)) {
*/
if ($row[0]=count($scatdata[$sdata['subcatid']])) {
/* end modification by kat */

のようにして、キャッシュに取り込んだ内容を呼び出すことでクエリ中の『RegEx』を回避した。

 これで、すっきり!3.5秒かかっていた表示が0.5秒で済むようになった。うーん、延び延びになっているキャッシングプラグインの開発が、これでまた延びるなぁ。

NP_MultipleCategories::showCategoryList() 全部を表示するには、ここをクリック

コメント

コメントはありません

コメント送信