简体   繁体   English

使用 SQL 从数据库视图而不是表中读取

[英]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.

相关问题 从 python 函数中返回 DataFrame 而不是使用 Pandas 的 pd.read_sql_query 列表 - Return DataFrame from within python function rather than list using pandas' pd.read_sql_query 如何通过 JDBC 从 Oracle 数据库中检索实际的 blob 数据而不是“oracle.sql.blob@”? - How to retrieve actual blob data rather than "oracle.sql.blob@" from an Oracle database over JDBC? pd.read_html 导入长字符串而不是表格 - pd.read_html importing a long string rather than a table 从 html 表中提取数据<p>而不是<table> - extracting data from an html table in <p> rather than <table> 使用 pd.read_sql 和 asyncio 从数据库读取 - Read from database using pd.read_sql and asyncio 使用python pandas从sql表读取postgres数组 - read postgres array from sql table using python pandas 如何使用 SQL Alchemy 从 INFORMATION_SCHEMA 读取表格? - How to read a table from INFORMATION_SCHEMA using SQL Alchemy? 在Tensorflow中使用文件中的数据而不是随机数据 - Using data from a file rather than random data in Tensorflow 使用值而不是索引从Python列表中选择 - Selecting from Python lists using values rather than indices 使用谷歌驱动器中的文件而不是在 colab 中上传 - Using files from google drive rather than uploading in colab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM