简体   繁体   English

如何提高SQL查询性能

[英]How To improve a sql query performance

I Have a view that return to me the customers and sum of their payment and its work but it take 8 second and when i remove the computed column part from the query it didn't take any time, and i tried to solve this by creating a non-clustered index on tables but its also 8 second, so I'm asking about if there is any method to improve the performance, although i have to use this technique. 我有一种观点,可以向我返回客户及其支付的款项及其工作的sum ,但是这需要8秒钟,当我从查询中删除计算列部分时,它并没有花费任何时间,因此我尝试通过创建来解决此问题表上的非聚集索引,但它也是8秒,所以我在问是否有任何方法可以提高性能,尽管我必须使用此技术。

my code is 我的代码是

SELECT ID
    ,NAME
    ,Total = (
        SELECT Sum(Value)
        FROM TRANSACTION
        WHERE Employee.ID = employee_ID
        )
FROM Employee;

Thanks in advance 提前致谢

You can try to use a join with group and see if that is faster: 您可以尝试对组使用联接,看看是否更快:

 select employee.id, employee.name, sum(value)
 from employee 
 join transaction where transaction.employee_ID = employee.id
 group by employee.id, employee.name

Also - try to avoid to give names that correspond to reserved words - like TRANSACTION - see https://dev.mysql.com/doc/refman/5.5/en/keywords.html 另外-尽量避免给出与保留字相对应的名称-像TRANSACTION参见https://dev.mysql.com/doc/refman/5.5/zh-CN/keywords.html

To hasten up you might want to consider a combined index on Emlpoyee(id,name) AND an index on Transaction.Employee_ID - both are neede to make the joins and grouping for this query fast. 为了加快处理速度,您可能需要考虑Emlpoyee(id,name)上的组合索引和Transaction.Employee_ID上的索引-两者都需要快速建立此查询的联接和分组。 Check if transaction.employee_ID has a FK_Constraint to Employee.ID - that might help as well. 检查transaction.employee_ID是否对Employee.ID具有FK_Constraint这可能也有帮助。

You could use a join in the subselect with the sum 您可以在子选择中使用与总和的联接

select e.id, e.name, t.total 
from employee  e
join (
  select employee_ID, sum(value) as total
  from TRANSACTION
  group by employee_ID
) t on t.employee_ID = e.id 

For this query: 对于此查询:

SELECT ID, NAME,
       (SELECT Sum(t.Value)
        FROM TRANSACTION t
        WHERE e.Employee.ID = t.employee_ID
       ) as Total
FROM Employee e;

You want an index on TRANSACTION(employee_ID, Value) . 您需要索引TRANSACTION(employee_ID, Value) That should actually give you optimal performance. 这实际上应该为您提供最佳性能。

You can write this as a join -- but a left join : 您可以将其写为join -但可以left join

SELECT e.ID, e.NAME, SUM(t.Value) as Total
FROM Employee e JOIN
     TRANSACTION t
     ON e.Employee.ID = t.employee_ID
GROUP BY e.ID, e.NAME;

However, the overall GROUP BY is usually less efficient than the "sub group by" in MySQL (and often in other databases as well). 但是,总体上, GROUP BY在MySQL中(通常在其他数据库中)通常不如“ sub group by”有效。

Try an indexed view on the total 尝试对总索引视图

CREATE VIEW totalView
WITH SCHEMABINDING
AS
    select employee_ID, sum(value) as total
      from TRANSACTION
      group by employee_ID

CREATE UNIQUE CLUSTERED INDEX PK_totalView ON totalView
(
    employee_id,total
);

Then join to that 然后加入那个

select employee.id, employee.name, coalesce(t.total,0)
 from employee 
 left outer join totalView t where t.employee_ID = employee.id

Adjust the join type according to your need. 根据需要调整连接类型。

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

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