Django 的速度慢得可怕,有 8000 个模型实例(如何删除 PostgreSQL 数据库)?
我在开发服务器上运行了 Django 安装。我最近使用了python manage.py loaddata 8000models.json
。现在一切都超级慢。页面将无法加载。 python manage.pylush
不返回。 ModelType.objects.count()
不返回。
(或者如果我等待足够长的时间,他们可能会回来。)
这里发生了什么事? Django 真的无法处理那么多数据吗?或者还有其他问题吗?
更新:我在 PostgreSQL 上观察到此问题,但在具有相同数据量的 SQLite 上未观察到此问题。也许我只需要擦除 PostgreSQL 并重新加载数据。
更新2:如何擦除 PostgreSQL 并重置? python manage.py Reset appname
没有响应。
更新 3:这就是我尝试擦除 PostgreSQL 的方式:
#! bin/bash
sudo -u postgres dropdb mydb
sudo -u postgres createdb mydb
sudo -u postgres psql mydb < ~/my-setup/my-init.sql
python ~/path/to/manage.py syncdb
但是,这会导致以下错误:
dropdb: database removal failed: ERROR: database "mydb" is being accessed by other users
DETAIL: There are 8 other session(s) using the database.
createdb: database creation failed: ERROR: database "mydb" already exists
ERROR: role "myrole" cannot be dropped because some objects depend on it
DETAIL: owner of table mydb.mytable_mytable
# ... more "owner of table", "owner of sequence" statements, etc
如何关闭这些其他会话?我没有运行阿帕奇。我一次只使用 Django 开发服务器的一个实例。然而,当它没有响应时,我用 Control+Z 杀死了它。也许这导致它不释放数据库连接,从而导致此问题?我该如何解决这个问题?
I have a Django installation running on the development server. I recently used python manage.py loaddata 8000models.json
. Now everything is super slow. Pages won't load. python manage.py flush
does not return. ModelType.objects.count()
does not return.
(Or maybe they will return if I wait a sufficiently long time.)
What's going on here? Is Django truly unable to handle that much data? Or is there some other issue?
Update: I observe this issue on PostgreSQL but not SQLite with the same amount of data. Perhaps I just need to wipe the PostgreSQL and reload the data.
Update 2: How can I wipe the PostgreSQL and reset? python manage.py reset appname
isn't responding.
Update 3: This is how I'm trying to wipe the PostgreSQL:
#! bin/bash
sudo -u postgres dropdb mydb
sudo -u postgres createdb mydb
sudo -u postgres psql mydb < ~/my-setup/my-init.sql
python ~/path/to/manage.py syncdb
However, this causes the following errors:
dropdb: database removal failed: ERROR: database "mydb" is being accessed by other users
DETAIL: There are 8 other session(s) using the database.
createdb: database creation failed: ERROR: database "mydb" already exists
ERROR: role "myrole" cannot be dropped because some objects depend on it
DETAIL: owner of table mydb.mytable_mytable
# ... more "owner of table", "owner of sequence" statements, etc
How can I close out these other sessions? I don't have Apache running. I've only been using one instance of the Django development server at a time. However, when it got unresponsive I killed it with Control+Z. Perhaps that caused it to not release a database connection, thereby causing this issue? How can I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Ctrl-Z 只是停止进程,而不会杀死它。 (我假设您使用的是 bash)在终端中输入
jobs
,您应该会看到旧进程仍在运行。一旦您终止了所有正在访问 PostgreSQL 数据库的作业,您应该能够按照预期删除、创建和同步数据库。
Ctrl-Z just stops the process, it does not kill it. (I assume you're using bash) type
jobs
in your terminal, and you should see the old processes still running.Once you kill all the jobs that are accessing the PostgreSQL database, you should be able to drop, create, and syncdb as you expect.
您是否尝试过检查您的 postrgres 数据库是否仍在回复,从它的声音来看,它可能正在处理您告诉它加载的数据。
加载数据时,您应该先让它完成。如果页面仍然需要很长时间才能呈现,您应该检查您的查询。
使用 .count(),即使在 PostgreSQL(计数相当慢)中,有 8000 个项目也应该在很长的时间内返回。
Have you tried checking that your postrgres database is still replying at all, from the sounds of it it is probably processing the data you told it to load.
When loading the data you should just let it finish first. If pages are still taking a very long time to render you should check your queries instead.
Using .count(), even in PostgreSQL (which has rather slow counts) with 8000 items should return within a good amount of time.