Jack Phoenix has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/326404 )

Change subject: SQL optimization
......................................................................


SQL optimization

Patch by labster.

Bug: T135367
Change-Id: Id72790d539064cf190659eb1ab1538e2730555e2
---
M CommentClass.php
M CommentsPage.php
2 files changed, 44 insertions(+), 24 deletions(-)

Approvals:
  Jack Phoenix: Verified; Looks good to me, approved



diff --git a/CommentClass.php b/CommentClass.php
index 1bc6b71..eef25f4 100644
--- a/CommentClass.php
+++ b/CommentClass.php
@@ -127,25 +127,30 @@
                $this->thread = $data['thread'];
                $this->timestamp = $data['timestamp'];
 
-               $dbr = wfGetDB( DB_SLAVE );
-               $row = $dbr->selectRow(
-                       'Comments_Vote',
-                       array( 'Comment_Vote_Score' ),
-                       array(
-                               'Comment_Vote_ID' => $this->id,
-                               'Comment_Vote_Username' => 
$this->getUser()->getName()
-                       ),
-                       __METHOD__
-               );
-               if ( $row !== false ) {
-                       $vote = $row->Comment_Vote_Score;
+               if ( isset( $data['current_vote'] ) ) {
+                       $vote = $data['current_vote'];
                } else {
-                       $vote = false;
+                       $dbr = wfGetDB( DB_SLAVE );
+                       $row = $dbr->selectRow(
+                               'Comments_Vote',
+                               array( 'Comment_Vote_Score' ),
+                               array(
+                                       'Comment_Vote_ID' => $this->id,
+                                       'Comment_Vote_Username' => 
$this->getUser()->getName()
+                               ),
+                               __METHOD__
+                       );
+                       if ( $row !== false ) {
+                               $vote = $row->Comment_Vote_Score;
+                       } else {
+                               $vote = false;
+                       }
                }
 
                $this->currentVote = $vote;
 
-               $this->currentScore = $this->getScore();
+               $this->currentScore = isset( $data['total_vote'] )
+                       ? $data['total_vote'] : $this->getScore();
        }
 
        public static function newFromID( $id ) {
diff --git a/CommentsPage.php b/CommentsPage.php
index 617c004..dae5ffc 100644
--- a/CommentsPage.php
+++ b/CommentsPage.php
@@ -164,17 +164,32 @@
        public function getComments() {
                $dbr = wfGetDB( DB_SLAVE );
 
-               $tables = array();
-               $params = array();
-               $joinConds = array();
-
                // Defaults (for non-social wikis)
-               $tables[] = 'Comments';
+               $tables = array(
+                       'Comments',
+                       'vote1' => 'Comments_Vote',
+                       'vote2' => 'Comments_Vote',
+               );
                $fields = array(
                        'Comment_Username', 'Comment_IP', 'Comment_Text',
                        'Comment_Date', 'UNIX_TIMESTAMP(Comment_Date) AS 
timestamp',
                        'Comment_user_id', 'CommentID', 'Comment_Parent_ID',
+                       'vote1.Comment_Vote_Score AS current_vote',
+                       'SUM(vote2.Comment_Vote_Score) AS comment_score'
                );
+               $joinConds = array(
+                       // For current user's vote
+                       'vote1' => array(
+                               'LEFT JOIN',
+                               array(
+                                       'vote1.Comment_Vote_ID = CommentID',
+                                       'vote1.Comment_Vote_Username' => 
$this->getUser()->getName()
+                               )
+                       ),
+                       // For total vote count
+                       'vote2' => array( 'LEFT JOIN', 'vote2.Comment_Vote_ID = 
CommentID' )
+               );
+               $params = array( 'GROUP BY' => 'CommentID' );
 
                // If SocialProfile is installed, query the user_stats table 
too.
                if (
@@ -183,10 +198,8 @@
                ) {
                        $tables[] = 'user_stats';
                        $fields[] = 'stats_total_points';
-                       $joinConds = array(
-                               'Comments' => array(
-                                       'LEFT JOIN', 'Comment_user_id = 
stats_user_id'
-                               )
+                       $joinConds['Comments'] = array(
+                               'LEFT JOIN', 'Comment_user_id = stats_user_id'
                        );
                }
 
@@ -218,7 +231,9 @@
                                'CommentID' => $row->CommentID,
                                'Comment_Parent_ID' => $row->Comment_Parent_ID,
                                'thread' => $thread,
-                               'timestamp' => $row->timestamp
+                               'timestamp' => $row->timestamp,
+                               'current_vote' => ( isset( $row->current_vote ) 
? $row->current_vote : false ),
+                               'total_vote' => ( isset( $row->comment_score ) 
? $row->comment_score : 0 ),
                        );
 
                        $comments[] = new Comment( $this, $this->getContext(), 
$data );

-- 
To view, visit https://gerrit.wikimedia.org/r/326404
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Id72790d539064cf190659eb1ab1538e2730555e2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Comments
Gerrit-Branch: master
Gerrit-Owner: Jack Phoenix <[email protected]>
Gerrit-Reviewer: Jack Phoenix <[email protected]>
Gerrit-Reviewer: UltrasonicNXT <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to