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 topicMapper.selectAll #105

Closed wtune closed 4 years ago

wtune commented 4 years ago

For finding topics with user information via the automatic paged query selectAll, a query counting the total number of rows selectAll retrieves will be issued:

SELECT 
  COUNT(1) 
FROM 
  (
    select 
      t.*, 
      u.username, 
      u.avatar 
    from 
      topic t 
      left join user u on t.user_id = u.id 
    order by 
      t.top desc, 
      t.in_time desc
  ) TOTAL

However, since the query use topic to left join user table on primary key u.id, the final result is the same to query select count(1) from topic without any join operator or order by. So the counting query has a chance to be optimized.

atjiu commented 4 years ago

select count (1) This query is done by mybatis-plus framework, I did not write this sql