0

假设我有下面的 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
4

2 回答 2

4

我会尝试几件事:

  1. 将您的 HAVING 子句放入内部查询中的 WHERE
  2. 在内部查询中自行选择 first_name 和 last_name
  3. 在外部查询中按名字、姓氏分组(为什么叫价格?)
  4. 做名的串联 || ' ' || 外部查询中的姓氏

还要确保有以下索引:

  • customer.project_id
  • customer.creation_userid
  • 用户.userid
于 2012-09-21T10:26:12.057 回答
1

最好的增长通常来自创建索引。您询问内部连接。没有内部连接会很快吗?那么你可能需要一个关于 user.userid 的索引。

于 2012-09-21T10:32:26.830 回答