查询数据量的语句

FIR1987 2009-12-23 08:02:46
这是一个统计数据库中有多少数据量的语句
中间有些过程没有看懂 哪位高人请指点一下 谢谢~
set nocount on
if object_id(N'tempdb.db.#temp') is not null
drop table #temp
create table #temp (name sysname,count numeric(18))

insert into #temp
select o.name,i.rows
from sysobjects o,sysindexes i
where o.id=i.id and o.Xtype='U' and i.indid<2

select count(count) 总表数,sum(count) 总记录数 from #temp
select * from #temp
set nocount off

...全文
156 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
FIR1987 2010-01-03
  • 打赏
  • 举报
回复
<2是啥意思?
[Quote=引用 4 楼 terry717 的回复:]
insert into #temp

select o.name,i.rows
from sysobjects o,sysindexes i
where o.id=i.id and o.Xtype='U' and i.indid <2
这句是查询系统表sysobjects 和 sysindexes 关联的数据,

然后插入到 临时表#temp 中

[/Quote]
claro 2009-12-27
  • 打赏
  • 举报
回复
[SQL code]

--查询当前数据库的数据量计数,按表分组
use --数据库
select max(a.rowcnt) 计数,b.[name] 表名
from sysindexes a
join sys.objects b on b.object_id=a.id
where b.type='U'
group by b.[name]
having max(a.rowcnt) >0
order by 1 desc
[/SQL code]
claro 2009-12-27
  • 打赏
  • 举报
回复
[Quote =SQL code]

--查询当前数据库的数据量计数,按表分组
use --数据库
select max(a.rowcnt) 计数,b.[name] 表名
from sysindexes a
join sys.objects b on b.object_id=a.id
where b.type='U'
group by b.[name]
having max(a.rowcnt) >0
order by 1 desc
[/Quote]
claro 2009-12-27
  • 打赏
  • 举报
回复
这样看吧:
[Quote]
SQL code
--查询当前数据库的数据量计数,按表分组
use --数据库
select max(a.rowcnt) 计数,b.[name] 表名
from sysindexes a
join sys.objects b on b.object_id=a.id
where b.type='U'
group by b.[name]
having max(a.rowcnt) >0
order by 1 desc
[/Quote]
dawugui 2009-12-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fir1987 的回复:]
exec sp_executesql @sql , N'@a int output' , @totalCounts output
这句存储过程 后面是什么意思呢?
[/Quote]

sp_executesql
执行可以多次重用或动态生成的 Transact-SQL 语句或批处理。Transact-SQL 语句或批处理可以包含嵌入参数。

语法
sp_executesql [@stmt =] stmt
[
{, [@params =] N'@parameter_name data_type [,...n]' }
{, [@param1 =] 'value1' [,...n] }
]

参数
[@stmt =] stmt

包含 Transact-SQL 语句或批处理的 Unicode 字符串,stmt 必须是可以隐式转换为 ntext 的 Unicode 常量或变量。不允许使用更复杂的 Unicode 表达式(例如使用 + 运算符串联两个字符串)。不允许使用字符常量。如果指定常量,则必须使用 N 作为前缀。例如,Unicode 常量 N'sp_who' 是有效的,但是字符常量 'sp_who' 则无效。字符串的大小仅受可用数据库服务器内存限制。

stmt 可以包含与变量名形式相同的参数,例如:

N'SELECT * FROM Employees WHERE EmployeeID = @IDParameter'

stmt 中包含的每个参数在 @params 参数定义列表和参数值列表中均必须有对应项。

[@params =] N'@parameter_name data_type [,...n]'

字符串,其中包含已嵌入到 stmt 中的所有参数的定义。该字符串必须是可以隐式转换为 ntext 的 Unicode 常量或变量。每个参数定义均由参数名和数据类型组成。n 是表明附加参数定义的占位符。stmt 中指定的每个参数都必须在 @params 中定义。如果 stmt 中的 Transact-SQL 语句或批处理不包含参数,则不需要 @params。该参数的默认值为 NULL。

[@param1 =] 'value1'

参数字符串中定义的第一个参数的值。该值可以是常量或变量。必须为 stmt 中包含的每个参数提供参数值。如果 stmt 中包含的 Transact-SQL 语句或批处理没有参数,则不需要值。

n

附加参数的值的占位符。这些值只能是常量或变量,而不能是更复杂的表达式,例如函数或使用运算符生成的表达式。

返回代码值
0(成功)或 1(失败)

结果集
从生成 SQL 字符串的所有 SQL 语句返回结果集。

注释
在批处理、名称作用域和数据库上下文方面,sp_executesql 与 EXECUTE 的行为相同。sp_executesql stmt 参数中的 Transact-SQL 语句或批处理在执行 sp_executesql 语句时才编译。然后编译 stmt 中的内容并作为执行计划运行(独立于名为 sp_executesql 的批处理的执行计划)。sp_executesql 批处理不能引用调用 sp_executesql 的批处理中声明的变量。sp_executesql 批处理中的本地游标和变量对调用 sp_executesql 的批处理是不可见的。对数据库上下文所作的更改只在 sp_executesql 语句结束前有效。

如果只更改了语句中的参数值,则 sp_executesql 可用来代替存储过程多次执行 Transact-SQL 语句。因为 Transact-SQL 语句本身保持不变仅参数值变化,所以 Microsoft® SQL Server™ 查询优化器可能重复使用首次执行时所生成的执行计划。



说明 如果语句字符串中的对象名不是全限定名,则该执行计划不会被重用。


sp_executesql 支持与 Transact-SQL 字符串相独立的参数值的设置:

DECLARE @IntVariable INT
DECLARE @SQLString NVARCHAR(500)
DECLARE @ParmDefinition NVARCHAR(500)

/* Build the SQL string once.*/
SET @SQLString =
N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level'
SET @ParmDefinition = N'@level tinyint'
/* Execute the string with the first parameter value. */
SET @IntVariable = 35
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable
/* Execute the same string with the second parameter value. */
SET @IntVariable = 32
EXECUTE sp_executesql @SQLString, @ParmDefinition,
@level = @IntVariable

替换 sp_executesql 中的参数的能力,与使用 EXECUTE 语句执行字符串相比,有下列优点:

因为在 sp_executesql 中,Transact-SQL 语句的实际文本在两次执行之间未改变,所以查询优化器应该能将第二次执行中的 Transact-SQL 语句与第一次执行时生成的执行计划匹配。这样,SQL Server 不必编译第二条语句。


Transact-SQL 字符串只生成一次。


整型参数按其本身格式指定。不需要转换为 Unicode。
权限
执行权限默认授予 public 角色。

示例
A. 执行简单的 SELECT 语句
下面的示例创建并执行一个简单的 SELECT 语句,其中包含名为 @level 的嵌入参数。

execute sp_executesql
N'select * from pubs.dbo.employee where job_lvl = @level',
N'@level tinyint',
@level = 35

B. 执行动态生成的字符串
下面的示例显示使用 sp_executesql 执行动态生成的字符串。该示例中的存储过程用来向一组表中插入数据,该表用于划分一年的销售数据。一年中的每个月均有一个表,格式如下:

CREATE TABLE May1998Sales
(OrderID INT PRIMARY KEY,
CustomerID INT NOT NULL,
OrderDate DATETIME NULL
CHECK (DATEPART(yy, OrderDate) = 1998),
OrderMonth INT
CHECK (OrderMonth = 5),
DeliveryDate DATETIME NULL,
CHECK (DATEPART(mm, OrderDate) = OrderMonth)
)

有关从这些分区表中检索数据的更多信息,请参见使用包含分区数据的视图。

每个表的名称由月份名的前三个字母、年度的四位数字和常量 Sales 组成。名称可以从订单日期动态生成:

/* Get the first three characters of the month name. */
SUBSTRING( DATENAME(mm, @PrmOrderDate), 1, 3) +
/* Concatenate the four-digit year; cast as character. */
CAST(DATEPART(yy, @PrmOrderDate) AS CHAR(4) ) +
/* Concatenate the constant 'Sales'. */
'Sales'

下面示例中的存储过程动态生成并执行一个 INSERT 语句,向适当的表中插入新订单。该存储过程使用订单日期生成应包含数据的表的名称,然后将名称并入 INSERT 语句。(这是 sp_executesql 的一个简单示例。不包含错误检查,也不包括业务规则检查,例如确保两个表之间订单号没有重复。)

CREATE PROCEDURE InsertSales @PrmOrderID INT, @PrmCustomerID INT,
@PrmOrderDate DATETIME, @PrmDeliveryDate DATETIME
AS
DECLARE @InsertString NVARCHAR(500)
DECLARE @OrderMonth INT

-- Build the INSERT statement.
SET @InsertString = 'INSERT INTO ' +
/* Build the name of the table. */
SUBSTRING( DATENAME(mm, @PrmOrderDate), 1, 3) +
CAST(DATEPART(yy, @PrmOrderDate) AS CHAR(4) ) +
'Sales' +
/* Build a VALUES clause. */
' VALUES (@InsOrderID, @InsCustID, @InsOrdDate,' +
' @InsOrdMonth, @InsDelDate)'

/* Set the value to use for the order month because
functions are not allowed in the sp_executesql parameter
list. */
SET @OrderMonth = DATEPART(mm, @PrmOrderDate)

EXEC sp_executesql @InsertString,
N'@InsOrderID INT, @InsCustID INT, @InsOrdDate DATETIME,
@InsOrdMonth INT, @InsDelDate DATETIME',
@PrmOrderID, @PrmCustomerID, @PrmOrderDate,
@OrderMonth, @PrmDeliveryDate

GO

在该过程中使用 sp_executesql 比使用 EXECUTE 执行字符串更有效。使用 sp_executesql 时,只生成 12 个版本的 INSERT 字符串,每个月的表 1 个。使用 EXECUTE 时,因为参数值不同,每个 INSERT 字符串均是唯一的。尽管两种方法生成的批处理数相同,但因为 sp_executesql 生成的 INSERT 字符串相似,所以查询优化程序更有可能反复使用执行计划。

FIR1987 2009-12-27
  • 打赏
  • 举报
回复
exec sp_executesql @sql , N'@a int output' , @totalCounts output
这句存储过程 后面是什么意思呢?

[Quote=引用 3 楼 dawugui 的回复:]
SQL code动态获取数据库表记录总数--以sql 2000自带pubs的authors表为例declare@totalCountsintdeclare@sqlnvarchar(4000)declare@tblnamevarchar(10)set@tblname='authors'declare@pageSizeasintset@pageSize=2declare@pageCountsasintset@sql='select @a = count(*) from'+@tblnameexec sp_executesql@sql , N'@a int output' ,@totalCounts outputset@pageCounts=(@totalCounts/@pageSize)select@totalCounts,@pageCounts/*

----------- -----------
23 11

(所影响的行数为 1 行)*/

SQL code--获取库中所有表及该表的记录数declare@sqlvarchar(8000)set@sql='select * from ('select@sql=@sql+' select name ='''+ name+''' , count(*) as num from ['+name+'] union all'from sysobjectswhere xtype='u'set@sql=left(@sql,len(@sql)-10)+')a'exec(@sql)/*
--sql server 2000 自带库 pubs 的结果
name num
----------- -----------
titleauthor 25
stores 6
sales 21
roysched 86
discounts 3
jobs 14
pub_info 8
employee 43
authors 23
publishers 8
titles 18*/
[/Quote]
Terry717 2009-12-24
  • 打赏
  • 举报
回复
insert into #temp

select o.name,i.rows
from sysobjects o,sysindexes i
where o.id=i.id and o.Xtype='U' and i.indid <2
这句是查询系统表sysobjects 和 sysindexes 关联的数据,

然后插入到 临时表#temp 中
dawugui 2009-12-23
  • 打赏
  • 举报
回复
动态获取数据库表记录总数

--以sql 2000自带pubs的authors表为例

declare @totalCounts int
declare @sql nvarchar(4000)
declare @tblname varchar(10)
set @tblname = 'authors'
declare @pageSize as int
set @pageSize = 2
declare @pageCounts as int

set @sql = 'select @a = count(*) from ' + @tblname
exec sp_executesql @sql , N'@a int output' , @totalCounts output

set @pageCounts=(@totalCounts/@pageSize)

select @totalCounts,@pageCounts


/*

----------- -----------
23 11

(所影响的行数为 1 行)
*/


--获取库中所有表及该表的记录数

declare @sql varchar(8000)
set @sql='select * from ('
select @sql=@sql+' select name = ''' + name + ''' , count(*) as num from ['+name+'] union all ' from sysobjects where xtype='u'
set @sql = left(@sql,len(@sql) - 10) + ')a'
exec(@sql)

/*
--sql server 2000 自带库 pubs 的结果
name num
----------- -----------
titleauthor 25
stores 6
sales 21
roysched 86
discounts 3
jobs 14
pub_info 8
employee 43
authors 23
publishers 8
titles 18
*/
FIR1987 2009-12-23
  • 打赏
  • 举报
回复
sysobjects o,sysindexes i 我知道 这里用了索引 那能不能详细的解释下 我没看懂这个 谢谢了~
bancxc 2009-12-23
  • 打赏
  • 举报
回复
要是没有索引的能查出来吗
版本:presto-server-0.214.tar软件版本 presto-cli-0.214-executableCentOS71、presto的起因 hadoop ---hdfs----MR(java)-----hivehive底层原理用MR,速度比较慢,公司hadoop集群主要集中于晚上到凌晨,平日工作时间负载不是很高。但在工作时间内,公司业务人员有实时查询的需求,现在主要借助于hive提供业务人员的查询。hive是基于MR类的SQL查询工具,他会输入的查询SQL解析为MapReduce,能极大的降低使用大数据门槛,让一般的业务人员可以直接准对大数据进行查询,但是有一个利弊,它的查询基于MR,会让人等待比较着急,等待的时间可能是几个小时或者一天。 spark基于内存提高改良的hive,sql,现在factbook在hive上面开发一套利器,准对hive可以通过sql语句快速查询,presto。2、Facebook为何开发Presto  Facebook的2011的数据仓库存储在少量大型hadoopfs集群,Hive是FaceBook在几年前专门为Hadoop打造的一款数据仓库工具,在以前,facebook的科学家和分析师一直靠hive进行数据分析.但hive使用MR作为底层计算框架,是专为批处理设计的,但是随着数据的不断增多,使用hive进行一个简单的数据查询可能要花费分钟或者几个小时,显然不能满足查询需求,FaceBooke也调研了其他比hive更快的工具,但是他们需要在功能有限的条件下做简单操作,以至于无法操作Facebook庞大的数据要求。2012年开始研究自己的框架--presto,每日可以超过1pb查询,而且速度比较快,faceBook声称Presto的性能比hive要好上10倍或者100倍,presto和hive都是facebook开发的 Presto是一个开源的分布式SQL查询引擎,适用于交互式查询数据量支持GB到PB字节。Presto的设计和编写完全是为了解决Facebook这样规模的商业数据仓库交互式分析和处理速度的问题Presto可以做什么 Presto支持在线数据查询,包括Hive kafka Cassandra关系数据库以及专门数据存储,一条Presto查询可以将多个数据源进行合并,可以跨越整个组织进行分析。Presto以分析师的需求作为目标,他们期望相应速度小于1秒到几分钟,Presto要么在使用速度的快的昂贵的商业方案,提高内存,要么是消耗大量的硬件进行快速查询。(128G 64G)本套课程教给如何在企业环境中使用Presto技术。

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧