简体   繁体   English

需要优化这个 Oracle SQL 查询

[英]Need to optimize this Oracle SQL query

My query takes too much time to fetch data.我的查询需要太多时间来获取数据。 Each table (ap1, ap2, ap4) has 1626 records.每个表(ap1、ap2、ap4)有 1626 条记录。 Please help me to optimize the query请帮我优化查询

SELECT *
FROM ap1 w1,
     ap2 w2, 
     ap4 w4
WHERE w4.first_name = '2'
  AND w4.id = '303'
  AND w4.reference like '%AXS%'
  AND w1.first_name = w4.first_name
  AND w2.first_name = w1.first_name
  AND w1.id = w4.id
  AND w2.id = w4.id
  AND w1.number = w4.number
  AND w2.number = w4.number;

Thanks谢谢

The proper way to write the query is:编写查询的正确方法是:

SELECT *
FROM ap1 w1 JOIN
     ap2 w2
     ON w2.first_name = w1.first_name AND
        w2.id = w4.id JOIN
     ap4 w4
     ON w1.first_name = w4.first_name AND
        w1.id = w4.id AND
        w1.number = w4.number
WHERE w4.first_name = '2' AND
      w4.id = '303' AND
      w4.reference like '%AXS%';

This makes it clear that you want an index on w4(id, first_name, reference, id, number) .这清楚地表明您想要w4(id, first_name, reference, id, number)上的索引。 And then on w1(id, first_name, number) and on w2(id, first_name)`.然后在w1(id, first_name, number)和 w2(id, first_name)`上。

However, this assumes that the join conditions are correct.但是,这假定联接条件是正确的。 If you query is performing so poorly on small numbers of rows, then they might be wrong.如果您的查询在少量行上的表现如此糟糕,那么它们可能是错误的。 I would suggest that you build up the query one table at a time to be sure the logic is correct.我建议您一次构建一张表以确保逻辑正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM