Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 将一个表与下一个表的最新版本联接_Sql_Oracle - Fatal编程技术网

Sql 将一个表与下一个表的最新版本联接

Sql 将一个表与下一个表的最新版本联接,sql,oracle,Sql,Oracle,您是否可以通过替换子查询来建议以下查询的高效性能版本 SELECT G.POSTINGDATE,G.CLIENTINSTRUMENTID,I.ID, I.INVENTORYID,I.VERSIONID FROM ABC G INNER JOIN XYZ i ON G.CLIENTINSTRUMENTID=I.CLIENTINSTRUMENTID AND ((G.POSTINGDATE BETWEEN I.POSTINGDATE AND I.POSTINGENDDATE) OR (G.POSTIN

您是否可以通过替换子查询来建议以下查询的高效性能版本

SELECT G.POSTINGDATE,G.CLIENTINSTRUMENTID,I.ID,
I.INVENTORYID,I.VERSIONID FROM ABC G
INNER JOIN XYZ i
ON G.CLIENTINSTRUMENTID=I.CLIENTINSTRUMENTID AND
((G.POSTINGDATE BETWEEN I.POSTINGDATE AND I.POSTINGENDDATE)
OR (G.POSTINGDATE > I.POSTINGDATE AND I.ID = ( 
    select MAX(ii.ID) from XYZ ii
      where ii.CLIENTINSTRUMENTID= i.CLIENTINSTRUMENTID  
      group by ii.clientinstrumentid )))
WHERE G.INVENTORYPOSITIONID IS NULL OR G.INVENTORYID IS NULL
GROUP BY G.POSTINGDATE,G.CLIENTINSTRUMENTID,I.ID,I.INVENTORYID,
I.VERSIONID;
此子查询获取所述clientinstrumentid的最新版本

I.ID = ( select MAX(ii.ID) from XYZ ii 
           where ii.CLIENTINSTRUMENTID = i.CLIENTINSTRUMENTID 
           group by ii.clientinstrumentid )

我决定编写一个CTE来预处理子查询,以预计算
max(id)

即使相关子查询不再存在,其性能改进仍有待观察。您需要尝试一下,看看是否更快:

with xyz2 (clientinstrumentid, max_id, id, inventoryid, versionid, 
           postingdate, postingenddate) as (
  select ii.clientinstrumentid. ii.max_id, i.id, i.inventoryid, i.versionid,
         i.postingdate, i.postingenddate
    from xyz i
    join (
    select clientinstrumentid, max(id) as max_id
      from xyz 
      group by clientinstrumentid 
    ) ii on ii.clientinstrumentid = i.clientinstrumentid
)
select g.postingdate, g.clientinstrumentid, i.id, i.inventoryid, i.versionid
  from abc g
  join xyz2 i on g.clientinstrumentid = i.clientinstrumentid 
   and ( (g.postingdate between i.postingdate and i.postingenddate)
      or (g.postingdate > i.postingdate and i.id = i.max_id)
   )
  where g.inventorypositionid is null
     or g.inventoryid is null
  group by g.postingdate, g.clientinstrumentid,
        i.id, i.inventoryid, i.versionid
这是一个“相关”查询,并且相关查询的速度很慢,特别是当您对许多行运行它们时。或者,您可以使用不相关的CTE。