Agniva De Sarker 95fd120670 MM-40799: Improve ReplyCount query (#19274)
* MM-40799: Improve ReplyCount query

This PR optimizes the reply count query to use only
the indexed column to count the rows rather than using
the ID column. This results in an index-only scan
rather than an index scan. As a result, the query
can be satisfied directly by scanning the index
and there is no need to touch the heap.

Count(*) also gives the same result, but is not recommended
as it also verifies non-null columns and can take slightly
longer.

Before:
```
explain analyze SELECT p.*, (SELECT COUNT(Posts.Id) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) AS ReplyCount FROM Posts p WHERE (CreateAt < (SELECT CreateAt FROM Posts WHERE Id = 'bguxt6zzcjytpffgza3dc7xody') AND p.ChannelId = '5xmynf36cinrzksuzhq7my1nbc' AND DeleteAt = 0) ORDER BY p.ChannelId, DeleteAt, CreateAt DESC LIMIT 30 OFFSET 0;
                                                                                 QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=9.14..130616.04 rows=30 width=922) (actual time=19.208..19.454 rows=30 loops=1)
   InitPlan 2 (returns $2)
     ->  Index Scan using posts_pkey on posts posts_1  (cost=0.56..8.58 rows=1 width=8) (actual time=0.028..0.030 rows=1 loops=1)
           Index Cond: ((id)::text = 'bguxt6zzcjytpffgza3dc7xody'::text)
   ->  Index Scan Backward using idx_posts_channel_id_delete_at_create_at on posts p  (cost=0.56..2720977.61 rows=625 width=922) (actual time=0.075..0.317 rows=30 loops=1)
         Index Cond: (((channelid)::text = '5xmynf36cinrzksuzhq7my1nbc'::text) AND (deleteat = 0) AND (createat < $2))
         SubPlan 1
           ->  Aggregate  (cost=4349.49..4349.50 rows=1 width=8) (actual time=0.007..0.007 rows=1 loops=30)
                 ->  Index Scan using idx_posts_root_id_delete_at on posts  (cost=0.56..4346.36 rows=1253 width=27) (actual time=0.007..0.007 rows=0 loops=30)
                       Index Cond: (((rootid)::text = (CASE WHEN ((p.rootid)::text = ''::text) THEN p.id ELSE p.rootid END)::text) AND (deleteat = 0))
```

After:

```
explain analyze SELECT p.*, (SELECT COUNT(Posts.RootId) FROM Posts WHERE Posts.RootId = (CASE WHEN p.RootId = '' THEN p.Id ELSE p.RootId END) AND Posts.DeleteAt = 0) AS ReplyCount FROM Posts p WHERE (CreateAt < (SELECT CreateAt FROM Posts WHERE Id = 'bguxt6zzcjytpffgza3dc7xody') AND p.ChannelId = '5xmynf36cinrzksuzhq7my1nbc' AND DeleteAt = 0) ORDER BY p.ChannelId, DeleteAt, CreateAt DESC LIMIT 30 OFFSET 0;
                                                                                 QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=9.14..130616.04 rows=30 width=922) (actual time=12.936..13.188 rows=30 loops=1)
   InitPlan 2 (returns $2)
     ->  Index Scan using posts_pkey on posts posts_1  (cost=0.56..8.58 rows=1 width=8) (actual time=0.029..0.030 rows=1 loops=1)
           Index Cond: ((id)::text = 'bguxt6zzcjytpffgza3dc7xody'::text)
   ->  Index Scan Backward using idx_posts_channel_id_delete_at_create_at on posts p  (cost=0.56..2720977.61 rows=625 width=922) (actual time=0.076..0.324 rows=30 loops=1)
         Index Cond: (((channelid)::text = '5xmynf36cinrzksuzhq7my1nbc'::text) AND (deleteat = 0) AND (createat < $2))
         SubPlan 1
           ->  Aggregate  (cost=4349.49..4349.50 rows=1 width=8) (actual time=0.008..0.008 rows=1 loops=30)
                 ->  Index Only Scan using idx_posts_root_id_delete_at on posts  (cost=0.56..4346.36 rows=1253 width=8) (actual time=0.007..0.007 rows=0 loops=30)
                       Index Cond: ((rootid = (CASE WHEN ((p.rootid)::text = ''::text) THEN p.id ELSE p.rootid END)::text) AND (deleteat = 0))
                       Heap Fetches: 14
```

https://mattermost.atlassian.net/browse/MM-40799

```release-note
NONE
```

* Use COUNT(*) for faster

```release-note
NONE
```
2022-01-05 10:27:40 +05:30
2021-04-27 10:30:48 +02:00
2020-01-23 12:34:29 +01:00
2021-12-27 13:52:42 -06:00
2021-10-12 11:39:49 +05:30
2020-03-13 18:35:31 +01:00
2019-04-04 09:49:07 -04:00
2018-05-30 10:23:25 -04:00
2021-12-13 14:33:48 +01:00
2021-10-13 19:33:56 +02:00

Mattermost

Mattermost is an open source platform for secure collaboration across the entire software development lifecycle. This repo is the primary source for core development on the Mattermost platform; it's written in Go and React and runs as a single Linux binary with MySQL or PostgreSQL. A new compiled version is released under an MIT license every month on the 16th.

mattermost-hero

Useful Resources:

Table of Contents

Try out Mattermost

Deploy a Preview

Note: Heroku preview does not include email or persistent storage

Install Mattermost

Other Install Guides:

Native Mobile and Desktop Apps

In addition to the web interface, you can also download Mattermost clients for Android, iOS, Windows PC, Mac OSX, and Linux.

Google Play App Store Windows PC Mac OSX Linux

Get Security Bulletins

Receive notifications of critical security updates. The sophistication of online attackers is perpetually increasing. If you are deploying Mattermost it is highly recommended you subscribe to the Mattermost Security Bulletin mailing list for updates on critical security releases.

Subscribe here

Get Involved

Learn More

License

See the LICENSE file for license rights and limitations.

Get the Latest News

Contributing

Please see CONTRIBUTING.md.

Any other questions, mail us at info@mattermost.com. Wed love to meet you!

Описание
No description provided
Readme 636 MiB
Languages
TypeScript 47%
Go 40.2%
JavaScript 8.6%
SCSS 2.8%
HTML 1.1%
Разное 0.2%