After a few months of busy, I plant the latest e-commerce platform project finally on-line, during the problems encountered and solutions, can also be taken to communicate with you more.
Most of our projects are c#.net, using the evolving and popular EF framework, and using a Danish flagship CMS, DMS's. NET Web application Sitecore.
This article is based on the emphasis on the coding specifications and some coding techniques on the performance of the system. The non-canonical encoding method may have a negligible effect on the performance of a single method or module, but in large e-commerce projects, high concurrency scenarios can be seen everywhere, and improper coding may have a significant impact on the overall system performance and user experience.
As an e-commerce project, the most obvious performance impact of the module is to place orders and modify inventory. In high concurrency scenarios, the performance requirements of database access for these modules are very high, and a little performance waste may make the system perform poorly in use.
First, let's elaborate on the code specification for the EF framework. Here we can refer to MSDN's introduction to IQueryable <t> and IEnumerable<t>:
For methods that run on an in-memory collection (that is, those methods that extend IEnumerable < T>), the returned enumerable object captures the arguments passed to the method. When the object is enumerated, the logic of the query operator is used and the query results are returned.
In contrast, a method that extends IQueryable <t> does not implement any query behavior, but generates an expression tree that represents the query to execute. Query processing is handled by the source IQueryable<t> object.
IQueryable<t> generates a query expression tree and does not take the data out directly, but the extended method of the object provides us with a number of efficient accessibility features, such as any () that determines whether the data exists, Querying the Count () of the number of data, sum () of the statistics count attribute, and so on, EF will help us generate optimal SQL to reduce the performance loss when accessing the database. These methods can be implemented in a more clumsy way, but they are much less efficient.
In addition, the coding process should avoid using the ToList () method and GetByID () method, here can still refer to MSDN, but the author can be straightforward to describe: After using these two methods, the program will directly read the data from the database, and loaded into memory, Next we can manipulate these real data directly and use the methods that list<t> extends. However, this scenario can cause the following performance problems:
1. The non-business expression tree is used to query data directly, its data volume is too large, database access, disk IO, all need to consume a lot of time and space.
The 2.EF supported navigation properties will isolate the associated objects, and the huge data will have a larger branch, allowing memory and CPU to soar.
To avoid these problems, EF provides us with a proven approach:
1. The ToList () and Getdtobyid () methods are seldom used in the initial query.
2. During the query process, use the Select () and SelectMany () methods to select only the properties or objects that you want.
To see the performance loss caused by irregular coding, let's look at an example comparison:
1. Here is the code that selects only the properties that you want:
var productsku = UnitOfWork.ProductSku.Get (p = p.id = = Item. SKUID)
. Select (p = new {p.id, P.productid, p.product})
. FirstOrDefault ();
Time to submit your order:
2. Using the GetByID () method:
var productsku = UnitOfWork.ProductSku.GetByID (item. SKUID);
Time to submit your order:
The author of the computer is old, the data in the i5 8G RAM computer only need 200ms of time, on the more powerful server time is shorter.
In contrast, we can see that only one method has been modified, and the time of the request of the program is nearly one times the error! If our code is filled with this lazy nonstandard notation, in high concurrency scenarios, the system will be dragged very slowly, even the program error.
Next, we say aside from the EF framework approach, good at SQL programming of the park friends, may disdain EF offers a variety of solutions, directly write SQL, using ADO is not faster? It is true that running SQL directly will make the program faster and the speed of ADO is recognized by everyone. EF also supports the way you write SQL directly:
var productsku = dbcontext.database.sqlquery<productsku> (SQLSTR);
If SQL programming is strong, the author is very supportive of this programming scenario. But the EF framework also has its natural advantages:
1.EF Framework allows more novice software practitioners to learn and write programs faster
2.EF provides a comprehensive extension approach to help software practitioners achieve a variety of functions
3. The EF C # code written by spec is not much slower than native SQL
This reminds me of the contradiction between C#.net and Java programmer ^_^. I have written about Java for some time because of chance coincidence.
The design concepts of the two are really different:
1.c#.net allows beginners to get started faster, providing more class libraries and methods, and its excellent IDE gives programmers the hassle and effort. And c#.net is open source.
2.Java requires programmers to do more thinking, to configure their own environment variables, their own knocking command line, so that programmers in the process of thinking to deepen the understanding of computer principles, operating systems and software engineering.
No doubt: Both are the best contemporary high-level programming languages (Php,python,javascript and other peers do not spray ^_^) and spend time arguing who is the best language, rather than learning it.
The above is the introduction of the performance optimization of the basic article, the follow-up will bring you the database level of optimization experience and understanding.
C # Large e-Commerce project performance optimization (i.)