快速读取总数据量的另一种方法:sys.partitions

本文介绍了如何通过执行sp_MSforeachtable和sys.partitions获取数据库中所有表的大约行数,以满足快速查看数据量的需求。示例中展示了在事务未提交情况下,sys.partitions的rows列仍能显示未提交行数的特点,并提供了一个简单查询语句来实现这一功能。
部署运行你感兴趣的模型镜像

在我上一篇博客中提到了通过系统表拼接SQL语句,读取数据库中所有表定制数据量的方法,在回帖中,沟沟同学提到了另一种方法:

Exec sp_MSforeachtable@precommand="Createtable ##(name sysname,rowsint,reservednvarchar(20),datanvarchar(20),index_sizenvarchar(20),unusednvarchar(20))",@command1="insertinto ##exec sp_spaceused'?'",@postcommand="select*from ##"droptable ##


下面就来分析一下这种方法。


这个语句是先创建全局临时表,再遍历所有表,

执行sp_spaceused,将返回的行数和占用空间插入临时表中,最后查询表并删除表。


可以知道读取数据量的关键部分在于sp_spaceused存储过程。

这个存储过程中的表行数是从哪来的呢?它是从sys.partitions系统表中的rows列中来的。


在联机丛书中,对rows列的描述是“此分区中的大约行数”,

那么这个“大约”是虾米意思呢?

其实就是未提交的行数也会在这个表中显示。

下面就以一个简单的例子来演示一下:


1.创建一个测试表,插入3行数据,和2行未提交的数据

IF OBJECT_ID('TB') IS NOT NULL DROP TABLE TB
GO
CREATE TABLE TB(COL VARCHAR(50))
INSERT INTO TB
SELECT 'A' UNION ALL
SELECT 'B' UNION ALL
SELECT 'C'
GO

BEGIN TRAN

INSERT INTO TB
SELECT 'D' UNION ALL
SELECT 'E'

--ROLLBACK TRAN

2.新开一个窗口,运行下面三句

SELECT * FROM TB WITH(NOLOCK)

exec SP_SPACEUSED 'TB'

SELECT ROWS
FROM SYS.PARTITIONS
where OBJECT_NAME(OBJECT_ID)='TB'

可以看到,返回的行数都是5。

如果把第一句的WITH(NOLOCK)去掉,会读不出数量,因为第一个会话的事务没提交。


3.回到第一个窗口,运行注释里的ROLLBACK TRAN回滚事务,再运行新窗口的三个查询,可以看到数量又回到3了。


因此,对于想要快速看到表中大概数据量的需求,可以使用下面的语句:

SELECT OBJECT_NAME(OBJECT_ID),ROWS
FROM SYS.PARTITIONS
ORDER BY OBJECT_NAME(OBJECT_ID)

对于只要看总数量,不需要太准确的同学来说,此语句足够了。

您可能感兴趣的与本文相关的镜像

Dify

Dify

AI应用
Agent编排

Dify 是一款开源的大语言模型(LLM)应用开发平台,它结合了 后端即服务(Backend as a Service) 和LLMOps 的理念,让开发者能快速、高效地构建和部署生产级的生成式AI应用。 它提供了包含模型兼容支持、Prompt 编排界面、RAG 引擎、Agent 框架、工作流编排等核心技术栈,并且提供了易用的界面和API,让技术和非技术人员都能参与到AI应用的开发过程中

请解释一下我提供的lua代码:debug.print("备份extern分区") --backup file = AES_ENC(MD5(product_name)+AES_ENC(COMPRESS(user_config_xml_file))) local product_info_md5 = md5_product_info() local product_info_md5_file = io.open("/tmp/product_info_md5_file", "w") local agile_support = uci_r:get_profile("agile_config", "support") or "no" for num in string.gmatch(product_info_md5, "%x%x") do local number = "0x"..num product_info_md5_file:write(string.char(number)) end product_info_md5_file:close() -- 备份extern分区, 如有特殊情况的分区,再特殊处理 local extern_partitions = uci_r:get_profile("backup_restore", "extern_partition") or nil if extern_partitions ~= nil then extern_partitions = util.split(extern_partitions, " ") os.execute("mkdir /tmp/backup >/dev/null 2>&1") for i, v in ipairs(extern_partitions) do if v ~= nil then debug("-----------------------backup :" .. v) local externname = "/tmp/backup/ori-backup-" .. v .. ".bin" luci.sys.exec("nvrammanager -r " .. externname .. " -p " .. v .. " >/dev/null 2>&1") local filesize = fs.stat(externname).size if (v == 'router-config' or v == 'ap-config' or v == 'repeater-config' or v == 'multissid-config' or v == 'client-config') and filesize > 0 then hide_info(externname, username) end end end luci.sys.exec("nvrammanager -r /tmp/backup/ori-backup-user-config.bin -p user-config >/dev/null 2>&1") -- don't need to hide account info as it will be ignored in store procedue... if agile_support == "no" then hide_info("/tmp/backup/ori-backup-user-config.bin", username) else hide_common_info("/tmp/backup/ori-backup-user-config.bin") end hide_rsa2048_info("/tmp/backup/ori-backup-user-config.bin") --打包 os.execute("tar -cf /tmp/ori-backup-userconf.bin -C /tmp/backup . >/dev/null 2>&1") luci.sys.exec("rm -rf /tmp/backup >/dev/null 2>&1") else luci.sys.exec("nvrammanager -r /tmp/ori-backup-userconf.bin -p user-config >/dev/null 2>&1") cry.dec_file_entry("/tmp/ori-backup-userconf.bin", "/tmp/tmp-backup-userconf.xml") luci.sys.exec("mkdir -p /tmp/backupcfg") configtool.xmlToFile("/tmp/tmp-backup-userconf.xml", "/tmp/backupcfg") -- hide cloud info config local hide_files = {"accountmgnt", "cloud_config"} if username == "superadmin" then hide_files = {"cloud_config"} end for _, f in ipairs(hide_files) do luci.sys.exec("rm -f /tmp/backupcfg/config/" .. f) end -- recreate xml config files luci.sys.exec("rm -f /tmp/ori-backup-userconf.bin;rm -f /tmp/tmp-backup-userconf.xml") configtool.convertFileToXml("/tmp/backupcfg/config", "/tmp/tmp-backup-userconf.xml") cry.enc_file_entry("/tmp/tmp-backup-userconf.xml", "/tmp/ori-backup-userconf.bin") luci.sys.exec("rm -rf /tmp/backupcfg;rm -f /tmp/tmp-backup-userconf.xml") end luci.sys.exec("cat /tmp/product_info_md5_file /tmp/ori-backup-userconf.bin > /tmp/mid-backup-userconf.bin") cry.enc_file_entry("/tmp/mid-backup-userconf.bin", "/tmp/save-backup-userconf.bin") local feedback = {http_form} if feedback[1] == true then local reader = require("io").popen("cat /tmp/save-backup-userconf.bin") luci.sys.exec("rm -f /tmp/save-backup-userconf.bin; rm -f /tmp/product_info_md5_file; rm -f /tmp/mid-backup-userconf.bin; rm -f /tmp/ori-backup-userconf.bin") uci_r:set("accountmgnt", "login", "logining", logining) uci_r:commit("accountmgnt") return reader else local reader = sys.ltn12_popen("cat /tmp/save-backup-userconf.bin") if agile_support == "no" then luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.bin"' % {configtool.getsysinfo("product_name"), os.date("%Y-%m-%d")}) else --support agile_config local agileconfig_filename = agile_config_filename(true) local productname = util.execl("getfirm HOSTNAME_NO_BLANK") if productname then debug.printf("[ag]agileconfig_filename: " .. productname[1] .. agileconfig_filename) else debug.printf("[ag]getfirm HOSTNAME_NO_BLANK failed!") uci_r:set("accountmgnt", "login", "logining", logining) uci_r:commit("accountmgnt") return false end local TPLink = uci_r:get_profile("tplink","TPLINK_TPLink") or "yes" if TPLink == "no" then luci.http.header('Content-Disposition', 'attachment; filename="%s%sn_agc3031.bin"' % { productname[1], agileconfig_filename}) else luci.http.header('Content-Disposition', 'attachment; filename="%s%sn.bin"' % { productname[1], agileconfig_filename}) end end luci.http.prepare_content("application/octet-stream") luci.ltn12.pump.all(reader, luci.http.write) luci.sys.exec("rm -f /tmp/save-backup-userconf.bin; rm -f /tmp/product_info_md5_file; rm -f /tmp/mid-backup-userconf.bin; rm -f /tmp/ori-backup-userconf.bin") end
最新发布
09-25
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值