我们有一个社交网站,会员可以在其中相互评价兼容性或匹配度。该user_match_ratings表包含超过 2.2 亿行(9 演出数据或近 20 演出索引)。针对此表的查询通常显示在 slow.log(阈值 > 2 秒)中,并且是系统中记录最频繁的慢查询:
Query_time: 3 Lock_time: 0 Rows_sent: 3 Rows_examined: 1051
"select rating, count(*) as tally from user_match_ratings where rated_user_id = 395357 group by rating;"
Query_time: 4 Lock_time: 0 Rows_sent: 3 Rows_examined: 1294
"select rating, count(*) as tally from user_match_ratings where rated_user_id = 4182969 group by rating;"
Query_time: 3 Lock_time: 0 Rows_sent: 3 Rows_examined: 446
"select rating, count(*) as tally from user_match_ratings where rated_user_id = 630148 group by rating;"
Query_time: …Run Code Online (Sandbox Code Playgroud) 名为“messages”的表中有大约 5000 万行。问题是,即使使用索引,特定查询也运行得非常慢。我的背景更多是 Mysql,所以我更像是 Postgres (9.5) 的新手。任何解决此表查询缓慢问题的建议或帮助将不胜感激。
\n\n该表的结构如下:
\n\nmydatabase=# \\d+ messages\n\n\n id | integer | not null default nextval(\'messages_id_seq\'::regclass) | plain | | \n conversation_id | integer | | plain | | \n user_id | integer | | plain | | \n content | text | | extended | | \n attached_photos | text | | extended | | \n attached_video | text | | extended | | \n status | character varying(255) | | extended | | …Run Code Online (Sandbox Code Playgroud)