SQL优化:内部联接

时间:2012-09-21 10:16:18

标签: sql oracle query-optimization

假设我有下面的SQL

如何优化查询的性能?

select product, address, quantity, 
    price, create_date, sum(no_of_photo) as no_of_photo
from
(
    SELECT customer.PROJECT_ID as project_id ,
        customer.ORDER_TYPE as order_type,  
        customer.product_name as product, 
        customer.address_name as address ,    
        customer.quantity_name as quantity,
        user.first_name || ' ' || user.last_name AS price,
        customer.create_date, customer.no_of_photo 
    FROM customer 
    INNER JOIN user 
        ON customer.creation_userid = user.userid
) master_list 
group by project_id, order_type , 
    product, address, quantity, price, 
    create_date 
having project_id = 123456 
order by product, address, quantity, price, create_date

2 个答案:

答案 0 :(得分:4)

我会尝试一些事情:

  1. 将您的HAVING子句放入内部查询的WHERE
  2. 在内部查询中选择first_name和last_name
  3. 按外部查询中的first_name,last_name分组(为什么称为价格?)
  4. 执行first_name ||的连接''||外部查询中的last_name
  5. 还要确保有以下索引:

    • customer.project_id
    • customer.creation_userid
    • user.userid

答案 1 :(得分:1)

通常通过创建索引来实现最佳增长。你询问内部联接。没有内部联接,它会快吗?那么你可能需要user.userid上的索引。