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.selectAllForAdmin #107

Closed wtune closed 4 years ago

wtune commented 4 years ago

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

SELECT 
  COUNT(1) 
FROM 
  comment c 
  LEFT JOIN topic t ON c.topic_id = t.id 
  LEFT JOIN user u ON u.id = c.user_id 
WHERE 
  c.in_time BETWEEN '2019-10-01' 
  AND '2019-10-31'

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 in_time BETWEEN '2019-10-01' AND '2019-10-31' without any join operator. So the counting query has a chance to be optimized.

atjiu commented 4 years ago

105