Postgresql 在文件_fdw外部表和postgres _fdw外部表上联接
在postgreSQL 9.5中: 我有一个名为:sheetheight的外部表(由file_fdw创建)和一个名为:dzlog的外部表(由postgres_fdw创建) 1-对于连接外部表,我有以下查询:Postgresql 在文件_fdw外部表和postgres _fdw外部表上联接,postgresql,join,foreign-data-wrapper,Postgresql,Join,Foreign Data Wrapper,在postgreSQL 9.5中: 我有一个名为:sheetheight的外部表(由file_fdw创建)和一个名为:dzlog的外部表(由postgres_fdw创建) 1-对于连接外部表,我有以下查询: 从dzlog.ullid=sheetheight.ullid上的dzlog内部连接板材高度中选择* 对于上面的查询,EXPLAIN ANALYZE返回这个: ------------------------------------------------- Hash Join (cost
从dzlog.ullid=sheetheight.ullid上的dzlog内部连接板材高度中选择*
对于上面的查询,EXPLAIN ANALYZE
返回这个:
-------------------------------------------------
Hash Join (cost=111.66..13688.18 rows=20814 width=2180) (actual time=7670.872.
.8527.844 rows=2499 loops=1)
Hash Cond: (sheetheight.ullid = dzlog.ullid)
-> Foreign Scan on sheetheight (cost=0.00..12968.10 rows=106741 width=150)
(actual time=0.116..570.571 rows=223986 loops=1)
Foreign File: D:\code\sources\sheetHeight_20151025_221244_0000000004987
6878996.csv
Foreign File Size: 18786370
-> Hash (cost=111.17..111.17 rows=39 width=2030) (actual time=7658.661..765
8.661 rows=34107 loops=1)
Buckets: 2048 (originally 1024) Batches: 32 (originally 1) Memory Usa
ge: 4082kB
-> Foreign Scan on dzlog (cost=100.00..111.17 rows=39 width=2030) (ac
tual time=47.162..7578.990 rows=34107 loops=1)
Planning time: 8.755 ms
Execution time: 8530.917 ms
(10 rows)
查询的输出有两个名为ullid的列
ullid,日期,颜色,单张,dz0,dz1,dz2,dz3,dz4,dz5,dz6,dz7,ullid,单张,通行证
2-要从python应用程序直接访问csv文件和sql本地表,我有:
我没有使用FDWs,而是使用Pandas merge dataframe
从python应用程序直接访问csv文件和postgreSQL本地表,从而完成了相同的查询。这个连接是原始连接,因此我首先获取csv文件,然后使用python中的pandas库获取sql表,然后基于公共列合并两个数据帧
import pandas as pd
def rawjoin(query,connection=psycopg2.connect("dbname='mydb' user='qfsa' host='localhost' password='123' port=5433")):
query=("SELECT * FROM dzlog;")
firstTable= pd.read_csv('.\sources\sheetHeight_20151025_221244_000000000498768789.csv', delimiter=';', header=0)
secondTable =pd.read_sql(query,connection)
merged= pd.merge(firstTable, secondTable, on= 'ullid', how='inner')
return merged
结果是具有一个ullid列的连接数据帧
有什么不同的想法吗?我做了其他类型的连接,原始访问和FDW访问的结果相同,其他查询如下:
q7=("SELECT dzlog.color FROM dzlog,sheetheight WHERE dzlog.ullid = sheetheight.ullid;")
q8=("SELECT sheetheight.defectfound FROM dzlog, sheetheight WHERE dzlog.ullid = sheetheight.ullid;")
q9=("SELECT dzlog.color, sheetheight.defectfound FROM dzlog, sheetheight WHERE dzlog.ullid= sheetheight.ullid;")
我不知道你的第二个例子是什么,所以很难说。使用哪个图书馆?它是生成SQL还是在应用程序中执行连接(这几乎总是性能损失)?如果这导致一个SQL语句,那么该语句是什么
第一个查询返回该列两次,因为您要求它返回所有相关表中的所有列,并且两个表都有该列,联接条件强制该列相等
您可以编写只输出一次列的SQL语句,如下所示:
SELECT *
FROM dzlog
JOIN sheetheight
USING (ullid);
这看起来很像第二个示例中的代码,不是吗?是的,查询就是问题所在。您的查询是正确的。