【发布时间】:2021-02-03 15:34:52
【问题描述】:
使用 SQL Server 2016,我创建了表 VISIT,其中包含 3 列 ID、settoday、counter。这是我用来计算每日访问次数的 asp 代码。目前该表有2000多条记录:
<%
'today is calculated on local calendar then:
sql="select * from visit where settoday='"& today &"'"
recordSet.open sql,objcon
if not recordSet.eof then
sql="update visit set counter=counter+1 where id=" & recordSet("id")
objcon.execute sql
else
sql="insert into visit (settoday,counter) values ('" & today & "','1')"
objcon.execute sql
end if
recordSet.close
%>
ID 列是主键,我还在 settoday 列上创建了一个索引。但是当我检查事件探查器时,更新查询需要很长时间才能执行。我还能做些什么来优化更新代码?
【问题讨论】:
-
警告,您的代码容易受到注入攻击。 参数化您的查询。
-
你的表有索引还是主键
-
是的,我提到过 ID 是主键,settoday 是索引。 @查理脸
-
请通过pastetheplan.com分享这些查询的查询计划
-
为什么需要
settoday列上的索引?这会减慢表上的任何更新
标签: sql sql-server asp-classic sql-update query-optimization