ASP can execute your dynamic web pages quickly, but you can also make them execute faster by compressing code and database connections. This is a detailed article on how to streamline code and ASP features to get the fastest execution speed. For an impatient user, any delay between pressing a user button and the result appearing on their screen could mean they're browsing another site? If you have a commercial site, this could mean losing potential sales.
We don't have any way to control the user's bandwidth, but we do get the best performance by optimizing the ASP site. Most of the potential performance improvements are through system changes rather than code tightening. An inappropriate idea is to ask the system administrator to upgrade the system once you encounter system efficiency problems.
First, what factors might affect Asp's performance? Unfortunately, there are many factors? These are just some of them:
available bandwidth
The speed of the processor and other hardware on the server
Other programs running on the server (like those OpenGL screen savers!)
Database connection mode, connection pool, database system itself (for example, Oracle is better than Sql Server, Sql server is better than Access)
language used
Stored procedures are better than row-based SQL statements
Use compiled components instead of VB or JavaScript, good ASP programming experience, such as error handling, etc.
Some of the above factors may already be commonly noticed by developers with knowledge and experience of IIS, but others may be very complex issues for them. In this article, we will try to explain all the factors that affect the performance of Asp* and let us look at the main things that can be done in the few milliseconds we shave.
ASP script size
Is your script page (and other pages) longer than necessary? This is something that will reduce Asp* performance as soon as it is executed. ASP scripts are useful for getting information and formatting output, but scripts are also interpreted line by line, so the longer your script, the longer it will take to execute.
If your script is huge, what can you do to reduce its length? Here are some suggestions:
Can you convert them into server-side components, that is, into VB DLLs or into uncompiled components through advanced Windows programming languages or appropriate COM interface languages? And register them on the server side. A quick guide can be found at
Found at http://www.webdevelopersjournal.com/articles/activex_for_asp.html. Compiling a well-written ActiveX component can not only greatly improve performance, but also protect your software (scripts), especially when you publish your ASP site on a third-party host.
Because scripts are interpreted line by line, performance can be improved by eliminating redundant scripts or creating more efficient scripts. If you have hundreds of lines of code in a single ASP file, probably this way you can separate users, transactions, and data services nicely. In fact, if you do this, you may find some redundant code: if you need to output several tables, you can write a general function to output a table and just call it multiple times.
When talking about the size of Asp scripts, we have to mention the size of the included files. When you use an include file, the entire include file is loaded. When the include file is included, it is equivalent to writing that part of the code in the Asp file itself. Therefore, if you define a lot of common methods and definitions in a lengthy include file, understand that when you include the file, whether you want to use every method or definition in it, it will be included in the entire file. loaded. ASP caches all expansion code, which reduces search efficiency. In this case, the included file must be split into smaller, modular files. Also understand that include files are treated as separate page requests by the server, and using too many include files can affect download times.
<!-- #include file=Header.asp -->
<!-- #include file=Footer.asp -->
<SCRIPT language=vbscript runat=server>
Sub Main()
WriteHeader
WriteBody
WriteFooter
End Sub
SubWriteBody()
...
End Sub
Main?'Call procedure Main
</SCRIPT>
If your script is lengthy, use Response.IsClientConnected. This means that your server CPU can avoid waiting in a loop when the client is no longer connected to the server.
<%
'Check if the client is still connected
If Not Response.IsClientConnected Then
'Still connected, handler
Else
'disconnect
End If
%>
Interspersing ASP and HTML
Does everyone do this? When we output the table, we convert between ASP and HTML code, which is a bad habit. For example:
<HTML>
<BODY>
<%
Set MyConn = Server.CreateObject(ADODB.Connection)
MdbFilePath = Server.MapPath(sample.mdb)
MyConn.Open Driver={Microsoft Access Driver (*.mdb)}; DBQ= & MdbFilePath & ;
SQL_query = SELECT * FROM Friends
Set RS = MyConn.Execute(SQL_query)
WHILE NOT RS.EOF
%>
<LI><%=RS(Name)%>: <A HREF=>Homepage</A>
<%
RS.MoveNext
WEND
%>
</BODY>
</HTML>
Another common example is when using an IF statement:
<%
If Not Session(DBOpen) Then
%>
<H1>Database not connected</H1>
<%
Else
%>
<H1>Database open</H1>
<%
End If
%>
In these cases, script performance can be improved by writing the server-side script together and using Response.write to generate the HTML code. for example:
<%
If not Session (DBOpen) Then
Response.Write <H1>Database not connected</H1>
Else
Response.Write <H1>Database open</H1>
End If
%>
In the case of large scripts and many scripts, you will see performance improvements. Note that the use of <% tags is avoided as much as possible, so as to improve performance. ASP does not need to calculate the Ascii code of characters when executing the script.
Session status
There is no doubt that the ability to maintain a certain state through Session is a very powerful feature in ASP. However, it can affect your* performance. Obviously, the scalability of your site becomes another issue if you limit the use of Sessions. However, sessions consume server resources for each user.
What if you don't use session variables, or in fact you don't have to? Is using hidden form fields, saving data in the database, and query strings the trick? So you should disable Session state. You can disable session use with the following statement:
@EnableSessionState = False
In this way, ASP will no longer check session information.
If you have to rely on session state, you should avoid storing large amounts of data in the session object. The session in IIS will be maintained as long as the client's HTTP cookie is available, causing the memory occupied by the session to be occupied until the session terminates or times out. In this way, if many users use your program at the same time, your server resources may be exhausted.
Database access
Database access is a must-have? Accessing a database will drastically slow down your application, but obviously many sites would be worthless without a database. But by accessing the database through stored procedures instead of using embedded SQL statements, you can increase the potential performance. They also have good flexibility* by using stored procedures and ActiveX Data Objects (ADO). Whenever possible, output data from stored procedures.
Make sure your database has indexes, as this will directly improve the efficiency of your program. Also, try to run Update Statistics on your database server to help track your data distribution so that your database can modify query execution based on this information. Note that some databases, such as MS Access, are truly acceptable in enterprise-level programs? SQL Sever 7.0 or Oracle is a better bet.
Let SQL work as it was designed to count, join, sort, and group data. When you can write a query statement to do these things, don't do it yourself in other languages.
Here is the simplest syntax to count all columns:
SELECT count(*) FROM publishers WHERE state='NY'
If you count a specific column, you must use the group by statement to group that column, otherwise it won't work:
SELECT count(city),city FROM publishers GROUP BY city
Categorized data returned:
SELECT * FROM TableName WHERE FieldName>50 OR FieldName<100 ORDER BY FieldName2, Field Name3
Use Odbc or file DSN to connect to database? Use fast OLEDB Provider technology to connect to your database instead of using a DSN connection. No more asking your ISP (or database administrator/network administrator) to set up a system DSN for you, and no configuration changes when you move web files.
OLEDB sits between the ODBC layer and the application. ADO is an application on top of ODEDB in your ASP pages. Your ADO calls are first sent to OLEDB and then to the ODBC layer. However, you can connect directly to the OLEDB layer, and if you do so, you will see improvements in server-side performance. However, how to connect directly to OLEDB?
If you are using SQLServer 7, use the following connection code to connect to the database:
strConnString = DSN='';DRIVER={SQL SERVER}; & _
UID=myuid;PWD=mypwd; & _
DATABASE=MyDb;SERVER=MyServer;
The most important parameter is the DRIVER= part. If you want to bypass ODBC and connect to SQL Server by using OLEDB (which is a faster connection), use the following syntax:
strConnString =Provider=SQLOLEDB.1;Password=mypassword; & _
Persist Security Info=True;User ID=myuid; & _
Initial Catalog=mydbname; & _
Data Source=myserver;Connect Timeout=15
Is there something wrong?
Now you might be wondering: what is our point in this new connection method? Why not use the standard DSN-less/System DSN approach? Well, according to Wrox's test results in his book "ADO 2.0 Programmer's Reference", if you compare OLEDB connection with DSN or DSN-less connection method, you will find the following improvements:
*Can compare:
SQL Access
OLEDBDSNOLEDBDSN
Connection time: 18?82?Connection time: 62?99
Time to query 1,000 records: 29005400 Time to query 1,000 records: 100950
Note: This result can be found on pages 232 and 233 of Wrox's "ADO 2.0 Programmer's Reference" book. Time is measured in milliseconds, and query time to 1,000 records is calculated using a server-side cursor (there is not much difference in performance between OLEDB and DSN recordsets when using client-side cursors).
ASP decoding problem:
Validate user input on the client side whenever possible to reduce the number of HTTP round-trip requests. If your browser has the ability to support JavaScript or other scripts, use their power to free up more server resources.
The following VBScript runs in the client browser to validate user information before submitting it to your server:
<SCRIPT LANGUAGE=VBScript>
<!--
Sub btnEnter_OnClick
DimTheForm
Set TheForm = Document.MyForm
If IsNumeric(TheForm.Age.Value) Then
TheForm.submit
Else
Msgbox Please enter a numerical age.
End if
End Sub
//-->
</SCRIPT>
<FORMmethod=POST name=MyFormaction=myfile.asp>? Name: <INPUT typr=text name=Name>
Age: <INPUT type=text name=Age>
<INPUT type=button name=btnEntervalue=Enter>
</FORM>
Use local variables and avoid global variables. Local variables are accessed faster by the Asp script engine than global variables because the entire name domain does not need to be searched. Avoid changing array definitions. It is more efficient to simply allocate sufficient size during first initialization. In this case, you may waste some memory, but you gain the advantage of speed. This technique is obviously effective when the server is under heavy load.
If you need to reference an object that is not necessarily used, it is better to use the <OBJECT> tag instead of using the Server.CreateObject method. Using Server.CreateObject causes the object to be created immediately. On the contrary, the <OBJECT> tag does not create the object immediately. If you do not use the object after defining it with <object>, you will not waste resources.
For example: The following example uses the <OBJECT> tag to create an application-scope advertising wheel Ad Rotator object implementation.
example:
<OBJECT runat=server scope=Application id=MyAds progid=MSWC.AdRotator>
</OBJECT>
After storing the Ad Rotator object in Application, you can access the object in any program page using the following syntax
<%=MyAds.GetAdvertisement(CustomerAds.txt) %>
Turn on the 'Option Explicit' switch. In VB and VBScript, you can use variables without explicit declaration. But turning this option on can identify and define variables, which can write variables well and help improve performance. Undefined local variables are slower because the namespace must be searched to see if the variable exists before creating it. Get rid of it and make every variable clearly defined (define first, use later).
This is a good habit, it may trap typos, and it's faster.
Unless you really need to use it, don't use the Server.MapPath method. Use the real path if you know it. Using MapPath requires IIS to retrieve the current server path, which means a special request has to be sent to the server, which means reduced performance. Another method is to store the path in a local variable and use it when needed, so that the server does not need to look up multiple times.
Check how you are doing
You can measure your system performance through tools such as System Performance Monitor, netMon and PerfMon. To test web* performance, you can use WCAT (Web Capacity Analysis Tool). Using WCAT, you can test the ability of your IIS server and network configuration to respond to a wide variety of client requests, data or HTML pages. These test results can be used as guidance for optimizing your server and network configurations. WCAT is specifically designed to estimate the amount of customer workload that Internet Services in Windows 2000 (or Windows NT) and IIS can respond to.
(simulation). For more information, see the IIS Resource Kit. There is also a lengthy WCAT User's Guide at the MSDN Online Web sorkshop site with a download link. If you are serious about your Asp* capabilities, be sure to get this tool.
Strive to optimize application performance and your web application will run more smoothly. Don't affect server performance if you don't really need it.
asp uses stored procedures to implement data paging
1. Create table tiku_koushi
if exists (select * from dbo.sysobjects where id =
object_id(N'[dbo].[tiku_koushi]') and OBJECTPROPERTY
(id, N'IsUserTable') = 1)
drop table [dbo].[tiku_koushi]
GO
CREATE TABLE [dbo].[tiku_koushi] (
[id] [int] IDENTITY (1, 1) NOT NULL,
[title] [varchar] (250) COLLATE
Chinese_PRC_CI_AS NULL ,
[list2_id] [char] (10) COLLATE
Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
2. Stored procedure sp_c
CREATE proc sp_c
@tablename varchar(50),
@title varchar(250),
@list2_id varchar(50)
as
if @tablename='tiku_koushi'
select count(*) from tiku_koushi where title like '%'+@title+'%' and list2_id=@list2_id
GO
3. Stored procedure sp_search_tiku
CREATE PROCEDURE sp_search_tiku
@tablename varchar(50),
@title varchar(250),
@list2_id varchar(10),
@pagesize int,
@page int
AS
if @tablename='tiku_koushi'
begin
declare @ks int
declare @str varchar(200)
set @ks=@pagesize*(@page-1)
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[temp_table91]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
begin
select * into temp_table91 from tiku_koushi where
title like '%'+@title+'%' and list2_id=@list2_id order
by id desc
set rowcount @pagesize
set @str='select * from temp_table91 where id not in
(select top '+str(@ks)+' id from temp_table91)'
execute(@str)
drop table temp_table91
end
end
GO
4. search_koushi.asp
<!-- #include file=conn.asp -->
<%
line=6
if request(page)= then
page=1
else
page=request(page)
end if
if page<1 then
page=1
end if
title=trim(request(title))
list2_id=trim(request(list2_id))
set rs2=conn.execute(sp_c 'tiku_koushi','&title&','&list2_id&')
pagecount=CInt(rs2(0)/line)
if(CInt(rs2(0)) mod line)=0 then
pagecount=pagecount
else
pagecount=pagecount+1
end if
if CInt(page)>=pagecount then
page=CInt(pagecount)
end if
str=
str=str&page=&page&&title=&title&&list2_id=&list2_id
set rs=conn.execute
(sp_search_tiku 'tiku_koushi','&title&','&list2_id&','&line&','&CInt(page)&')
if rs.eof then
response.write no record
else
%>
<html>
<head>
<style type=text/css>
td{font-size:12px;}
a{text-decoration:none;}
</style>
<script language=javascript>
</script>
</head>
<body>
<table width=518 border=1 bordercolorlight=000000
bordercolordark=#ffffff
align=center cellpadding=0 cellspacing=0>
<!--DWLayoutTable-->
<tr bgcolor=#dfdfdf>
<td width=454 align=center height=24 valign=middle>Oral examination questions</td>
<td width=63 align=center valign=middle>Delete</td>
</tr>
<% do until rs.eof %>
<tr height=22>
<td valign=middle>·<a href=void(0)
onclick=window.open('editkoushi.asp?id=<%=rs(id)%>&page=<%=page%>&title=<%=title%>&list2_id=<%=list2_id%>','' ,'width=518
height=160 left=100')>
<%=rs(title)%></a></td>
<td align=center valign=middle>Delete</td>
</tr>
<%
rs.movenext
loop
%>
<tr align=left valign=middle bgcolor=efeff6
height=22>
<td colspan=2 style=padding-left:6px;>
<a href=search_koushi.asp?page=<%=1%>&title=<%=title%>&list2_id=<%=list2_id%>>Homepage</a> <a
href=search_koushi.asp?page=<%=page-1%>&title=<%=title%>&list2_id=<%=list2_id%>>Previous page</a> <a
href=search_koushi.asp?page=<%=page+1%>&title=<%=title%>&list2_id=<%=list2_id%>>Next page</a> <a
href=search_koushi.asp?page=<%=pagecount%>&title=<%=title%>&list2_id=<%=list2_id%>>Last page</a>
A total of <%=pagecount%> pages. The current page is: <%=page%>/<%=pagecount%> pages
There are <%=rs2(0)%> records in total</td>
</tr>
</table>
</body>
</html>
<%
rs2.close
set rs2=nothing
rs.close
set rs=nothing
end if
%>