ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: little-key

[分享] 提高百万级SQL数据库in查询速度的方法

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2012-11-8 12:50 | 显示全部楼层
方法一反着用挺新鲜的

TA的精华主题

TA的得分主题

发表于 2012-11-18 15:34 | 显示全部楼层
方法4的IN在这里是属于运算符,方法1的IN在这里是属于谓词,虽然都是IN,但表示的意义是不同。方法4是属于SELECT查询,方法1是属于子查询。两者是不同的查询。

TA的精华主题

TA的得分主题

发表于 2012-11-24 10:30 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
学习了,谢谢

TA的精华主题

TA的得分主题

发表于 2013-9-24 09:52 | 显示全部楼层
大师,我经过测试,有以下问题:
我之前用ACCESS做数据库,用SELECT * FROM TABLE WHERE FILEDS IN("XX","UU")这种方法是可以的。
但是我切换到用SQL SERVER做数据库,因为我是用变量的形式,,所以SELECT * FROM TABLE WHERE FILEDS IN('XX','UU')就不行了。。大师在SQL SERVER如何解决啊。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-9-24 10:40 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
平平淡淡的天使 发表于 2013-9-24 09:52
大师,我经过测试,有以下问题:
我之前用ACCESS做数据库,用SELECT * FROM TABLE WHERE FILEDS IN("XX"," ...

in在Sql Server 中是可以使用的

TA的精华主题

TA的得分主题

发表于 2013-9-24 10:47 | 显示全部楼层
本帖最后由 平平淡淡的天使 于 2013-9-24 10:49 编辑
little-key 发表于 2013-9-24 10:40
in在Sql Server 中是可以使用的

  1. Sub jinzhengduishou()
  2. t = Timer
  3. On Error Resume Next
  4. Application.ScreenUpdating = False
  5. shenghui = "SELECT 'ZZ'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '浙CDCDCDC有限公司%'"
  6.     jinyuan = "SELECT 'XX'as 发货公司,* FROM XXX where 发货公司中文 like '宁CDCDCD易有限公%'"
  7.     yangguang = "SELECT 'DD'as 发货公司,* FROM XXX WHERE 发货公司中文 IN('厦CDCDCDC明有限公司','浙CDCDCD股份有限公司')"
  8.     lidaxin = "SELECT 'GFD'as 发货公司,* FROM XXX WHERE 发货公司中文 IN('CDCDCD明有限公司','立CDCDCD有限公司','厦CDCD限公司') "
  9.     aodeliang = "SELECT 'THG'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '%CDCDCD%'"
  10.     tongshida = "SELECT 'OIU'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '厦门CDCDC'"
  11.     hengdiandebang = "SELECT 'MNB'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '浙CDCDCD口有限公司'"
  12.     ningbooushang = "SELECT 'NVDVD'as 发货公司,* FROM XXX WHERE 发货公司中文 in('宁CDCDCD公司','宁CDCDCDCD工工贸有限公司')"
  13.     zhejiangmeike = "SELECT 'FDFD'as 发货公司,* FROM XXXX WHERE 发货公司中文 IN('上CDCDCD有限公司','CDCDCD器有限公司')"
  14.     hangzhouzhuangcheng = "SELECT 'OIU'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%杭CDCDC出口有限公司%'"
  15.      zm = "SELECT 'FDFDF'as 发货公司,* FROM XXX WHERE 发货公司中文 IN('深圳CDCDCD有限公司','启CDCDCD技有限公司')"
  16.     ln = "SELECT 'FDFDFD'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%深CDCD照明%'"
  17.      yg = "SELECT 'FDCDC'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%深CDCDCD电科技%'"
  18.      xingji = "SELECT 'ADDAA'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%厦CDCD电器%'"
  19.       qiangl = "SELECT 'VDVSD'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%上CDCDC电子%'"
  20.       ljin = "SELECT 'VSCSDCDS'as 发货公司,* FROM XXX WHERE 发货公司中文 LIKE '%东CDCD电子%'"
  21.     donglin = "SELECT 'XAXASX'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%厦CSCSC电子%'"
  22.        zhongr = "SELECT 'DEWDW'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%南CSCSCS器%'"
  23.         zhoumin = "SELECT 'CSDCS'as 发货公司,* FROM XXXX WHERE 发货公司中文 LIKE '%深CSCSCSC份%'"
  24.         juzuo = "SELECT 'CDSCSD'as 发货公司,* FROM XXXX WHERE 发货公司中文 IN('深CSCSCS份有限公司','深CSCSCSCS业有限公司')"
  25.         hailan = "SELECT 'VDVDFV'as 发货公司,* FROM XXXX where 发货公司中文 in('厦CSCSCSCS公司','厦CSCSCSCS限公司')"
  26.         guoxing = "SELECT 'CSDCSD'as 发货公司,* FROM XXXXXX where 发货公司中文='佛CSACSSCS限公司'"
  27. Set cnn = New ADODB.Connection
  28. Set rst = New ADODB.Recordset
  29.   rst.CursorLocation = adUseClient
  30.    Range("a5:AT66536").Cells.ClearContents
  31.    ActiveSheet.PivotTables("数据透视表1").TableRange2.Delete
  32. If TextBox1.Text = "" Or TextBox2.Text = "" Then
  33.     MsgBox "请输入日期", 1 + 16, "系统提示"
  34. Else
  35.    strcn = "Provider=SQLOLEDB; User ID=XX;Password =XXX.X.XX;Data Source=XXXXXX.XXXX.XXX,1433;Initial Catalog=XXX" 'SQL SERVER版本
  36.     cnn.Open strcn
  37.    shuliang = "select sum(数量) from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 BETWEEN '" & TextBox1.Value & "' and '" & TextBox2.Value & "'"
  38.    jine = "select sum(美金金额) from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 BETWEEN '" & TextBox1.Value & "' and '" & TextBox2.Value & "'"
  39.     sql = "select 发货公司,sum(美金金额) as 总金额,sum(数量) as 总数量 from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 between '" & TextBox1.Value & "' and '" & TextBox2.Value & "'  group by 发货公司 ORDER BY sum(美金金额) desc "

  40.      sql1 = "select 发货公司,收货公司英文,sum(美金金额) as 总金额,sum(数量) as 总数量 from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 between '" & TextBox1.Value & "' and '" & TextBox2.Value & "'  group by 发货公司,收货公司英文 ORDER BY sum(美金金额) desc "

  41.     sql2 = "select 原产目的国,month(日期) as 月份,sum(美金金额) AS 总金额 from (" & shenghui & " union all " & jinyuan & " union all " & yangguang & " union all " & lidaxin & " union all " & aodeliang & " union all " & tongshida & " union all " & hengdiandebang & " union all " & ningbooushang & " union all " & zhejiangmeike & " union all " & hangzhouzhuangcheng & " union all " & zm & " union all " & ln & " union all " & yg & " union all " & xingji & " union all " & qiangl & " union all " & ljin & " union all " & donglin & " union all " & zhongr & " union all " & zhoumin & " union all " & juzuo & " union all " & hailan & " union all " & guoxing & ")a WHERE 日期 between '" & TextBox1.Value & "' and '" & TextBox2.Value & "'  group by 原产目的国,month(日期)"

  42.      End If
  43. Set rst = cnn.Execute(sql)
  44.     Cells(5, 1).CopyFromRecordset rst
  45. Set rst = cnn.Execute(sql1)
  46.     Cells(5, 10).CopyFromRecordset rst
  47.      Set objPivotCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlExternal)
  48.     Set objPivotCache.Recordset = cnn.Execute(sql2)
  49.      With objPivotCache
  50.         .CreatePivotTable TableDestination:=Range("R3"), TableName:="数据透视表1"
  51.     End With
  52.      With ActiveSheet.PivotTables("数据透视表1")
  53.         .PivotFields("原产目的国").Orientation = xlRowField
  54.         .PivotFields("月份").Orientation = xlColumnField
  55.         .AddDataField ActiveSheet.PivotTables("数据透视表1").PivotFields("总金额"), "求和项:总金额", xlSum
  56.        With .PivotFields("求和项:总金额")
  57.         .NumberFormat = "0.00"
  58.        End With
  59.     End With
  60. '   arr5 = cnn.Execute(sql2).GetRows
  61. '       arr5 = Application.WorksheetFunction.Transpose(arr5)
  62. '       Call daquxiaoshao
  63.       rst.Close
  64.     Set rst = Nothing
  65.       cnn.Close
  66.     Set cnn = Nothing
  67.       MsgBox " 运行时间:" & Format((Timer - t), "0.0000000")
  68. End Sub
复制代码

大师,不好意思,是我搞错了。。我没有SELECT * FROM (SELECT * FROM TABLE UNION ALL SELECT * FROM TABLE)A 这里没有加别名,在SQL SERVER中好像都要加别名才能识别。
大师,您所IN('XX','YY')基本3秒。我才100多一年的数据。。就花了26秒多。。惭愧啊。。代码如下:大师帮忙优化下吧。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-9-24 10:55 | 显示全部楼层
平平淡淡的天使 发表于 2013-9-24 10:47
大师,不好意思,是我搞错了。。我没有SELECT * FROM (SELECT * FROM TABLE UNION ALL SELECT * FROM ...

(SELECT * FROM TABLE UNION ALL SELECT * FROM TABLE)
你这个重复的意义是什么呢?

TA的精华主题

TA的得分主题

发表于 2013-9-24 11:03 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
little-key 发表于 2013-9-24 10:55
(SELECT * FROM TABLE UNION ALL SELECT * FROM TABLE)
你这个重复的意义是什么呢?

就是,我有很多个条件,我要把它先汇总 所以要用UNION ALL,,然后再汇总的基础上进行 分组 所以完整的是 SELECT SUM(数量),sum(金额),sum(名称) from (这里面试所有UNION ALL 汇总的数据) a  这里要加个别名,SQL SERVER 才能识别。。
是这个意思,大师。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2013-9-24 13:30 | 显示全部楼层
平平淡淡的天使 发表于 2013-9-24 11:03
就是,我有很多个条件,我要把它先汇总 所以要用UNION ALL,,然后再汇总的基础上进行 分组 所以完整的是 ...

你这个是一个表的合并

TA的精华主题

TA的得分主题

发表于 2013-9-24 15:10 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
little-key 发表于 2013-9-24 13:30
你这个是一个表的合并

大师,,我上面的代码,能否帮忙优化下,,谢谢!!
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-17 01:36 , Processed in 0.035836 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表