사용자 의견
6695 단어 alonetone
묘사
수리https://github.com/sudara/alonetone/issues/205토론 #1
토론 #2
에서 성능의 영향을 검사한다.나는 우리가 이미 평론 색인에 약간의 손실을 입었다고 생각한다. 그래서 아마도 이것은 이 문제를 정리할 기회일 것이다.토론 #셋
@sudara는 무엇을 도와주고 싶습니까comment index issues
?사용자 id가 인덱스되지 않은 것을 보았습니다. 우리는 추가할 수 있습니다.당신은 더 구체적인 생각이 있습니까?
토론 #4
@InnateWonder Comments# 인덱스는 항상 느린 작업 중 하나입니다. 일부 원인은 조회가 느린 것 같습니다. (보기에도 n+1이 많은 것 같습니다)https://oss.skylight.io/app/applications/QMmsxBDrac9Q/recent/6h/endpoints/CommentsController%23index?responseType=html기존 지표는 다음과 같습니다.
t.index ["commentable_id"], name: "index_comments_on_commentable_id"
t.index ["commenter_id"], name: "index_comments_on_commenter_id"
t.index ["created_at"], name: "index_comments_on_created_at"
t.index ["is_spam"], name: "index_comments_on_is_spam"
이제 두 가지 문제가 발생할 수 있습니다. 하나는 will paginate의 계수입니다.다른 하나는 사용자 또는 일반 질의로 의견을 구하는 것입니다.
이 두 가지를 합치면 보통 300밀리초가 필요하고, 때로는 사용자당 200밀리초가 필요하다.그것들은 아마도 20밀리초 정도의 시간이 필요할 것이다.
평론은 데이터베이스에서 다태적이다. 그것들은 하나
commentable_type
가 있다. 역사상 사람들이 평론updates
(블로그 항목)과features
, 그리고assets
- 지금은 assets
에 대한 평론만 존재하기 때문이다.우리는 다태성을 제거하는 것을 고려해야 한다.현재 다음과 같이 다중 유형을 포함하도록 색인을 조정할 수 있습니다.https://mauricio.github.io/2008/09/27/handling-database-indexes-for-rails-polymorphic-associations.html
나는 이 PR이 너무 큰 손해를 끼칠 것이라고 생각하지 않는다. 그러나 우리의 인덱스는 이미 매우 나쁘다. (기존의 인덱스는 현재db가 표를 57k줄로 줄이는 것만 돕고 있기 때문에) 그래서 나는 지금이 평가하기에 좋은 시기라고 생각한다.
다음은 일반적인 리뷰 질의에 대한 설명입니다.
irb(main):010:0> User.first.comments.on_track.public_or_private(true).page(1).explain
=> EXPLAIN for: SELECT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 1 AND `comments`.`commentable_type` = 'Asset' AND `comments`.`is_spam` = FALSE ORDER BY comments.id DESC, id DESC LIMIT 30 OFFSET 0
+----+-------------+----------+------------+------+---------------------------+---------------------------+---------+-------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------------------+---------------------------+---------+-------+-------+----------+-------------+
| 1 | SIMPLE | comments | NULL | ref | index_comments_on_is_spam | index_comments_on_is_spam | 2 | const | 57392 | 1.0 | Using where |
+----+-------------+----------+------------+------+---------------------------+---------------------------+---------+-------+-------+----------+-------------+
1 row in set (0.00 sec)
이 PR에 추가 조건을 추가하면 마지막으로 몇 가지 작업만 추가됩니다.irb(main):011:0> User.first.comments.on_track.public_or_private(true).where("commenter_id != user_id").page(1).explain
=> EXPLAIN for: SELECT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 1 AND `comments`.`commentable_type` = 'Asset' AND `comments`.`is_spam` = FALSE AND (commenter_id != user_id) ORDER BY comments.id DESC, id DESC LIMIT 30 OFFSET 0
+----+-------------+----------+------------+------+----------------------------------------------------------+---------------------------+---------+-------+-------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+----------------------------------------------------------+---------------------------+---------+-------+-------+----------+-------------+
| 1 | SIMPLE | comments | NULL | ref | index_comments_on_commenter_id,index_comments_on_is_spam | index_comments_on_is_spam | 2 | const | 57392 | 0.5 | Using where |
+----+-------------+----------+------------+------+----------------------------------------------------------+---------------------------+---------+-------+-------+----------+-------------+
1 row in set (0.00 sec)
현재 type/spam/private에 색인을 추가했습니다. 이 모든 것이 더 좋을 것입니다. 이 색인은db에서 내용을 60줄로 줄이는 데 도움이 됩니다. 그래야만 다른 조건을 평가할 수 있습니다.irb(main):002:0> User.first.comments.on_track.public_or_private(true).where("commenter_id != user_id").page(1).explain
User Load (0.5ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
Comment Load (19.9ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 1 AND `comments`.`commentable_type` = 'Asset' AND `comments`.`is_spam` = FALSE AND (commenter_id != user_id) ORDER BY comments.id DESC, id DESC LIMIT 30 OFFSET 0
=> EXPLAIN for: SELECT `comments`.* FROM `comments` WHERE `comments`.`user_id` = 1 AND `comments`.`commentable_type` = 'Asset' AND `comments`.`is_spam` = FALSE AND (commenter_id != user_id) ORDER BY comments.id DESC, id DESC LIMIT 30 OFFSET 0
+----+-------------+----------+------------+-------+-------------------------------------------------------------------------------------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+-------------------------------------------------------------------------------------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | comments | NULL | index | index_comments_on_commenter_id,index_comments_on_commentable_type_and_is_spam_and_private | PRIMARY | 4 | NULL | 60 | 2.5 | Using where |
+----+-------------+----------+------------+-------+-------------------------------------------------------------------------------------------+---------+---------+------+------+----------+-------------+
1 row in set (0.00 sec)
토론 #5
에 새 인덱스가 추가되고 사용되지 않는 인덱스가 삭제되었습니다.https://github.com/sudara/alonetone/commit/42d440f048c96dfdaccdd2604b28862ab9554d90
토론 #6
Reference
이 문제에 관하여(사용자 의견), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://github.com/sudara/alonetone/issues/244텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)