Teradata 之top n与sample n

Teradata取n条样本数据的方法有两种:redis

select top x * from table;
select * from table sample n;数据库

那么两者有什么区别呢?说明以下:less

TOP N 
若是有Order By关键字首先要对数据库的数据进行排序,而后获取N条数据或者抽样比率为N;
若是没有Order By关键字,要作一次STAT FUNCTION的全AMP收集,而后选择某一个或者几个AMP 来抽取数据。dom

Sample N 
首先要对数据库的数据进行全表扫描,而后获取N数据;
采用的是Sampling的形式。ide

[@more@]性能

性能比较:
当数据量比较小的时候,TOP N的速度要比Sample的速度快;
当数据量比较大的时候,Sample N的速度要比TOP的速度快。测试

测试:ui

Explain select top 10 * from PD_PORTAL.TOPIC_COMP_DETAILthis

1) First, we lock a distinct PD_PORTAL."pseudo table" for read on a
RowHash to prevent global deadlock for PD_PORTAL.TOPIC_COMP_DETAIL. 
2) Next, we lock PD_PORTAL.TOPIC_COMP_DETAIL for read. 
3) We do an all-AMPs STAT FUNCTION step from
PD_PORTAL.TOPIC_COMP_DETAIL by way of an all-rows scan with no
residual conditions into Spool 5, which is redistributed by hash
code to all AMPs. The result rows are put into Spool 1
(group_amps), which is built locally on the AMPs. This step is
used to retrieve the TOP 10 rows. One AMP is randomly selected to
retrieve 10 rows. If this step retrieves less than 10 rows, then
execute step 4. The size is estimated with low confidence to be
10 rows (27,460 bytes). 
4) We do an all-AMPs STAT FUNCTION step from
PD_PORTAL.TOPIC_COMP_DETAIL by way of an all-rows scan with no
residual conditions into Spool 5 (Last Use), which is
redistributed by hash code to all AMPs. The result rows are put
into Spool 1 (group_amps), which is built locally on the AMPs. 
This step is used to retrieve the TOP 10 rows. The size is
estimated with low confidence to be 10 rows (27,460 bytes). 
5) Finally, we send out an END TRANSACTION step to all AMPs involved
in processing the request.
-> The contents of Spool 1 are sent back to the user as the result of
statement 1..net

Explain select * from PD_PORTAL.TOPIC_COMP_DETAIL sample 10

1) First, we lock a distinct PD_PORTAL."pseudo table" for read on a RowHash to prevent global deadlock for PD_PORTAL.TOPIC_COMP_DETAIL.  2) Next, we lock PD_PORTAL.TOPIC_COMP_DETAIL for read.  3) We do an all-AMPs SAMPLING step from PD_PORTAL.TOPIC_COMP_DETAIL by way of an all-rows scan with no residual conditions into Spool 1 (group_amps), which is built locally on the AMPs. Samples are specified as a number of rows.  4) Finally, we send out an END TRANSACTION step to all AMPs involved in processing the request. -> The contents of Spool 1 are sent back to the user as the result of statement 1.

相关文章
相关标签/搜索