做了一段时间的性能测试,把自己在性能测试过程中,使用到的Oracle中用到的sql语句整理一番
(1)多个字段以某种格式拼接
Oracle方式:"||"字符串拼接符;
示例:将“id”及“code”用逗号拼接: select t.id||','||t.code from OTable t;
MySQL方式:使用 concat()函数;
示例:select concat(t.id,',',t.code) from MTable t;
(2)以某一个字段为维度统计数量
Oracle方式:count + group by;
示例:统计某个创建人对应的数据数量:select t.creatorname,count(1) from OTable t (此处省去where条件查询) group by t.creatorname;
(3)统计每秒生成的数据的数量,并按数量由大到小进行排序-----------结果常用于性能测试目标TPS确定的参考值;
Oracle方式:count + group by + order by;(修改下面的sql中to_char的格式,可修改精确度为min或者hour)
示例:select to_char(t.Createtime,'yyyy-mm-dd hh24:mi:ss'),COUNT(1) from OTable t where
to_char(t.Createtime,'yyyy-mm-dd hh24:mi:ss') >='2016-01-01 00:00:00' and
to_char(t.Createtime,'yyyy-mm-dd hh24:mi:ss') <='2016-12-31 23:59:59'
group by to_char(t.Createtime,'yyyy-mm-dd hh24:mi:ss')
order by count(1) desc;
MySQL方式:DATE_FORMAT+ group by + order by;(修改DATE_FORMAT中的第二个参数,可修改精确度为min或者hour)
示例:select DATE_FORMAT(start_time,'%Y%m%d %H%i%s') iSecond,count(id) as icount
from MTable t where t.start_time BETWEEN '2017-06-01 00:00:00' and '2017-06-01 23:59:59'
group by iSecond
order by icount desc;
(4)统计某张表两个时间的时间差
MySQL方式:TIMESTAMPDIFF()函数,计算两个时间之间的差值,若第一个时间参数值晚于第二个时间参数值,则结果为负值;
示例:SELECT t.START_TIME, t.FINISH_TIME,
TIMESTAMPDIFF(SECOND, t.START_TIME, t.FINISH_TIME) AS `iSecond`
from MTable t where t.start_time BETWEEN '2017-05-10 00:00:00' and '2017-06-30 23:59:59';
(本文转载自网络,感谢原文作者,如有版权问题请及时留言,我们会第一时间处理)