python - 远程 Postgresql 很慢
问题描述
我使用 Django ORM 运行 .py 文件,该文件连接到另一台服务器上的 Postgresql 服务器。两台服务器都在 Ubuntu 20.04 上运行
当我运行相同的文件时,它需要以下时间:
- 使用 postgresql 在服务器上 2-3 秒
- 在另一台服务器上 8-12 秒。
当运行具有更多进程的 .py 文件时,可能需要 20 秒。如果我在 postgresql 服务器上同时运行相同的脚本,无论如何需要 2-3 秒
我试过了:
- 关闭两台服务器上的防火墙(sudo ufw disable)
- 更改 postgres 配置,然后重新启动 postgres 服务器
- 使用 pgBouncer
我检查了服务器上的互联网速度及其正常
这是 postgresql.conf
# Generated by PGConfig 2.0 beta
## http://pgconfig.org
# Memory Configuration
shared_buffers = 2GB
effective_cache_size = 6GB
work_mem = 41MB
maintenance_work_mem = 512MB
# Checkpoint Related Configuration
min_wal_size = 512MB
max_wal_size = 2GB
checkpoint_completion_target = 0.9
wal_buffers = 16MB
# Network Related Configuration
listen_addresses = '*'
max_connections = 1000
# Storage Configuration
random_page_cost = 1.1
effective_io_concurrency = 200
# Worker Processes
max_worker_processes = 8
max_parallel_workers_per_gather = 4
max_parallel_workers = 8
# Logging configuration for pgbadger
logging_collector = on
log_checkpoints = on
log_connections = on
log_disconnections = on
log_lock_waits = on
log_temp_files = 0
lc_messages = 'C'
# Adjust the minimum time to collect data
log_min_duration_statement = '10s'
log_autovacuum_min_duration = 0
# 'csvlog' format configuration
log_destination = 'csvlog'
在 pg_hba 我只插入 1 个字符串
#
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all all md5
有这个速度是正常的还是我可以配置?
解决方案
推荐阅读
- python - Python 3.7 anaconda 环境-import _ssl DLL 加载失败错误
- python - 无需下载即可查找在线视频的视频尺寸
- react-native - testflight 会清除本地数据吗?
- ios - 如何在 UIActivityViewController 中只保留空投选项?
- sql - U-SQL 是否接受使用多个 FIRST_VALUE 来删除特定列中的重复项?
- javascript - 纹理缓存溢出:16 > 可用的纹理单元
- google-app-engine - 将 Datastore Admin 映射到不同的 URL?
- c++ - [temp.spec]/6 的起源故事?
- sql - 带有时隙的约会计划的数据库模式
- java - 使用 Spark 的 MapReduce 调用不同的函数并聚合