[英]Read from a database view rather than a table using SQL
I am trying to read data from a view that I have created based on other views in my database using Python.我正在尝试从我使用 Python 根据我的数据库中的其他视图创建的视图中读取数据。
Database set-up:数据库设置:
Redshift-postgres
dev
Schemas
schema_name
Tables
table_name
Views
view_name
Python code:蟒蛇代码:
connection=psycopg2.connect("dbname=dev host=redshift.amazonaws.com port=5439 user=user password=password")
cursor = connection.cursor()
schema = "SELECT count(*) FROM schema_name.table_name;"
schema2 = "SELECT count(*) FROM schema_name.view_name;"
result = pd.read_sql(schema, connection)
When I run the code with schema
it returns data from a table but when I try to run schema2
it does not return anything and no error message is provided.当我使用
schema
运行代码时,它会从表中返回数据,但是当我尝试运行schema2
它不返回任何内容,也没有提供任何错误消息。 The script does not stop it just idles.脚本不会停止它只是空闲。
Am I doing something wrong when trying to get data from a view rather than a table?尝试从视图而不是表中获取数据时,我做错了什么吗?
UPDATE更新
When I run schema2 = "SELECT * FROM INFORMATION_SCHEMA.TABLES;"
当我运行
schema2 = "SELECT * FROM INFORMATION_SCHEMA.TABLES;"
it does return the following:它确实返回以下内容:
table_catalog table_schema table_name table_type
dev schema_name view_name VIEW
It lists the view I am trying to read data from.它列出了我试图从中读取数据的视图。
I created the view like so:我创建了这样的视图:
CREATE OR REPLACE VIEW schema_name.view_name
AS SELECT *
CASE
WHEN statement
ELSE false
END AS column_name,
FROM schema_name.another_view_name
LEFT JOIN schema_name.another_view_name_2 data_1 ON data_2 = data_3
LEFT JOIN schema_name.another_view_name_3 data_4 ON data_5 = data_6
I have named irelevant data data_x
where I join the views based on some conditions.我有一个名为irelevant数据
data_x
,我加入基于某些条件的意见。
Turned out that amazon redshift DB got clogged up and had 5 queries that ran for 4 days with no response.结果发现 amazon redshift DB 被阻塞了,有 5 个查询运行了 4 天没有响应。 Anyhow, I still find that reading from a view with 2 left joins takes like 3 hours to read which is not how it should work.
无论如何,我仍然发现从具有 2 个左连接的视图中阅读需要 3 个小时才能阅读,这不是它应该如何工作的。 The view has only 1.2m rows.
该视图只有 1.2m 行。 Thank you for your suggestions.
谢谢你的建议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.