前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据库涉及大量数据查询时的注意事项

数据库涉及大量数据查询时的注意事项

作者头像
白墨石
发布2022-05-17 14:07:37
4840
发布2022-05-17 14:07:37
举报
文章被收录于专栏:生信情报站
  1. 避免频繁连接和关闭数据库,这样会导致IO访问次数太频繁。
  2. 设计表时要建立适当的索引,尤其要在 where 及 order by 涉及的列上建立索引
  3. 避免全表扫描,以下情况会导致放弃索引直接进行全部扫描
  • 避免在 where 子句中使用!=或<>操作符
  • 避免在 where 子句中对字段进行 null 值判断 select id from table where num is null 解决方法:建表时设置默认值0,也就是将null用0填充,然后查询: select id from table where num=0
  • 避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描 select id from t where num=10 or num=20 解决方法:使用 union select id from t where num=10 union all select id from t where num=20
  • 避免使用 like select id from t where name like ‘%abc%’ 解决方法:使用全文检索
  • 避免使用 in 和 not in select id from t where num in(1,2,3) 解决方法1:连续值使用 between 解决方法2:用 exists 替换 in select num from a where exists(select 1 from b where num=a.num)
  • 避免使用参数 select id from t where num=@num 解决方法:强制查询使用索引 select id from t with(index(index_name)) where num=@num
  • 避免表达式操作 select id from t where num/2=100 解决方法:select id from t where num=100*2
  • 避免函数操作 select id from t where substring(name,1,3)=’abc’ 查询以abc开头的id 解决方法:全文索引
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档