What optimization tutorials can be done to improve website access speed

1. Server optimization

  l Windows series:

  64-bit Win2008r2 + Hpyer-V + load balancing + IIS7.5

  64-bit Win2003 + virtual machine + load balancing + IIS6

  prohibits the server from accessing the external network. Turn off unnecessary ports.

  Remove all permissions on each disk, leaving only administrators group and system full control permissions.


  The IIS7.5 logo uses ApplicationPoolIdentity, and the user for the directory permission is: IIS AppPool site name

  IIS6 needs to create an independent user for each site, and set permissions for the IIS user separately.

  IIS disables logging.

  Second, database optimization

  l 64-bit MSSQL2008:

  use less triggers/cursors/foreign keys. Multipurpose stored procedures and views. Appropriate use of indexes will greatly improve the query speed, and try not to exceed 3 indexes for each table.

  Regularly check the deadlocked process and waiting process, and clear it, and correct the source of the deadlock in time.

  Complex statistical reports need to be run with a plan.

  To query the number of records, use count(0) instead of count(*).

  Do not use * for table fields when selecting. * Querying all fields is slow.

  If the field is an index column, you can use UNION ALL instead of OR, and take the index line query.

  Large recordsets use paging queries.

  Try not to use TEXT/NTEXT types, use VARCHAR(MAX)/NVARCHAR(MAX).

  Try not to use temporary tables, use the Table table type or inline views.

  三、  后端优化

  l  多使用DbDataReader,尽量不要使用DataTable/DataSet读取数据。

  l  for (int i = 0; i < dt.Rows.Count; i++) 应该这么写:

  for (int i = 0, len = dt.Rows.Count; i < len; i++)或

  foreach (DataRow info in dt.Rows)推荐这种写法

  l  少用try catch,用到try catch时要和 finally一起使用。

  l  多使用using如:

  using (DbDataReader dr = Data.GetDbDataReader(strSql)) { while (dr.Read()) { }; }

  l  大字符串操作请使用StringBuilder 少使用string,字符串比较用Compare,字符串相加用Concat,大字符串相加用StringBuilder Append。

  l  变量要在先定义在使用,不要在循环内定义变量。如:

  错误的写法是:for (int i = 1; i < 10; i++) { string s = i.ToString(); }

  正确写法:string s = string.Empty; for (int i = 1; i < 10; i++) { s = i.ToString(); }

  这里还有一个地方要注意的:在字符串相加的时候,如果有int类型的要先转成string类型在相加,减少不必要的装箱拆箱操作。


l  如果你使用的是HTML控件,需要禁用<%@ Page EnableViewState="false" AutoEventWireup="false" EnableSessionState="false",web.config <pages enableViewState="false" enableSessionState="false" />

  l  使用foreach替代for

  l  操作数据库要使用存贮过程/视图。

  l  多使用CACHE对数据缓存。这才是最关键的。NET提供:HttpContext.Current.Cache/HttpRuntime.Cache,共享缓存有velocity/ memcached

  l  可使用<%@ OutputCache Duration="60" 缓存页面,可使用Response.BufferOutput = true;/ <%@ Page Buffer="true" 输出缓冲。

  l  可将站点生成静态面页,好处多多。

  l  可使用URL重写成伪静态,提供rss/baidu-sitemap/google-sitemap服务,有利于搜索引擎收录。

  l  Ajax调用页面要使用.ashx一般处理程序,速度要比.aspx文件要快。Ajax请求要使用POST不要使用GET。

  l  发布站点时DLL要Release版本,不要用Debug版本。

  l  IIS需要使用集成模式,不要使用经典模式。

  l  Web.config 加 <customErrors mode="On" /> 关闭错误提示。

  l  Web.config 加 <compilation debug="false" /> 关闭调试模式。

  l  使用Server.Transfer替换Response.Redirect

  l  多使用泛型集合操作,少用ArrayList。

  四、  前端优化

  l  最好不使用NET控件,用HTML控件,页面使用CSS+DIV布局。

  l  使用JSON + AJAX进行数据交互。

  l  要学会使用jQuery提高开发速度。

  l  尽量少用<img标记,改用background样式控制背景图片。这样做的好处是方便扩展多皮肤。

  l  将CSS background背景图片多张合并成一张图片,保证合并后的图片大小不要超过50K。可将JS文件合并在一个JS文件中,然后使用closure_packer_yui_compiler进行压缩,这样做的好处是可减少客户端连接数。

  l  JS代码段不要放在HTML页面,应该放在独立的JS文件里面,好处是JS文件可以CDN/缓存。

  l  可将CSS文件放到<head></head>之间,JS文件或代码放到</body>之前,让页面先显示在执行JS。

  l  可将网站的图片/CSS/JS/资料/资源放到独立的站点,做下CDN加速,二级域名会有COOKIES,最好使用一级域名。

  l  A标记和IMG标记需要加上title/alt,<head>标签内需要加title/keywords/description/rss/sitemap优化,有利于搜索引擎收录。

  l  页面可启用gzip压缩。

  l  安装FF的插件“YSlow/Page Speed”。

  五、  其它优化

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325995725&siteId=291194637