大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍SQLServer如何使用UNION代替OR提升查询性能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
成都创新互联致力于成都网站设计、成都网站制作,成都网站设计,集团网站建设等服务标准化,推过标准化降低中小企业的建站的成本,并持续提升建站的定制化服务水平进行质量交付,让企业网站从市场竞争中脱颖而出。 选择成都创新互联,就选择了安全、稳定、美观的网站建设服务!
SQLServer数据库查询的过程中,通过对SQL语句的优化来提高SQL查询的性能。下面创新互联小编来讲解下SQLServer怎么使用UNION代替OR提升查询性能?
SQLServer怎么使用UNION代替OR提升查询性能
SQL>settimingonSQL>setautotraceonSQL>selectcount(*)rowcount_lhy2fromswgl_ddjbxxt3wheret。fzgs_dm='001085'4and(t。lrr_dm='e90e3fe4237c4af988477329c7f2059e'orexists5(selecty。kh_id6fromkhgl_khywdlxxy7wherey。kh_id=t。kh_id8andy。sskhjl_dm='e90e3fe4237c4af988477329c7f2059e')or9t。kpr_dm='e90e3fe4237c4af988477329c7f2059e')10andt。xjbz='9999'11andt。FROMNBGL1='0';SQL>setline300SQL>/ROWCOUNT_LHY————————60已用时间:00:00:20。53执行计划——————————————————————————————————————Planhashvalue:1217125969——————————————————————————————————————————————————————————————————————|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|——————————————————————————————————————————————————————————————————————|0|SELECTSTATEMENT||1|86|28048(1)|00:05:37||1|SORTAGGREGATE||1|86||||*2|FILTER|||||||*3|TABLEACCESSFULL|SWGL_DDJBXX|5926|497K|28048(1)|00:05:37||*4|TABLEACCESSBYINDEXROWID|KHGL_KHYWDLXX|1|57|5(0)|00:00:01||*5|INDEXRANGESCAN|IDX_KHGL_KHYWDLXX_KHID|1||3(0)|00:00:01|————————————————————————————————————————————————————————————————————PredicateInformation(identifiedbyoperationid):——————————————————————————————————-2-filter("T"。"LRR_DM"='e90e3fe4237c4af988477329c7f2059e'OR"T"。"KPR_DM"='e90e3fe4237c4af988477329c7f2059e'OREXISTS(SELECT0FROM"KHGL_KHYWDLXX""Y"WHERE"Y"。"KH_ID"=:B1AND"Y"。"SSKHJL_DM"='e90e3fe4237c4af988477329c7f2059e'))3-filter("T"。"FROMNBGL1"='0'AND"T"。"XJBZ"='9999'AND"T"。"FZGS_DM"='001085')4-filter("Y"。"SSKHJL_DM"='e90e3fe4237c4af988477329c7f2059e')5-access("Y"。"KH_ID"=:B1)统计信息——————————————————————————————————————0recursivecalls0dbblockgets804560consistentgets71127physicalreads0redosize516bytessentviaSQL*Nettoclient469bytesreceivedviaSQL*Netfromclient2SQL*Netroundtripsto/fromclient0sorts(memory)0sorts(disk)1rowsprocessed
用UNION代替OR对其进行优化后的代码如下:
SQL>selectcount(*)2from(select*3fromswgl_ddjbxxt4wheret。lrr_dm='e90e3fe4237c4af988477329c7f2059e'5andt。fzgs_dm='001085'6andt。xjbz='9999'7andt。FROMNBGL1='0'8union9select*10fromswgl_ddjbxxt11wheret。kpr_dm='e90e3fe4237c4af988477329c7f2059e'12andt。fzgs_dm='001085'13andt。xjbz='9999'14andt。FROMNBGL1='0'15union16select*17fromswgl_ddjbxxt18whereexists19(selecty。kh_id20fromkhgl_khywdlxxy21wherey。kh_id=t。kh_id22andy。sskhjl_dm='e90e3fe4237c4af988477329c7f2059e')23andt。fzgs_dm='001085'24andt。xjbz='9999'25andt。FROMNBGL1='0');COUNT(*)————————60已用时间:00:00:06。89执行计划——————————————————————————————————————Planhashvalue:3846872744————————————————————————————-----------------------------------------------------------------------------|Id|Operation|Name|Rows|Bytes|TempSpc|Cost(%CPU)|Time|-----------------------------------------------------------------------------------------------------------------------|0|SELECTSTATEMENT||1|||52263(1)|00:10:28||1|SORTAGGREGATE||1||||||2|VIEW||5996|||52263(1)|00:10:28||3|SORTUNIQUE||5996|2238K|6344K|52263(47)|00:10:28||4|UNION-ALL||||||||*5|TABLEACCESSFULL|SWGL_DDJBXX|59|19234||28037(1)|00:05:37||*6|TABLEACCESSBYINDEXROWID|SWGL_DDJBXX|10|3260||1209(1)|00:00:15||*7|INDEXRANGESCAN|IDX_SWGL_DDJBXX_KPRDM|4748|||34(0)|00:00:01||*8|TABLEACCESSBYINDEXROWID|SWGL_DDJBXX|1|326||5(0)|00:00:01||9|NESTEDLOOPS||5927|2216K||22527(1)|00:04:31||10|SORTUNIQUE||10165|565K||1916(1)|00:00:23||11|TABLEACCESSBYINDEXROWID|KHGL_KHYWDLXX|10165|565K||1916(1)|00:00:23||*12|INDEXRANGESCAN|IDX_KHGL_KHYWDLXX_SSKHJL|10165|||111(0)|00:00:02||*13|INDEXRANGESCAN|IDX_SWGL_DDJBXX_KHID|2|||2(0)|00:00:01|-----------------------------------------------------------------------------------------------------------------------PredicateInformation(identifiedbyoperationid):---------------------------------------------------5-filter("T"。"LRR_DM"='e90e3fe4237c4af988477329c7f2059e'AND"T"。"FROMNBGL1"='0'AND"T"。"XJBZ"='9999'AND"T"。"FZGS_DM"='001085')6-filter("T"。"FROMNBGL1"='0'AND"T"。"XJBZ"='9999'AND"T"。"FZGS_DM"='001085')7-access("T"。"KPR_DM"='e90e3fe4237c4af988477329c7f2059e')8-filter("T"。"FROMNBGL1"='0'AND"T"。"XJBZ"='9999'AND"T"。"FZGS_DM"='001085')12-access("Y"。"SSKHJL_DM"='e90e3fe4237c4af988477329c7f2059e')13-access("Y"。"KH_ID"="T"。"KH_ID")统计信息----------------------------------------------------------1recursivecalls0dbblockgets128422consistentgets10308physicalreads0redosize512bytessentviaSQL*Nettoclient469bytesreceivedviaSQL*Netfromclient2SQL*Netroundtripsto/fromclient2sorts(memory)0sorts(disk)1rowsprocessed
SQL改写之后,执行时间由原来的20秒下降到6秒,逻辑读由804560降低到128422,性能还是有很大提升的,到了这里优化还没完,可以创建一个组合索引进一步优化。
createindexidxonswgl_ddjbxx(fzgs_dm,xjbz,FROMNBGL1);
SQLServer怎么使用UNION代替OR提升查询性能
创建索引之后,原始的SQL执行时间,执行计划,统计信息如下:
SQL>selectcount(*)rowcount_lhy2fromswgl_ddjbxxt3wheret。fzgs_dm='001085'4and(t。lrr_dm='e90e3fe4237c4af988477329c7f2059e'orexists5(selecty。kh_id6fromkhgl_khywdlxxy7wherey。kh_id=t。kh_id8andy。sskhjl_dm='e90e3fe4237c4af988477329c7f2059e')or9t。kpr_dm='e90e3fe4237c4af988477329c7f2059e')10andt。xjbz='9999'11andt。FROMNBGL1='0';ROWCOUNT_LHY------------60已用时间:00:00:02。96执行计划----------------------------------------------------------Planhashvalue:3049366449--------------------------------------------------------------------------------------------------------|Id|Operation|Name|Rows|Bytes|Cost(%CPU)|Time|--------------------------------------------------------------------------------------------------------|0|SELECTSTATEMENT||1|86|506(0)|00:00:07||1|SORTAGGREGATE||1|86||||*2|FILTER|||||||3|TABLEACCESSBYINDEXROWID|SWGL_DDJBXX|5926|497K|506(0)|00:00:07||*4|INDEXRANGESCAN|IDX|2370||12(0)|00:00:01||*5|TABLEACCESSBYINDEXROWID|KHGL_KHYWDLXX|1|57|5(0)|00:00:01||*6|INDEXRANGESCAN|IDX_KHGL_KHYWDLXX_KHID|1||3(0)|00:00:01|--------------------------------------------------------------------------------------------------------PredicateInformation(identifiedbyoperationid):---------------------------------------------------2-filter("T"。"LRR_DM"='e90e3fe4237c4af988477329c7f2059e'OR"T"。"KPR_DM"='e90e3fe4237c4af988477329c7f2059e'OREXISTS(SELECT0FROM"KHGL_KHYWDLXX""Y"WHERE"Y"。"KH_ID"=:B1AND"Y"。"SSKHJL_DM"='e90e3fe4237c4af988477329c7f2059e'))4-access("T"。"FZGS_DM"='001085'AND"T"。"XJBZ"='9999'AND"T"。"FROMNBGL1"='0')5-filter("Y"。"SSKHJL_DM"='e90e3fe4237c4af988477329c7f2059e')6-access("Y"。"KH_ID"=:B1)统计信息----------------------------------------------------------1recursivecalls0dbblockgets702767consistentgets0physicalreads0redosize516bytessentviaSQL*Nettoclient469bytesreceivedviaSQL*Netfromclient2SQL*Netroundtripsto/fromclient0sorts(memory)0sorts(disk)1rowsprocessed
改写的SQL:
SQL>selectcount(*)2from(select*3fromswgl_ddjbxxt4wheret。lrr_dm='e90e3fe4237c4af988477329c7f2059e'5andt。fzgs_dm='001085'6andt。xjbz='9999'7andt。FROMNBGL1='0'8union9select*10fromswgl_ddjbxxt11wheret。kpr_dm='e90e3fe4237c4af988477329c7f2059e'12andt。fzgs_dm='001085'13andt。xjbz='9999'14andt。FROMNBGL1='0'15union16select*17fromswgl_ddjbxxt18whereexists19(selecty。kh_id20fromkhgl_khywdlxxy21wherey。kh_id=t。kh_id22andy。sskhjl_dm='e90e3fe4237c4af988477329c7f2059e')23andt。fzgs_dm='001085'24andt。xjbz='9999'25andt。FROMNBGL1='0');COUNT(*)----------60已用时间:00:00:00。53执行计划----------------------------------------------------------Planhashvalue:2947849958-------------------------------------------------------------------------------------------------------------------------|Id|Operation|Name|Rows|Bytes|TempSpc|Cost(%CPU)|Time|-------------------------------------------------------------------------------------------------------------------------|0|SELECTSTATEMENT||1|||3469(1)|00:00:42||1|SORTAGGREGATE||1||||||2|VIEW||5995|||3469(1)|00:00:42||3|SORTUNIQUE||5995|2238K|4760K|3469(86)|00:00:42||4|UNION-ALL||||||||*5|TABLEACCESSBYINDEXROWID|SWGL_DDJBXX|59|19234||506(0)|00:00:07||*6|INDEXRANGESCAN|IDX|2370|||12(0)|00:00:01||7|TABLEACCESSBYINDEXROWID|SWGL_DDJBXX|10|3260||50(0)|00:00:01||8|BITMAPCONVERSIONTOROWIDS||||||||9|BITMAPAND||||||||10|BITMAPCONVERSIONFROMROWIDS||||||||*11|INDEXRANGESCAN|IDX|2370|||12(0)|00:00:01||12|BITMAPCONVERSIONFROMROWIDS||||||||*13|INDEXRANGESCAN|IDX_SWGL_DDJBXX_KPRDM|2370|||34(0)|00:00:01||*14|HASHJOINRIGHTSEMI||5926|2216K||2423(1)|00:00:30||15|TABLEACCESSBYINDEXROWID|KHGL_KHYWDLXX|10165|565K||1916(1)|00:00:23||*16|INDEXRANGESCAN|IDX_KHGL_KHYWDLXX_SSKHJL|10165|||111(0)|00:00:02||17|TABLEACCESSBYINDEXROWID|SWGL_DDJBXX|5926|1886K||506(0)|00:00:07||*18|INDEXRANGESCAN|IDX|2370|||12(0)|00:00:01|-------------------------------------------------------------------------------------------------------------------------PredicateInformation(identifiedbyoperationid):---------------------------------------------------5-filter("T"。"LRR_DM"='e90e3fe4237c4af988477329c7f2059e')6-access("T"。"FZGS_DM"='001085'AND"T"。"XJBZ"='9999'AND"T"。"FROMNBGL1"='0')11-access("T"。"FZGS_DM"='001085'AND"T"。"XJBZ"='9999'AND"T"。"FROMNBGL1"='0')filter("T"。"FROMNBGL1"='0'AND"T"。"XJBZ"='9999'AND"T"。"FZGS_DM"='001085')13-access("T"。"KPR_DM"='e90e3fe4237c4af988477329c7f2059e')14-access("Y"。"KH_ID"="T"。"KH_ID")16-access("Y"。"SSKHJL_DM"='e90e3fe4237c4af988477329c7f2059e')18-access("T"。"FZGS_DM"='001085'AND"T"。"XJBZ"='9999'AND"T"."FROMNBGL1"='0')统计信息---------------------------------------------------------1recursivecalls0dbblockgets25628consistentgets0physicalreads0redosize512bytessentviaSQL*Nettoclient469bytesreceivedviaSQL*Netfromclient2SQL*Netroundtripsto/fromclient1sorts(memory)0sorts(disk)1rowsprocessed。
以上是“SQLServer如何使用UNION代替OR提升查询性能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!