当前位置:首页 > ASP > 正文内容

如何提高ASP数据查询执行效率

canca16年前 (2010-02-24)ASP433

1 明确查询的字段名称
2 使用rs(0)比rs(“name”)更快
3 使用记录集rs值前,将其赋值给变量
4 [TEST] 现有10W条数据,Access数据库保存
通过正常提取 | 通过存储过程提取| 使用GetRows()方法提取:

1 明确查询的字段名称

Select * from [data_table]
即从数据库data_table种抽取所有字段的记录值
Select * 语句的执行效率非常低,因为执行这样的语句时执行了两次查询, 先查询系统表来确定名称和数据类型.然后再查数据
所以精良减少使用select * 语句,而使用明确的字段名称,如:
Select name,pwd from [data_table]

 2 使用rs(0)比rs(“name”)更快

记录集rs()里面可以写字段名,或字段索引号.比如
Rs(0)对应rs(“name”)
Rs(1)对应rs(“pwd”)
已证明用索引数访问记录集要比字段名快出几倍,按字符串查询要比按整数查询花去的更多的时间和系统资源

 3 使用记录集rs值前,将其赋值给变量

<%
Set rs=conn.execute(“select cname,cpwd from [data_table] where id=1”)
If not rs.eof then
Do while not rs.eof
Cname=rs(0)  将rs赋值给变量
Cpwd=rs(1)
….
Rs.moveNext
Loop
End if
%>

 4 [TEST] 现有10W条数据,Access数据库保存。

A.通过正常提取:

<%
Set rs=server.createObject(“adodb.recordSet”)
Rs.open “select * from people order by id desc”,cn,1,1
Do while not rs.eof
 
   Response.write rs(“id”)&” | ”
   Rs.moveNext
loop
%>

耗时3,250.000毫秒  3秒

B. 通过存储过程提取:

<%
Set cn=server.createObject(“adodb.connection”)
Cn.open “driver={microsoft access driver (*.mdb)};DBQ=”&server.mapPath(“db2.mdb”)
Set cmd=server.createObject(“adodb.command”)
cmd.activeConnection=cn
cmd.commandText=”select * from people order by id desc”
set rs=cmd.execute
do while not rs.eof
response.write rs(“id”)&” | ”
rs.moveNext
loop
%>

耗时 2,187.500毫秒  2秒

 C.使用GetRows()方法提取:

<%
Set cn=server.createObject(“adodb.connection”)
Set cmd=server.createObject(“adodb.command”)
Cn.open “driver={microsoft access driver (*.mdb)};DBQ=”&server.mapPath(“db2.mdb”)
cmd.activeConnection=cn
cmd.commandText=”select * from people order by id desc”
set rs=cmd.execute

rsArray=rs.getRows()     将记录集数据存入一个数组, 该数组默认为二维数组
for i=0 to uBound(rsArray,2)   Ubound(array,num)  其中num意指数组维数, 默认不填为一维, 2等于二维
response.write rsArray(0,i)&” | ”    
next
%>

耗时:187.500毫秒  0.2秒

rsArray(a,b)

a表示存入该数组记录集的字段号               b表示存入该数组记录集的条数

如下表:

id uname upwd
     RsArray(0,0)    RsArray(1,0)    RsArray(2,0)
     RsArray(0,1)    RsArray(1,1)    RsArray(2,1)
     RsArray(0,2)    RsArray(1,2)    RsArray(2,2)

扫描二维码推送至手机访问。

版权声明:本文由Ant.Master's Blog发布,如需转载请注明出处。

本文链接:https://iant.work/post/308.html

标签: ASP
分享给朋友:

“如何提高ASP数据查询执行效率” 的相关文章

Asp包含文件include动态包含方法(含变量)

Author:flymorn Source:飘易博客Categories:Asp编程 PostTime:2007-12-18 12:24:04 正 文:     很多时候,由于程序设计需要,要求在asp的include包含文件里调用动态的文件。 如&l...

ASP生成JSON

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!--#include file="json.asp"--><!--#include file="inc/Conn.asp" --><...

Replace,Instr函数不区别大小写的方法

有时候感觉互联网是万能的,什么答案都能到,但还是有时候不能直接找到答案,要靠自己去解决。 VB语言里Replace函数如何不区别大小写是很多问的问题,但网上难得看到好答案,一般都是用正则表达式,其实此函数本身就有忽略大小写的功能参数,只是平时大家都不用,于是舍近求远了。 Replace函数一共有六个...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。