34,591
社区成员
发帖
与我相关
我的任务
分享
<%'使用是asp语言
Set cons = Server.CreateObject("ADODB.Connection")
ConnStr="driver={SQL Server};server=.; uid=sa; pwd=sa; database=dbc"
cons.Open connstr
for i2= 1 to 80
a1=***:a2=***:a3=***
'说明:a1、a2、a3都是smallint类型的小于30000的整数,都是从其它access数据库获取的数据,已经测试过,获取这80条数据总共不会超过0.1秒。
sql1="select * from d where a1="&a1&" and a2="&a2&" and a3="&a3
set rs1=server.createobject("adodb.recordset")
rs1.open sql1,cons,0,3,1
if rs1.eof then
rs1.addnew
rs1("a1")=a1:rs1("a2")=a2:rs1("a3")=a3
rs1.update
end if
rs1.close
next
%>
<%'使用是asp语言,SQL Server数据库
Set cons = Server.CreateObject("ADODB.Connection")
ConnStr="driver={SQL Server};server=.; uid=sa; pwd=sa; database=dbc"
cons.Open connstr
j=0
for i= 1 to 80
sql1="select a1,a2,a3 from d where a1=2 and a2=3 and a3=4"
set rs1=server.createobject("adodb.recordset")
rs1.open sql1,cons,0,3,1
if rs1.eof then
j=j+1
end if
next
rs1.close
%>
就算没有建议索引,查询80次也不需要1秒吧?
如果把上面的代码中的if rs1.eof then修改成if rs1.recordcount=0 then则执行只需不到0.1秒,但仅仅这样改是不对的,因为改了之后无论数据库中是否存在该条记录,rs1.recordcount都返回-1,和if rs1.eof then不等价。要使rs1.recordcount返回正确的记录数,就必须把rs1.open sql1,cons,0,3,1改成rs1.open sql1,cons,1,3,1
但这样一改,执行时间又变成要1秒左右的,和原来的if rs1.eof then执行时间不相上下。
所以,我觉得我上面的那段查询是否存在的代码还可以再优化,缩短执行时间。
<%'使用是asp语言,SQL Server数据库
Set cons = Server.CreateObject("ADODB.Connection")
ConnStr="driver={SQL Server};server=.; uid=sa; pwd=sa; database=dbc"
cons.Open connstr
for i= 1 to 80
sql1="select * from d where a1=2 and a2=3 and a3=4"
set rs1=server.createobject("adodb.recordset")
rs1.open sql1,cons,0,3,1
if rs1.eof then '此处消耗大量时间,需要修改
end if
rs1.close '此处消耗一部分时间,应该移到for循环的外面(即next的下面)
next
%>
[/quote]
查询记录是否存在的那段代码 在数据库 中执行需要多长时间?优化办法:
1.对a1,a2,a3增加索引
2.不要用select * 改成select 1 from d where a1=2 and a2=3 and a3=4"
<%'使用是asp语言,SQL Server数据库
Set cons = Server.CreateObject("ADODB.Connection")
ConnStr="driver={SQL Server};server=.; uid=sa; pwd=sa; database=dbc"
cons.Open connstr
for i= 1 to 80
sql1="select * from d where a1=2 and a2=3 and a3=4"
set rs1=server.createobject("adodb.recordset")
rs1.open sql1,cons,0,3,1
if rs1.eof then '此处消耗大量时间,需要修改
end if
rs1.close '此处消耗一部分时间,应该移到for循环的外面(即next的下面)
next
%>
<%'使用是asp语言,SQL Server数据库
Set cons = Server.CreateObject("ADODB.Connection")
ConnStr="driver={SQL Server};server=.; uid=sa; pwd=sa; database=dbc"
cons.Open connstr
for i= 1 to 80
sql1="select * from d where a1=2 and a2=3 and a3=4"
set rs1=server.createobject("adodb.recordset")
rs1.open sql1,cons,0,3,1
if rs1.eof then '此处消耗大量时间
end if
rs1.close '此处消耗大量时间
next
%>
rs1.close将数据库关闭80次,消耗了大量时间,所以应该把rs1.close去掉,最后再关闭数据库。
此外,rs1.eof 也消耗大量时间,我本来打算把if rs1.eof then用 if rs1.recordcount=0 then来代替,可惜这样改无论数据库中是否存在该条记录,rs1.recordcount都返回-1。要使rs1.recordcount返回正确的值,必须把rs1.open sql1,cons,0,3,1改成rs1.open sql1,cons,1,3,1,但这样一来总的执行时间又变长了,和if rs1.eof then没什么区别。
请问有什么办法提高查询速度呢?
insert into d(a1, a2, a3)
select a1, a2, a3 from
(
select a1-1 a1, a2-1 a2, a3-1 a3 union all
select a1-2, a2-2, a3-2 union all
select a1-3, a2-3, a3-3 union all
......
select a1-80, a2-80, a3-80 --80条数据
) a
where not exists(
select 1 from d where d.a1=a.a1 and d.a2=a.a2 and d.a3=a.a3
)