由于底层的处理机制大不相同,hivemysql在查询上还是有较大差异的!

 

  1. 单个表的select操作

最简单的查询

  • .in关键字查询:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2)

例:select * from t_student where age in (21,23);

select * from t_student where age not in (21,23);

between and的范围查询:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2

例:select * frome t_student where age between 21 and 29;

select * frome t_student where age not between 21 and 29;

like的模糊查询:select 字段1,字段2... frome 表名 where 字段 [not] like '字符串'

"%"代表任意字符;

"_"代表单个字符;

空值查询:select 字段1,字段2...frome 表名 where 字段 is[not] null;

 

  •  

and多条件查询:

select 字段1,字段2...frome 表名 where 条件表达式1 and 条件表达式2 [and 条件表达式n]

例:select * frome t_student where gradeName='一年级' and age=23

or的多条件查询

select 字段1,字段2...frome 表名 where 条件表达式1 or 条件表达式2 [or 条件表达式n]

例:select * frome t_student where gradeName='一年级' or age=23//或者,条件只要满足一个

distinct去重复查询:select distinct 字段名 from 表名

Orderby sortby 的区别(前者是要mapreduce操作后者在本机上排序)

 

  1. 分组查询 group by 属性名 [having 条件表达式][with rollup]

常用函数:count group_concat rollup

1.select gradeName,count(stuName) from t_student group by gradeName;

2.select gradeName,count(stuName) from t_student group by gradeName having count(stuName)>3;

3.select gradeName,group_concat(stuName) from t_student group by gradeName with rollup;

 

  1. 子查询

0.一般嵌套子查询

1.带比较运算符的子查询(子查询可以使用比较运算符)

select * from t_book where price>=(select price from t_priceLevel where priceLevel=1);

2. in关键字的子查询(一个查询语句的条件可能落在另一个select语句的查询结果中)

select * from t_book where bookType in(select id from t_bookType);

select * from t_book where bookType not in(select id from t_bookType);

3.exists关键字的子查询(加入子查询查询到记录,则进行外层查询,否则,不执行外层查询)

select * from t_book where exists(select * from t_booktype);

select * from t_book where not exists(select * from t_booktype);

4.any关键字的子查询(any关键字表示满足其中任一条件)

select * from t_book where price>= any(select price from t_priceLevel);

5.all关键字的子查询(all关键字表示满足所有条件)

select * from t_book where price>= all(select price from t_priceLevel);

2,3,4,5 目前仅mysql支持

Hive中有基于partition的查询,从效率上讲是一个剪枝的过程

 

  1. 多表连接查询

Mysql中支持内连接,左右外连接(注意外连接的工作原理,没有匹配项返回null,可用where过滤),级联多表连接的时候,从中间解读;

与此对应hive中有内连接join,外连接(left/right outer join)加上full outer join(全表关联),semi join是用来在hive中解决in exists子查询的问题。

Hive的join可大致划分为common join map join ,两者的区别在于后者应用于大小表数据倾斜的情况具体参考http://www.cnblogs.com/1130136248wlxk/articles/5517628.html

Map完输出为相同keylist,然而按照hash分发到不同reducetask中。

 

  1. 合并查询

1.union

使用union关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;

select id from t_book union select id from t_bookType;

2.union all

使用union all,不会去除掉重复的记录;

select id from t_book union all select id from t_bookType;

  补充:hive 性能优化方向

     列剪裁 分区剪裁  需要设定相关参数

     join 小表依次放在前面 ,左边的在reduce阶段要放进内存,减少内存发生溢出的几率

    map join 用于小表和大表的倾斜情况