SQL 随机抽样的总结
对于SQL 随机抽样我们常想到的就是newid(),但如果对于一个在百万、千万甚至更大海量数据表中抽样的话,简单的newid(),其性能,效率就不是很理想了。所以在这里有必要讨论一下,择优而用。
long_goods是一个百万数据的表,Ctrl+L执行以下语句:
--id_index是我为主键加的一个非聚焦索引
SELECT top 1 * from long_goods order by newid()
--查询开销 43%
SELECT top 1 * from long_goods with(index=id_index) order by newid()
--查询开销 54%
select top 1 * from long_goods where id=(select top 1 id from long_goods order by newid())
--查询开销 1%
select top 1 * from long_goods where id=(select top 1 id from long_goods with(index=id_index) order by newid())
--查询开销 1%
虽然第三个与第四个的开销是一样,但实际应该是第四种优于第三种。
如果MS SQL2005升级到支持 TABLESAMPLE 的话,以下语句的抽样执行效率可为最优的
SELECT * from long_goods TABLESAMPLE SYSTEM (10 PERCENT)
呵呵..下班了,至于其中原因,下次有空再续.
相关文档:
NO.1
alter table pdt modify("PDTNAME",varchar2(100))
NO.2
字段不用“”
alter table pdt modify(PDTNAME,varchar2(100)) &n ......
这里使用的数据库和数据表分别来自Sql语句学习笔记(1)——创建数据库和Sql语句学习笔记(2)——创建数据表
use RetalDB
/********************
单表查询
*********************/
--查询tb_user中的数据:无条件查询
--(1)
select * from tb_user--查询所有信息
--(2)
select top 2 * from ......
新建表:
create table [表名]
(
[自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,
[字段1] nVarChar(50) default '默认值' null ,
[字段2] ntext null ,
[字段3] datetime,
[字段4] money null ,
[字段5] int default 0,
[字段6] Decimal (12,4) default 0,
[字段7] image null ,
)
删除表:
Drop table [表 ......
1.sql是一种脚本语言
2.sql可写脚本程序,但最重要的作用发挥在对数据库的操作上
3.sql server2005中的注释有两种/***/多行注释,--可以单行注释
4.sql中的自定义变量以@开头,系统变量以@@开头
5.sql中声明变量和VB类似,使用declare声明变量如:declare @i int,@j int,此语句声明了2个整形变量@i和@j
6.sql中的数据 ......
在PL/SQL中引入了控制结构,包括选择结构,循环结构和跳转结构
一 选择结构
1,IF 语句
在PL/SQL中,选择结构可以通过if语句来实现,也可以通过Case语句(oracle9i中)
利用if语句实现选择控制的语法为:
if condition1 then statements1 ......