简体   繁体   English

WCF OData 性能改进

[英]WCF OData performance improvement

I have following query which is taking a lot of time -我有以下查询需要很多时间 -

var allEmployees = (from e in context.Employees.Expand("Payroll/Customer")
.Expand("HR")
.Expand("Payroll1")                                                 
where e.IsActive
&& e.Payroll.EmployeeId== this.CurrentEmployee.EmployeeId
orderby e.Name
select e).ToArray();

Questions -问题 -

  1. How can I improve the performance?如何提高性能?
  2. Which part of the query taking too much time, It can be Expand clause, where, orderby, select.查询哪部分时间过长,可以是Expand子句、where、orderby、select。
  3. At last I am converting the result in Array.最后我将结果转换为数组。 Can it impact the performance?它会影响性能吗?
  4. Do I have other alternatives, like Parallel.For, PLINQ etc. I am not sure about it.我是否有其他选择,如 Parallel.For、PLINQ 等。我不确定。

Please suggest请建议

The most important part is your Expand最重要的部分是您的展开

No, It's not possible to LINQ over WCF data services with Parallel LINQ .不,不可能使用 Parallel LINQ 通过 WCF 数据服务进行 LINQ。

The best way is to index over foreign keys of relations between customers and human resources and payroll tables最好的方法是索引客户与人力资源和工资表之间关系的外键

ToArray wont takes too long ToArray 不会花费太长时间

Use SQL profiler and SQL execution plan to improve your performance and find bottle necks使用 SQL 分析器和 SQL 执行计划来提高性能并找到瓶颈

Consider creating method on your service layer and use Select method to return only columns you need考虑在您的服务层上创建方法并使用 Select 方法仅返回您需要的列

Another best practice is using JSON format instead of using XML Serializer.另一个最佳实践是使用 JSON 格式而不是使用 XML Serializer。 It will decrease your response size up to 70%.它会将您的响应大小减少多达 70%。

Let me know if you want more information.如果您需要更多信息,请告诉我。

good luck祝你好运

I know the question has been asked a few years ago, but if my advices can help someone, here they are:我知道几年前有人问过这个问题,但是如果我的建议可以帮助某人,那么它们是:

  1. How can I improve the performance?如何提高性能? - Add Indexes for the columns you search or order by frequently. - 为您经常搜索或排序的列添加索引。 An index is ordering your column to than optimize query.索引正在对您的列进行排序,而不是优化查询。 You can do this in your DB https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-nonclustered-indexes or if you are using EF Core https://docs.microsoft.com/en-us/ef/core/modeling/indexes .您可以在您的数据库https://docs.microsoft.com/en-us/sql/relational-databases/indexes/create-nonclustered-indexes 中执行此操作,或者如果您使用的是 EF Core https://docs.microsoft.com /en-us/ef/core/modeling/indexes So you can add indexes for Name and IsActive in Employees.因此,您可以为员工中的 Name 和 IsActive 添加索引。
  2. SQL Joins (.Expand) can slow down your performance, you should avoid them if possible. SQL 连接 (.Expand) 会降低您的性能,您应该尽可能避免它们。 You can do so by flattening down the tables to avoid one-2-many/many-2-many relationships您可以通过展平表格来避免一对多/多对二的关系
  3. I'd also recommend to debug this using Performance Profiler in Visual Studio (Debug -> Performance Profiler).我还建议使用 Visual Studio 中的性能探查器(调试 -> 性能探查器)来调试它。 You can launch it and query the DB and it will than show you the output documents with all queries.您可以启动它并查询数据库,然后它会显示所有查询的输出文档。 And than you can check this query via MSSQL Management Studio (if you are using MSSQL of course).然后您可以通过 MSSQL Management Studio 检查此查询(当然,如果您使用的是 MSSQL)。 For that you need to enable Live Query Statistics and see which instruction takes the longest.为此,您需要启用 Live Query Statistics 并查看哪条指令花费的时间最长。

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

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