Oracle Query或语句问题以及性能调优

时间:2014-10-27 19:47:26

标签: sql oracle oracle11g

我遇到问题让我的查询正确执行。我有一张7000万行的表。我正在尝试在ip表中搜索特定日期之间分配给客户的不同ip。这些日期由解除分配日期控制。

因此,

我正在尝试在解除分配日期为空或在1月1日到31日之间搜索分配给客户的ips。我的查询运行速度不是很快,也是第一次运行它或者在deallocation_date为空的同一行上运行它,它返回每一行。

这是我查询我运行它的第一种方式,它返回的每个客户不仅仅是我正在搜索的客户,还需要1分钟才能开始执行。

select distinct e.ip_address, 
a.customer_name,
c.vm_id, 
d.allocation_date, 
d.deallocation_date
from customers a, 
vm_groups b, 
vms c, 
vm_ip_address_histories d, 
ip_addresses e 
where a.customer_id=30
and a.customer_id = b.customer_id
and b.vm_group_id = c.vm_group_id
and c.vm_id = d.vm_id
and d.ip_address_id = e.ip_address_id
and d.deallocation_date is null or trunc(d.deallocation_Date) between to_date('1-oct-14') and to_date('31-oct-14')
/

我跑的第二种方式,但15分钟后还没有回来

select distinct e.ip_address, 
a.customer_name,
c.vm_id, 
d.allocation_date, 
d.deallocation_date
from customers a, 
vm_groups b, 
vms c, 
vm_ip_address_histories d, 
ip_addresses e 
where a.customer_id=30
and a.customer_id = b.customer_id
and b.vm_group_id = c.vm_group_id
and c.vm_id = d.vm_id
and d.ip_address_id = e.ip_address_id
and d.deallocation_date is null 
or trunc(d.deallocation_Date) between to_date('1-oct-14') and to_date('31-oct-14')
/

这种方式并没有解决它,我认为它确实如此,但它没有返回带有释放日期的值。

select distinct e.ip_address, 
a.customer_name,
c.vm_id, 
d.allocation_date, 
d.deallocation_date
from customers a, 
vm_groups b, 
vms c, 
vm_ip_address_histories d, 
ip_addresses e 
where a.customer_id=30
and a.customer_id = b.customer_id
and b.vm_group_id = c.vm_group_id
and c.vm_id = d.vm_id
and d.ip_address_id = e.ip_address_id
and (d.deallocation_date is null or trunc(d.deallocation_Date) between to_date('1-oct-14') and to_date('31-oct-14'))
/

我也试过了,它只返回了null的值。:

select distinct e.ip_address, 
a.customer_name,
c.vm_id, 
d.allocation_date, 
d.deallocation_date
from customers a, 
vm_groups b, 
vms c, 
vm_ip_address_histories d, 
ip_addresses e 
where a.customer_id=30
and a.customer_id = b.customer_id
and b.vm_group_id = c.vm_group_id
and c.vm_id = d.vm_id
and d.ip_address_id = e.ip_address_id
and exists (select * from vm_ip_address_histories 
         where d.deallocation_date is null 
         or trunc(d.deallocation_Date) between to_date('1-oct-14') and last_day('1-oct-14'))
/

1 个答案:

答案 0 :(得分:0)

select distinct e.ip_address, 
a.customer_name,
c.vm_id, 
d.allocation_date, 
d.deallocation_date
from customers a, 
vm_groups b, 
vms c, 
vm_ip_address_histories d, 
ip_addresses e 
where a.customer_id=30
and a.customer_id = b.customer_id
and b.vm_group_id = c.vm_group_id
and c.vm_id = d.vm_id
and d.ip_address_id = e.ip_address_id
and (d.deallocation_date is null or d.deallocation_date between to_date('01-10-2014', 'DD-MM-YYYY') and to_date('31-10-2014 23:59:59', 'DD-MM-YYYY HH24:MI:SS'))
/