专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

大数据:百万级数据处理技巧篇(二)

ins518 2024-11-06 18:58:22 技术文章 11 ℃ 0 评论

大数据:百万级数据处理技巧篇(一)

SQL 优化技巧(接上)

6、能用union all就不用用union,因为union会对数据进行排序,会影响效率,如:

select id from users where id=1
union 
select id from users where id=2;

替换为:

select id from users where id=1
union all
select id from users where id=2;

7、where 子句中含in 枚举字段,枚举类型太多,如oracle最大支持1000个值, 如:

select id from users where id in (1,2,3,4,5,6,7,...,1000);

如果值比较固定,可以将数据存入数据字典表(data dictionary table),然后按照第五点优化,用exists替换(也可以join table)。

8、select 中尽量不用使用子查询,如:

select id
         ,user_id
         ,(select user_name
            from users  u 
          where a.user_id=u.id) as user_name
  from acount where a.id=1;

可以替换为join表:

select a.id
         ,a.user_id
        ,u.user_name user_name
 from acount a left join users u on  a.user_id=u.id  where a.id=1;

9、少用distinct去重,例如账户表中有很多用户,按distinct处理,会对原始数据排序,默认升序;如:

select distinct a.user_id
from acount;

替换为:

select a.user_id,count(1)
from acount
group by user_id;

10、大表的数据初始化,尽量不要先建索引,可以先把数据导入数据库中,然后初始化索引。



本篇文章就先介绍到这里,如果大家遇到其他问题,欢迎在底下留言:包含但不限于数据导入导出/SQL性能问题/千万级数据优化/亿级数据优化

编:昊几居

作者介绍:资深大数据库开发工程师,专注数据治理方向。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表