Home > Database > Mysql Tutorial > 给互动百科优化一个SQL

给互动百科优化一个SQL

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:36:46
Original
1285 people have browsed it

这是他们开发写的一个SQL语句:SELECTSQL_NO_CACHECOUNT(a.ad_id)FROMt_wiki_adaWHERE1=1andexists(select*fromt_wiki_ad_relbwherea.ad_id=b.ad_idandb.rel_sta

这是他们开发写的一个SQL语句:

SELECT SQL_NO_CACHE COUNT(a.ad_id) FROM t_wiki_ad a WHERE 1 = 1 and exists (select * from t_wiki_ad_rel b where a.ad_id = b.ad_id and b.rel_state = 1 and b.biz_code = '100010') and exists (select * from t_wiki_ad_rel b where a.ad_id = b.ad_id and b.rel_state = 1 and b.biz_id = '代孕母亲') and exists (select * from t_wiki_ad_rel b where a.ad_id = b.ad_id and b.rel_state = 1);


执行计划:

wKiom1MFolTQscQ1AAVBjoae4Ug146.jpg


执行时间:

wKiom1MFotHCoCjQAAIsRqVBfH0996.jpg

耗时30.20秒


优化思路,去除没必要的关联查询,去除1=1,把exist改为join,优化后:

wKioL1MFo0GjWrCPAAEQd1rQQew727.jpg

耗时0秒


执行计划:

wKiom1MFpInDRRxaAANI8NOHBmk584.jpg



本文出自 “贺春旸的技术专栏” 博客,请务必保留此出处

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template