打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ACCESS的真假:二、检查记录有无再insert 或 update 比 不管有无直接 d...
你自己可以测试一下,不过根据经验,
DELETE,再INSERT(数据量比只插入没有的记录大 ) ,再建索引,速度比较慢。


比如表 table1 (id int primary key,cname varchar(10)
+----+-------+
| id | cname |
+----+-------+
|  1 |    1  |
|  2 |    2  |
|  3 |    3  |
|  4 |    4  |
|  5 |    5  |
|  6 |    6  |
|  7 |    7  |
|  8 |    8  |
| .. |   ... |
+----+-------+

 

如果现在有一记录 (1234, 'KKKK'), 需要添加。那么两种方法。

方法一: 先做判断记录 1234是否存在,存在的话就 update table1 set cname = 'KKKK' where id=1234; 否则就 insert into table1 values(1234,'KKKK');

方法二: 不做任何判断,直接先 delete from table1 where id=1234; insert into table1 values(1234,'KKKK');

 

那么哪一种方法快?

在未做试验前,感觉上第二种方法,删除再添加记录应该是比较慢的一种。 但只是凭感觉,事实上是什么样呢,于是做了个简单的测试。

ACCESS 2003 SP3 + Windows 2000 SP4

新建一个空的t.mdb文件,然后建表 table1 (id int primary key,cname varchar(10)

新建一个模块,代码如下。

  1. Option Compare Database  
  2. Option Explicit  
  3.   
  4.   
  5. Public Sub tx()  
  6.   
  7.   
  8.     Dim i As Integer  
  9.     For i = 1 To 10000  
  10.         CurrentProject.Connection.Execute "insert into table1 values(" & i & ",'" & i & "')"  
  11.     Next  
  12. End Sub  
  13.   
  14.   
  15. Public Sub t1()  
  16.     'CurrentProject.Connection.Execute "delete from table1 where id=1234"   
  17.       
  18.     Dim rs As New ADODB.Recordset  
  19.     Dim ssql As String  
  20.     ssql = "select * from table1 where id=1234"  
  21.     rs.Open ssql, CurrentProject.Connection, adOpenStatic, adLockOptimistic  
  22.     If rs.EOF Then  
  23.         rs.AddNew  
  24.         rs.Fields("id").Value = 1234  
  25.     End If  
  26.     rs.Fields("cname").Value = "KKK"  
  27.     rs.Update  
  28.     rs.Close  
  29. End Sub  
  30.   
  31. Public Sub t2()  
  32.     'CurrentProject.Connection.Execute "delete from table1 where id=1234"   
  33.       
  34.     Dim rs As New ADODB.Recordset  
  35.     Dim ssql As String  
  36.     ssql = "select * from table1 where id=1234"  
  37.     rs.Open ssql, CurrentProject.Connection  
  38.     If rs.EOF Then  
  39.         ssql = "insert into table1 values(1234,'1234')"  
  40.     Else  
  41.         ssql = "update table1 set cname='1234' where id=1234"  
  42.     End If  
  43.     rs.Close  
  44.     CurrentProject.Connection.Execute ssql  
  45. End Sub  
  46.   
  47. Public Sub t3()  
  48.     'CurrentProject.Connection.Execute "delete from table1 where id=1234"   
  49.       
  50.     Dim ssql As String  
  51.     Dim nAffectedRow As Long  
  52.       
  53.     ssql = "update table1 set cname='1234' where id=1234"  
  54.     CurrentProject.Connection.Execute ssql, nAffectedRow  
  55.       
  56.     If nAffectedRow = 0 Then  
  57.         ssql = "insert into table1 values(1234,'1234')"  
  58.         CurrentProject.Connection.Execute ssql, nAffectedRow  
  59.     End If  
  60.       
  61. End Sub  
  62.   
  63. Public Sub t4()  
  64.     'CurrentProject.Connection.Execute "delete from table1 where id=1234"   
  65.       
  66.     Dim ssql As String  
  67.       
  68.     ssql = "delete from table1 where id=1234"  
  69.     CurrentProject.Connection.Execute ssql  
  70.       
  71.     ssql = "insert into table1 values(1234,'1234')"  
  72.     CurrentProject.Connection.Execute ssql  
  73.       
  74. End Sub  
  75.   
  76.   
  77.   
  78. Public Sub t()  
  79.     Dim i As Integer  
  80.     Dim nCnt As Integer  
  81.     nCnt = 5000  
  82.       
  83.     Debug.Print "t1 start.", Now  
  84.     For i = 1 To nCnt  
  85.         Call t1  
  86.     Next i  
  87.     Debug.Print "t1 end  .", Now  
  88.       
  89.     Debug.Print "t2 start.", Now  
  90.     For i = 1 To nCnt  
  91.         Call t2  
  92.     Next i  
  93.     Debug.Print "t2 end  .", Now  
  94.       
  95.     Debug.Print "t3 start.", Now  
  96.     For i = 1 To nCnt  
  97.         Call t3  
  98.     Next i  
  99.     Debug.Print "t3 end  .", Now  
  100.       
  101.     Debug.Print "t4 start.", Now  
  102.     For i = 1 To nCnt  
  103.         Call t4  
  104.     Next i  
  105.     Debug.Print "t4 end  .", Now  
  106.       
  107. End Sub  

结果很有趣。在把  'CurrentProject.Connection.Execute "delete from table1 where id=1234" 这一句注释的情况下,也就是表中存在该记录。需要进行更新。

表中记录已存在 测试结果:
t1 start.     5/14/2009 12:02:36 PM
t1 end  .     5/14/2009 12:02:42 PM
t2 start.     5/14/2009 12:02:42 PM
t2 end  .     5/14/2009 12:02:52 PM
t3 start.     5/14/2009 12:02:52 PM
t3 end  .     5/14/2009 12:02:57 PM
t4 start.     5/14/2009 12:02:57 PM
t4 end  .     5/14/2009 12:03:05 PM

 

把这个注释找开,即模拟记录不存在的情况。

表中记录不存在 测试结果:
t1 start.     5/14/2009 12:14:22 PM
t1 end  .     5/14/2009 12:14:34 PM
t2 start.     5/14/2009 12:14:34 PM
t2 end  .     5/14/2009 12:14:48 PM
t3 start.     5/14/2009 12:14:48 PM
t3 end  .     5/14/2009 12:15:01 PM
t4 start.     5/14/2009 12:15:01 PM
t4 end  .     5/14/2009 12:15:14 PM

 

在更新情况下各方法所耗时。t1: 6s  t2: 10s t3: 5s t4: 8s
在插入情况下各方法所耗时。t1: 12s  t2: 14s t3: 13s t4: 13s

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
账套解决方案
TreeView (树视图)遍历数据库的方法
在表中新增一个字段的代码
《神奇的VBA》编程:查询Access数据库
excel连接ORACLE正确方法
VBA在Excel中的应用(四)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服