Small changes, big effect: Remember to troubleshoot and solve a high CPU load

Source: Internet
Author: User

Problem cause: I received an email from my O & M colleagues saying that the CPU usage has increased significantly since the last website update (as shown in the red box), but the number of website renewals has not increased.

Troubleshooting: Why does the CPU usage increase? It must have been a page with a large access volume that consumes CPU, such as file read/write and some complex operations in the memory. Based on the last website update, the problem is locked on the housing details page. It mainly involves reading XML files (more than 2 MB at the maximum) into the able. Each time you open the page, you can judge whether there is any data in the datatable based on the values of the two columns in the datatable, what is more serious is that the read files are not cached, resulting in frequent reading of files and keeping the CPU busy.

Once the problem is found, it is much easier to modify it, and it is better to add a cache. below is the modified pseudoCode. After the modification, the website is updated, and the CPU usage is restored to the normal value (the Part following the red box ).

Simple code example

     ///     <Summary>  
/// Obtain a list of real estate experts
/// </Summary>
/// <Returns> List of real estate experts </Returns>
Public Static Datatable getagentxml ()
{
Datatable dt = Null ;
String Cachename = " Agentall_zyzj " ;
If (Cachemanager. iscached (cachename ))
{
Object O = cachemanager. getcache (cachename );
If (O! = Null )
{
Dt = o As Datatable;
}
}
If (Dt = Null )
{
// Read XML file to datatable
Dt = getagentxmlfromfile ();
If (DT! = Null )
{
Try
{
If (Dt. Rows. Count> 0 )
{
DT. primarykey = New Datacolumn [] {DT. Columns [ " Newcode " ], DT. Columns [ " Agentid " ]};
}
}
Catch
{}
Cachemanager. insertcache (cachename, DT, system. datetime. Now. addminutes ( 60 ));
}
}
Return DT;
}
/// <Summary>
/// Determines whether it is a real estate expert
/// </Summary>
/// <Returns> Is it a real estate expert? </Returns>
Public Static Bool Checkzyzj ( Long Agentid, Long Newcode)
{
Datatable agentdt = New Datatable ();
Bool Iszyzj = False ;
Try
{
Agentdt = getagentxml ();
If (Agentdt! = Null & Amp; agentdt. Rows. Count & gt; 0 )
{
Datarow [] DRS = agentdt. Select ( " Agentid = " + Agentid + " And newcode = " + Newcode );
If (DRS! =Null & Amp; Drs. Length & gt; 0 )
{
Iszyzj = True ;
}
}
}
Catch (Exception ex)
{
Iszyzj = False ;
}
Return Iszyzj;
}

Note: This optimization also makes a small adjustment while adding cache. After reading data from the XML file to the datatable, it sets the primary key "DT" for the datatable. primarykey = new datacolumn [] {DT. columns ["newcode"], DT. columns ["agentid"]} ", setting a primary key for the datatable can greatly improve the SELECT query efficiency (this is somewhat similar to the primary key of the table in the database ). Because we modified two places this time, it is difficult to use data description to increase the efficiency of the primary key. However, a complex report has been generated before, and a large number of queries need to be made to the able in the memory. After simply adding the primary key, the efficiency is improved by seven or eight times.

The Caching process is crucial, and the hourly cache of website traffic has little impact. Once the traffic comes up, simple logic also needs to be considered. on weekdays, there will be more ideas to predict the performance, reduce the chance of problems after going online.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.