Changeset 427

Show
Ignore:
Timestamp:
2011-04-22 08:23:54 (1 year ago)
Author:
hannes
Message:

redundant storage of number of posts per topic for better performance

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/classes/misc/Post.php

    r413 r427  
    524524                        } 
    525525                        // get last page of topic 
    526                         $q = $C->prepare('SELECT COUNT(postid) FROM ' . $SETTINGS['dbtableprefix'] . 'posts WHERE topic=:id'); 
    527                         $q->bindParam(':id', $INPUT['reply'], PDO::PARAM_INT, 12); 
    528                         $q->execute(); 
    529                         $row = $q->fetchColumn(); 
    530                         $q = NULL; 
     526                        $numberofposts = Topic::countPosts($INPUT['reply']); 
    531527                        // calculate page 
    532                         $page = ceil($row / $SETTINGS['postsperpage']); 
     528                        $page = ceil($numberofposts / $SETTINGS['postsperpage']); 
    533529                        // send e-mail notifications (using the language the receipient has set) 
    534530                        $q = $C->prepare('SELECT member FROM ' . $SETTINGS['dbtableprefix'] . 'subscriptions WHERE topic = :id'); 
  • trunk/classes/pages/Category.php

    r407 r427  
    8585                    $q3 = NULL; 
    8686                    // number of posts 
    87                     $q4 = $C->prepare('SELECT COUNT(postid) AS posts FROM ' . $SETTINGS['dbtableprefix'] . 'posts WHERE topic IN (SELECT topicid FROM ' . $SETTINGS['dbtableprefix'] . 'topics WHERE forum=:forum)'); 
     87                    $q4 = $C->prepare('SELECT SUM(posts) AS posts FROM ' . $SETTINGS['dbtableprefix'] . 'topics WHERE forum = :forum'); 
    8888                    $q4->bindParam(':forum', $row2['forumid'], PDO::PARAM_INT, 12); 
    8989                    $q4->execute(); 
  • trunk/classes/pages/Delete.php

    r205 r427  
    8585                    $q->execute(); 
    8686                    $q = NULL; 
     87                    // recount posts 
     88                    Topic::countPosts($INPUT['id']); 
    8789                    // success message 
    8890                    $div = $this->html->body->addChild('div', $LANG['post_deleted']); 
  • trunk/classes/pages/Topic.php

    r215 r427  
    7272            $postbuttons = new Postbuttons(TRUE, $row->forum, TRUE, $row->topicid, $row->closed); 
    7373            $this->html->body->addElement($postbuttons->get()); 
    74             // get number of posts 
    75             $q2 = $C->prepare('SELECT COUNT(postid) AS posts FROM ' . $SETTINGS['dbtableprefix'] . 'posts WHERE topic=:topic'); 
    76             $q2->bindParam(':topic', $row->topicid, PDO::PARAM_INT, 12); 
    77             $q2->execute(); 
    78             $row2 = $q2->fetchObject(); 
    79             $q2 = NULL; 
    8074            // calculate start for this page 
    8175            if (!isset($INPUT['page']) || $INPUT['page'] == '') { 
     
    8478            $_start = $SETTINGS['postsperpage'] * ($INPUT['page'] - 1); 
    8579            // build page navigation 
    86             $pages = new Pagination($SETTINGS['postsperpage'], $INPUT['page'], $row2->posts); 
     80            $pages = new Pagination($SETTINGS['postsperpage'], $INPUT['page'], $row->posts); 
    8781            // display page navigation 
    8882            if ($_pagination = $pages->display('topic', Array('id'), Array($row->topicid))) { 
     
    269263    } /* function getTitle */ 
    270264 
     265    /** 
     266     * Function:    countPosts 
     267     * Description: write updated number of posts into the database 
     268     * Input:       $id - topic's id 
     269     * Returns:     the number of posts (-1 on error) 
     270     **/ 
     271    public static function countPosts($id) { 
     272        global $C, $SETTINGS; 
     273        $q = $C->prepare('SELECT COUNT(*) FROM ' . $SETTINGS['dbtableprefix'] . 'posts WHERE topic = :id'); 
     274        $q->bindParam(':id', $id, PDO::PARAM_INT, 12); 
     275        if ($q->execute()) { 
     276            $number = $q->fetchColumn(); 
     277            $q = NULL; 
     278            $q = $C->prepare('UPDATE ' . $SETTINGS['dbtableprefix'] . 'topics SET posts = :number WHERE topicid = :id'); 
     279            $q->bindParam(':number', $number, PDO::PARAM_INT); 
     280            $q->bindParam(':id', $id, PDO::PARAM_INT); 
     281            if ($q->execute()) { 
     282                $q = NULL; 
     283                return $number; 
     284            } else { 
     285                $q = NULL; 
     286                return -1; 
     287            } 
     288        } else { 
     289            $q = NULL; 
     290            return -1; 
     291        } 
     292    } /* function countPosts */ 
     293 
    271294} /* class Topic */ 
    272295 
  • trunk/db/mysql.sql

    r425 r427  
    5959  pinned INTEGER NOT NULL DEFAULT 0, 
    6060  poll INTEGER NOT NULL DEFAULT 0, 
    61   topic_views INTEGER NOT NULL DEFAULT 0 
     61  topic_views INTEGER NOT NULL DEFAULT 0, 
     62  posts INTEGER NOT NULL DEFAULT 0 
    6263) CHARACTER SET utf8 COLLATE utf8_unicode_ci; 
    6364 
  • trunk/db/postgresql.sql

    r425 r427  
    5959  pinned INTEGER NOT NULL DEFAULT 0, 
    6060  poll INTEGER NOT NULL DEFAULT 0, 
    61   topic_views INTEGER NOT NULL DEFAULT 0 
     61  topic_views INTEGER NOT NULL DEFAULT 0, 
     62  posts INTEGER NOT NULL DEFAULT 0 
    6263); 
    6364 
  • trunk/db/sqlite.sql

    r425 r427  
    5959  pinned INTEGER NOT NULL DEFAULT 0, 
    6060  poll INTEGER NOT NULL DEFAULT 0, 
    61   topic_views INTEGER NOT NULL DEFAULT 0 
     61  topic_views INTEGER NOT NULL DEFAULT 0, 
     62  posts INTEGER NOT NULL DEFAULT 0 
    6263); 
    6364