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)
呵呵..下班了,至于其中原因,下次有空再续.
相关文档:
新建表:
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 [表 ......
一、问题引出:
当某一台SQL Server实例上创建有多个数据库的时候,如果某一个数据库过度占有系统资源,往往会导致其它数据库性能受到严重影响,甚至大大降低整个数据库的性能。
这种情况下,如果有一种技术或者方法可以限制数据库资源的占有量,给与一个峰值,从而给与其它的数据库足够的资源。那么这个问题就可 ......
例如:
普遍的SQL语句:
update book set bookname='sssss' where bookId=1;
在PL/SQL 中执行:
declare
v_book ......
纵表转横表的"SQL"示例:
纵表结构:
fname ftype fvalue
小乔 zaocan 10
小乔 zhongcan 20
小乔 wancan 5
转换后的表结构:
fname zaocan_value zhongcan_value wancan_value
小乔 10 20 5
纵表转横表SQL示例:
select Fname, sum(case ......