简体   繁体   English

Python postgreSQL 很慢

[英]Python postgreSQL is very slow

I'm doing a for Loop of x that is a list of 30-50 URLS through 1 table to see if any of the URL matches --- ( it took more than 5 Seconds )我正在做一个x的 for 循环,它是一个包含 30-50 个 URLS 到 1 个表的列表,以查看是否有任何 URL 匹配 ---(花了超过5 秒

About this system :关于这个系统

  • this is a free database from Heroku.com i'm using it as a test这是Heroku.com提供的免费数据库,我将其用作测试
  • as it says on the code i'm using " postgreSQL正如它在代码上所说的那样,我正在使用“ postgreSQL
  • I'm using flask_sqlalchemy我正在使用flask_sqlalchemy
  • i'm running this Local hosted so the front end and the backend is my laptop which is i5 1.9ghz and 8g rams.我正在运行这个本地托管,所以前端和后端是我的笔记本电脑,它是i5 1.9ghz 和 8g 内存。
  • time stamp difference is around 5 Sec just to check those 50 entries with 1 user online locally时间戳差异大约是5 秒,只是为了在本地检查 1 个用户在线的那50 个条目

python the code. python代码。

from flask_sqlalchemy import SQLAlchemy 
app.config['SQLALCHEMY_DATABASE_URI'] = "postgres://***" 
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = "True" 
db = SQLAlchemy(app,session_options={"autoflush": False}) 

class test1(db.Model): 
    id = db.Column(db.Integer, primary_key = True) 
    url = db.Column(db.String(255)) 
    code = db.Column(db.String(255)) 


    def __init__ (self,url,code): 
        self.url = url 
        self.code = code 

x = [url1,url2,url3,url4,....url50] 

t2 = datetime.time(datetime.now()) 
print("before : ",t2) 

for key in x: 

    y = test1.query.filter(test1.url == key).all() 
    if y: 
        print('----i got it') 
    else: 
        print('no') 

t3 = datetime.time(datetime.now()) 
print("after forloop : ",t3) 

I have asked many people in the chatrooms, many of them have their own different suspicions but no one can confirm why there is such a massive delay in a simple process.我在聊天室问过很多人,他们中的很多人都有不同的怀疑,但没有人能确认为什么在一个简单的过程中会有如此大的延迟。

You are creating separate queries for each test, which require a new SQL statement sent to the database server.您正在为每个测试创建单独的查询,这需要将新的 SQL 语句发送到数据库服务器。

That's not that big an issue for a local database, living on the same server.对于位于同一台服务器上的本地数据库来说,这不是什么大问题。 But on Heroku, the database does not live on the same server.但是在 Heroku 上,数据库不在同一台服务器上。 The database lives somewhere else in the cloud, and it depends on your specific configuration if those two locations are near to one another, or far apart.数据库位于云中的其他位置,这取决于您的特定配置,这两个位置是彼此靠近还是相距很远。

Connecting to any Heroku-hosted Postgres database from Africa is going to be slowed down by latency.从非洲连接到任何Heroku 托管的 Postgres 数据库会因延迟而变慢。 That latency is compounded by the number of queries you make.您所做的查询数量增加了这种延迟。

You can test how much of a latency, with a simple command:您可以使用一个简单的命令来测试延迟的程度:

time psql postgresql://*** -c select 1;

The difference between the real and user components is roughly the round-trip time it takes for one very simple query to reach the database server and the response to come back to you.真实组件和用户组件之间的区别大致在于一个非常简单的查询到达数据库服务器以及返回给您的响应所需的往返时间。

If you are experiencing huge delays in queries when deployed on Heroku , then your database instance and web Dyno are probably located in two different regions.如果您在 Heroku 上部署时遇到查询的巨大延迟,那么您的数据库实例和 Web Dyno 可能位于两个不同的区域。 Relocate one or the other to live in the same region, at the very least.至少搬迁一个或另一个居住在同一地区。 If you are connecting to the database from your own machine, then you'll also see large latencies.如果您从自己的机器连接到数据库,那么您也会看到很大的延迟。

On the command line, run the heroku info command to check what region your app is located in (look for the Region line).在命令行上,运行heroku info命令以检查您的应用程序所在的区域(查找Region行)。 Or check the Settings tab on the Heroku dashboard for your app.或者检查 Heroku 仪表板上的设置选项卡为您的应用程序。 See the Heroku Regions documentation for more information, and read up on migrating apps if you want to move it to another region.有关更多信息,请参阅Heroku区域文档,如果您想将其移动到另一个区域,请阅读迁移应用程序

If you want to move the database to the same region as your app, you'll have to re-create it, I believe.如果您想将数据库移动到与您的应用程序相同的区域,我相信您必须重新创建它。 Create and download a full backup, drop the database add-on, re-create it, and it should be created in the same region as the app.创建并下载完整备份,删除数据库附加组件,重新创建它,它应该与应用程序在同一区域创建。 Then restore the database from your backup.然后从备份中恢复数据库。

Even if your app dyno and database are in the same region, you'll still see larger latencies than you'd see with Flask and Postgres installed on your local machine.即使您的应用程序 dyno 和数据库在同一区域,您仍然会看到比在本地机器上安装 Flask 和 Postgres 时看到的更大的延迟。 How much latency you'll see also depends on the specific Heroku Postgres plan you pick .您会看到多少延迟还取决于您选择的特定Heroku Postgres 计划 The Hobby tier (free) only allows for 20 connections and has no in-memory cache, so you'll need to re-use connections more often and you can't count on the server having cached table information that'd be useful for a series of related queries. Hobby 层(免费)仅允许 20 个连接并且没有内存缓存,因此您需要更频繁地重用连接,并且您不能指望服务器具有缓存的表信息一系列相关查询。

Instead of querying in the loop you can query one time using in_ and search in memory.您可以使用 in_ 查询一次并在内存中搜索,而不是在循环中查询。

test1_objs = test1.query.filter(test1.url.in_(x)).all()
test1_urls = set()
for test1_obj in test1_objs:
    test1_urls.add(test1_obj.url)
for key in x: 
    if key in test1_urls: 
        print('----i got it') 
    else: 
        print('no') 

This will reduce the time but I think it is happening because of the network latency.这将减少时间,但我认为这是由于网络延迟而发生的。 If you use server on the cloud and try again the latency will be less.如果您在云上使用服务器并重试,则延迟会更少。

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

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