简体   繁体   English

SQL WHERE 子句挂在一个 AWS RDS PostgreSQL 数据库上,但不是另一个

[英]SQL WHERE clause hangs on one AWS RDS PostgreSQL database but not the other

I have two AWS PostgreSQL databases running PostgreSQL 12. The following query is instant on both database A and database B:我有两个运行 PostgreSQL 12 的 AWS PostgreSQL 数据库。以下查询在数据库 A 和数据库 B 上都是即时的:

select DISTINCT table_schema, table_name, column_name,constraint_type 
from information_schema.key_column_usage 
 JOIN information_schema.table_constraints USING (table_schema,table_name,constraint_schema,constraint_name);

And returns something like this.并返回这样的东西。 I've just included one schema for an example in the below output.我刚刚在下面的 output 中包含了一个示例模式。 (Around 40,000 records on database A and around 60,000 records on database B) (数据库 A 大约 40,000 条记录,数据库 B 大约 60,000 条记录)

      table_schema       |                table_name                 |               column_name                | constraint_type
-------------------------+-------------------------------------------+------------------------------------------+-----------------
 _remote                 | table_example_1                           | test_field_id                            | FOREIGN KEY
 _remote                 | table_example_1                           | id                                       | PRIMARY KEY
 _remote                 | table_example_1                           | tenant_id                                | FOREIGN KEY
 _remote                 | table_example_2                           | id                                       | PRIMARY KEY
 _remote                 | table_example_3                           | endpoint_id                              | FOREIGN KEY
 _remote                 | table_example_3                           | id                                       | PRIMARY KEY

However, as soon as I add a where clause to the end of the select, database A (which has fewer records overall) hangs, while database B returns instantly.但是,只要我在 select 的末尾添加 where 子句,数据库 A(总体记录较少)就会挂起,而数据库 B 会立即返回。

select DISTINCT table_schema, table_name, column_name,constraint_type 
from information_schema.key_column_usage 
  JOIN information_schema.table_constraints USING (table_schema,table_name,constraint_schema,constraint_name) 
where constraint_type in('PRIMARY KEY','FOREIGN_KEY') ;

What could be causing this?这可能是什么原因造成的? My first guess is the actual AWS infrastructure itself, but I'm curious if anyone has any ideas from a SQL standpoint.我的第一个猜测是实际的 AWS 基础设施本身,但我很好奇是否有人从 SQL 的角度有任何想法。

The WHERE clause works instantly when I try to filter by other columns, so it seems to be specifically constraint_type .当我尝试按其他列过滤时, WHERE 子句立即起作用,因此它似乎特别是constraint_type

Answering my own question.回答我自己的问题。 The issue was with the optimiser statistics.问题在于优化器统计信息。

This occurred due to a missing step on my part when upgrading Postgresql version on AWS.这是因为我在 AWS 上升级 Postgresql 版本时缺少步骤。 Although AWS do most of the work for you during a major version upgrade, one of the steps you must do manually is as follows (from AWS docs):尽管 AWS 在主要版本升级期间为您完成了大部分工作,但您必须手动执行的步骤之一如下(来自 AWS 文档):

Run the ANALYZE operation to refresh the pg_statistic table.运行 ANALYZE 操作以刷新 pg_statistic 表。 You should do this for every database on all your PostgreSQL DB instances.您应该为所有 PostgreSQL 数据库实例上的每个数据库执行此操作。 Optimizer statistics aren't transferred during a major version upgrade, so you need to regenerate all statistics to avoid performance issues.在主要版本升级期间不会传输优化器统计信息,因此您需要重新生成所有统计信息以避免性能问题。 Run the command without any parameters to generate statistics for all regular tables in the current database, as follows: ANALYZE VERBOSE运行不带任何参数的命令,为当前数据库中的所有常规表生成统计信息,如下: ANALYZE VERBOSE

After I ran the ANALYZE VERBOSE command, statistics were regenerated and my performance issues were resolved.在我运行ANALYZE VERBOSE命令后,重新生成了统计信息并且我的性能问题得到了解决。

Full documentation can be found here: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_UpgradeDBInstance.PostgreSQL.html完整文档可在此处找到: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_UpgradeDBInstance.PostgreSQL.ZFC35FDC70D5FC69D2698Z83A822C7A5E

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM