Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

access vba的执行效率太低,怎样才能改善?

jacksgq  发表于:2018-03-18 20:55:46  
复制

要从一个100万行的表里成对提取搜索的数据,用for循环时,执行1000次要近2个小时才能执行完毕,能改善吗?谢谢!

代码如下:


Sub aa()
Dim i As Integer
Dim sqlstr As String
Dim mydb As Database
Dim myds As Recordset, bb As Recordset
Dim NotInList As String
Dim fld As Field
Dim sql As String
DoCmd.SetWarnings False
start = Now()

For i = 1 To 1000

sql = "insert  into ccc select * from (select top 1 id,字段1,字段3,字段5  from a where 字段8 = 1 And 字段9 = 1 union select top 1 id,字段1,字段3,字段5  from a  where id>(select top 1 id from a where 字段8=1 and 字段9=1) and 字段1=(select top 1 字段1 from a where  字段8=1 and 字段9=1) and 字段9=0)"

DoCmd.RunSQL sql

DoCmd.RunSQL "delete  from A where exists(select * from ccc where cc.id=A.id)"

Next i

DoCmd.SetWarnings True

endtime = Now()

MsgBox start & "开始," & endtime & "结束。" & "已经完成,用时" & Hour(endtime) - Hour(start) & "小时" & Minute(endtime) - Minute(start) & "分" & Second(endtime) - Second(start) & "秒"


End Sub

 

Top
cwzj3807 发表于:2018-03-26 20:58:41

学习中



西出阳关无故人 发表于:2018-03-27 11:17:39
同样的查询为什么要执行一千次?不好理解,发实例看看。

SiliconXu 发表于:2018-03-27 15:10:36
我觉得首先要先优化的查询开始。

chinasa 发表于:2018-03-27 16:29:30

100万的数据量根本不是问题,你下面这段代码好复杂啊,能优化不?

sql = "insert  into ccc select * from (select top 1 id,字段1,字段3,字段5  from a where 字段8 = 1 And 字段9 = 1 union select top 1 id,字段1,字段3,字段5  from a  where id>(select top 1 id from a where 字段8=1 and 字段9=1) and 字段1=(select top 1 字段1 from a where  字段8=1 and 字段9=1) and 字段9=0)"

 



苏雨杭 发表于:2018-04-18 07:54:41
你这个查询看不懂,貌似有冲突的,给实例看看。

总记录:5篇  页次:1/1 9 1 :