优化一个4表相关的oracle查询

发布于 2024-09-12 17:59:30 字数 1068 浏览 5 评论 0原文

我需要优化以下查询,但不知道如何优化。

select distinct  v.codvia,
    v.codproine,
    v.codmunine,
    tv.SIMBOLO as SIMBOLO_TIPO_VIA,
    tv.NOMBRE as TIPO_VIA,
    c.nombrevia as NOMBRE_VIA,
    v.cp,
    m.nombre as NOMBRE_MUNICIPIO ,
    pr.nombre as NOMBRE_PROVINCIA   
    from tdinumvias v, tdimunicipio m, tdivia c, cditipovia tv, tdiprovincia pr
where (pr.codine = v.codproine) and  
          (m.codproine = v.codproine and m.codine = v.codmunine) and
      (c.codproine = v.codproine and c.codmunine = v.codmunine and
       c.codvia=v.codvia and tv.idtipovia=c.idtipovia) 

为以下对象创建了索引: v.鳕鱼肽, v.科德维亚, v.科德穆宁, c.考德穆宁, 可待因原, 米可待因, c.鳕鱼肽, 伊迪波维亚 和 c.idtipovia

在它的对应表中,但性能仍然很糟糕。


从评论中添加: 桌子尺寸为 11M in tdinumvias, tdimunicipio 10K, 970K 的 tdivia 其他的只有几行。

它需要不到一秒的时间,我想知道是否有可能将其达到 100 - 200 英里。

更新:

最后我们创建了一个新的中间表,其中 cp、codproine 和 codmunie 预先计算并编译为视图,然后使第一个查询从该视图中获取数据,这样可以得到大约 300-400 条查询米利斯.它没有我们想要的那么好,但没关系。

谢谢

I need to optimize the following query but 'm not able to wonder how.

select distinct  v.codvia,
    v.codproine,
    v.codmunine,
    tv.SIMBOLO as SIMBOLO_TIPO_VIA,
    tv.NOMBRE as TIPO_VIA,
    c.nombrevia as NOMBRE_VIA,
    v.cp,
    m.nombre as NOMBRE_MUNICIPIO ,
    pr.nombre as NOMBRE_PROVINCIA   
    from tdinumvias v, tdimunicipio m, tdivia c, cditipovia tv, tdiprovincia pr
where (pr.codine = v.codproine) and  
          (m.codproine = v.codproine and m.codine = v.codmunine) and
      (c.codproine = v.codproine and c.codmunine = v.codmunine and
       c.codvia=v.codvia and tv.idtipovia=c.idtipovia) 

there are indexes created for:
v.codproine,
v.codvia,
v.codmunine,
c.codmunine,
pr.codine,
m.codine,
c.codproine,
v.idtipovia
and c.idtipovia

In it's correspondent tables but the performance is still really bad.


Added from comments :
Table sizes are
11M in tdinumvias,
10K in tdimunicipio,
970K in tdivia
the others only have a few rows.

It takes a bit less than a second and i was wondering if it was possible to get it to 100 - 200 milis.

Update:

Finally we have created a new intermediate table with cp, codproine and codmunie precalculated and compiled as a view, then make the first query get data from this view, this gets the query in about 300-400 Millis . it's not as good as we wanted but it's okay.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏林苍穹下 2024-09-19 17:59:30

我使用 ANSI-92 语法重新编写,除了可读性之外,它不会提供任何性能优势:

SELECT DISTINCT v.codvia,
       v.codproine,
       v.codmunine,
       tv.SIMBOLO as SIMBOLO_TIPO_VIA,
       tv.NOMBRE as TIPO_VIA,
       c.nombrevia as NOMBRE_VIA,
       v.cp,
       m.nombre as NOMBRE_MUNICIPIO ,
       pr.nombre as NOMBRE_PROVINCIA   
  FROM tdinumvias v
  JOIN tdimunicipio m ON m.codproine = v.codproine 
                     AND m.codine = v.codmunine
  JOIN tdivia c ON c.codproine = v.codproine 
               AND c.codmunine = v.codmunine
               AND c.codvia = v.codvia
  JOIN cditipovia tv ON tv.idtipovia = c.idtipovia
  JOIN tdiprovincia pr ON pr.codine = v.codproine

检查您的 JOIN - 它们正是创建 DISTINCT 需求的原因。至少需要将一个 JOIN 转换为 IN 或 EXISTS 子句以消除重复项。

I re-wrote using ANSI-92 syntax, which won't provide any performance benefit aside from readability:

SELECT DISTINCT v.codvia,
       v.codproine,
       v.codmunine,
       tv.SIMBOLO as SIMBOLO_TIPO_VIA,
       tv.NOMBRE as TIPO_VIA,
       c.nombrevia as NOMBRE_VIA,
       v.cp,
       m.nombre as NOMBRE_MUNICIPIO ,
       pr.nombre as NOMBRE_PROVINCIA   
  FROM tdinumvias v
  JOIN tdimunicipio m ON m.codproine = v.codproine 
                     AND m.codine = v.codmunine
  JOIN tdivia c ON c.codproine = v.codproine 
               AND c.codmunine = v.codmunine
               AND c.codvia = v.codvia
  JOIN cditipovia tv ON tv.idtipovia = c.idtipovia
  JOIN tdiprovincia pr ON pr.codine = v.codproine

Review your JOINs - they are what is creating the need for the DISTINCT. At least one JOIN needs to be converted into an IN or EXISTS clause to get rid of the duplicates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文