atjiu / pybbs

更实用的Java开发的社区(论坛),Better use of Java development community (forum)
GNU Affero General Public License v3.0
1.84k stars 706 forks source link

Sub-optimal count query for commentMapper.selectByUserId #106

Closed wtune closed 4 years ago

wtune commented 4 years ago

For finding comments by user id via the automatic paged query selectByUserId, a query counting the total number of rows selectByUserId retrieves will be issued:

SELECT 
  COUNT(1) 
FROM 
  comment c 
  LEFT JOIN topic t ON t.id = c.topic_id 
  LEFT JOIN user u ON t.user_id = u.id 
  LEFT JOIN user uu ON c.user_id = uu.id 
WHERE 
  c.user_id = 3

However, since the query use comment to left join topic and user table on primary key id, the final result is the same to query select count(1) from comment where user_id = 3 without any join operator. So the counting query has a chance to be optimized.

atjiu commented 4 years ago

105