powershell 压缩

背景 (Background)

We recently inherited a database environment where we’re facing significant data growth with limits on the sizes we can allow our databases to grow. Since we maintain multiple development, QA and production environments and these environments must be sized appropriately. We set a few standards about tables that exceed certain sizes – rows, data or both, or have certain growth patterns and our standards force compression at thresholds we set for our different environments (including using page, row, or clustered columnstore index compression) We’re looking for how we can get information about data and compression in tables and options we have so that we can quickly determine candidates that don’t currently match our best practices design we’ve set for our environments.

最近,我们继承了一个数据库环境,在该环境中,我们面临着显着的数据增长,但我们可以允许数据库增长的大小受到限制。 由于我们维护多个开发,因此质量保证和生产环境以及这些环境的大小必须适当调整。 我们针对超过特定大小的表(行,数据或两者,或具有一定增长模式)设置了一些标准,并且我们的标准将压缩的强度设置为针对不同环境设置的阈值(包括使用页面,行或集群列存储索引压缩)正在寻找如何在我们拥有的表和选项中获取有关数据和压缩的信息,以便我们能够快速确定当前不符合我们为环境设置的最佳实践设计的候选人。

讨论区 (Discussion)

In the below few images, we see a few properties of a table, such as the row count, the compression information, and the index storage. If we want to get information about one table and we manage one server, we could use the below properties to get this information. What if we manage hundreds of servers and want to get this information for a few tables, or a set of tables that are a part of a database?

在以下几张图像中,我们看到了表的一些属性,例如行数,压缩信息和索引存储。 如果我们要获取有关一个表的信息并管理一台服务器,则可以使用以下属性来获取此信息。 如果我们管理数百台服务器,并希望获取几个表或数据库中一部分表的信息,该怎么办?

We want to create a script that will return the information we need for any table based on the server and database that we pass to our function. In some cases, we may find tables without compression that are a significant amount of space, even though we seldom use them (even if we need the data). We may also discover tables with large space in index use relative to the size of the table; it’s unfortunately common for developers to add indexes to tables without considering that more indexes do not necessarily mean higher performance. We also may administer an environment with many developers and use this script to identify new objects that don’t conform to what we require in our environment.

我们想要创建一个脚本,该脚本将基于传递给函数的服务器和数据库返回任何表所需的信息。 在某些情况下,即使很少使用(即使我们需要数据),我们也可能会发现没有压缩的表会占用大量空间。 我们可能还会发现相对于表的大小,索引使用空间较大的表。 不幸的是,对于开发人员来说,在不考虑更多索引并不一定意味着更高性能的情况下向表中添加索引是很常见的。 我们也可能会与许多开发人员一起管理环境,并使用此脚本来识别与我们的环境要求不符的新对象。

We’ll be using the SQL management objects library (SMO library) for obtaining information for a table and we’ll begin our script by returning the row count before adding the option for other details. Returning table row counts can be used for many other purposes, like comparing two environments, so we’ll want the functionality to be separate if the use case calls for us to only use one feature in this function.

我们将使用SQL管理对象库(SMO库)获取表的信息,并且在添加其他详细信息选项之前,我们将通过返回行数来开始脚本。 返回表的行计数可以用于许多其他目的,例如比较两个环境,因此如果用例要求我们仅在此功能中使用一个功能,则我们希望功能是独立的。

Since the location of the SMO library varies by SQL Server version, we’ll first create a switch statement and a parameter that requires input from a set of values that we pre-determine. The below locations reflect where different SMO library versions are stored, and we may want to verify this with our setup as well. I also have the library version written out before it’s added, using the Add-Type function; it’s possible that we may not need this, so we can remove since it’s not necessary. During troubleshooting, it can be helpful to have.

由于SMO库的位置随SQL Server版本的不同而不同,因此我们将首先创建一个switch语句和一个参数,该参数需要从我们预先确定的一组值中进行输入。 以下位置反映了不同SMO库版本的存储位置,我们也可能要通过我们的设置进行验证。 我还使用Add-Type函数在添加之前将库版本写了出来。 有可能我们可能不需要它,所以我们可以删除它,因为它不是必需的。 在故障排除期间,进行故障排除会很有帮助。

Function Get-TableInfo {Param([ValidateSet("SQL Server 2008R2","SQL Server 2012","SQL Server 2014","SQL Server 2016")][string]$smodllversion)Process{switch ($smodllversion){"SQL Server 2008R2" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2012" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2014" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2016" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}}}
}Get-TableInfo -smodllversion

Provided that we enter the correct version in our function, we’ll be able to use this information to get the table information. If the highest version we have installed in 2014, we’ll want to make sure to select this in the dropdown option.

只要我们在函数中输入正确的版本,就可以使用此信息获取表信息。 如果是2014年安装的最高版本,我们将确保确保在下拉选项中选择此版本。

Since we want to get the row count, storage and compression information using PowerShell, we will use another switch statement with a set of options to allow users to choose what they want. If users want storage information, they’ll pass in “storage”, for row count, they’ll pass in “rowcount”, etc. Within each choice (the parameter name), we’ll have different functions to returning the information for the user.

由于我们要使用PowerShell获取行数,存储和压缩信息,因此我们将使用另一个带有一组选项的switch语句,以允许用户选择所需的内容。 如果用户需要存储信息,则将传递“存储”,进行行计数,将传递“行数”,依此类推。在每个选择(参数名称)中,我们将具有不同的功能来返回信息用户。

Function Get-TableInfo {Param([ValidateSet("SQL Server 2012","SQL Server 2014","SQL Server 2016")][string]$smodllversion, [ValidateSet("rowcount","storage","compression")][string]$choice)Process{switch ($smodllversion){"SQL Server 2012" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2014" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2016" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}}switch ($choice){"rowcount"{}"storage"{}"compression"{}}}
}

Next, we’ll add the connection information for our script and we’ll add the table information, since the row count, storage and compression information involve a table for this script. Since each detail is a part of a table, we’ll add the table information outside of our choice switch statement, rather than create the table object within each part of the switch statement. This is cleaner and more efficient and also allows us to return all information, if we choose to add that choice (the all option seen in the below script).

接下来,我们将添加脚本的连接信息,并添加表信息,因为行数,存储和压缩信息都涉及此脚本的表。 由于每个细节都是表的一部分,因此我们将表信息添加到选择switch语句之外,而不是在switch语句的每个部分中创建表对象。 如果我们选择添加该选项(在以下脚本中看到的all选项),则它更干净,更高效,并且还允许我们返回所有信息。

Function Get-TableInfo {Param([ValidateSet("SQL Server 2012","SQL Server 2014","SQL Server 2016")][string]$smodllversion, [ValidateSet("rowcount","storage","compression","all")][string]$choice, [Parameter(Mandatory=$true)]$server, [Parameter(Mandatory=$true)]$database, [Parameter(Mandatory=$true)]$table)Process{switch ($smodllversion){"SQL Server 2012" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\110\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2014" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\120\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}"SQL Server 2016" { Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server\130\SDK\Assemblies\Microsoft.SqlServer.Smo.dll"}}$smoscon = New-Object Microsoft.SqlServer.Management.SMO.Server($server)$gettableinfo = $smoscon.Databases["$database"].Tables["$table"]switch ($choice){"rowcount"{$gettableinfo.RowCount}"storage"{### The "SpaceUsed" reports in kilobytes, so we convert to megabytes($gettableinfo.DataSpaceUsed + $gettableinfo.IndexSpaceUsed)/(1024)}"compression"{$gettableinfo.HasClusteredColumnStoreIndex$gettableinfo.HasCompressedPartitions}"all"{$all += "Rowcount: " + $gettableinfo.RowCount + ", SpaceUsed: " + ($gettableinfo.DataSpaceUsed + $gettableinfo.IndexSpaceUsed)/(1024) + ", ClusteredColumnStoreIndex: " + $gettableinfo.HasClusteredColumnStoreIndex + ", CompressedPartitions: " + $gettableinfo.HasCompressedPartitions$all}}}
}Get-TableInfo -smodllversion 'SQL Server 2014' -choice all -server "OurServer" -database "OurDatabase" -table "OurTable"

With the script, we can select our choice and experiment with tables. When we return the row count information, this mirrors checking the row count properties of a table within the interface. A few DBAs and developers debate about the best approach of checking row counts, and this varies based on use case. In most cases, when I check a row count, I do not want to interrupt data flow. However, this method may be inappropriate if you must know the exact count and prefer a “hard count” query. The space information combines the total space of a table by aggregating the data and index space. According to Microsoft, both of these space properties within the table class report details in kilobytes, so in the script we convert this to megabytes. If we’re in larger data environments, we can convert to an appropriate measure such as megabytes, gigabytes, petabytes, etc. Finally, the compression information reports whether we have a clustered columnstore index (which uses compression) and (or) whether we have compressed partitions.

使用脚本,我们可以选择我们的选择并尝试使用表。 当我们返回行计数信息时,这反映了检查接口中表的行计数属性。 一些DBA和开发人员就检查行计数的最佳方法争论不休,并且这取决于用例。 在大多数情况下,当我检查行计数时,我不想中断数据流。 但是,如果您必须知道确切的计数并希望使用“硬计数”查询,则此方法可能不合适。 空间信息通过汇总数据和索引空间来合并表的总空间。 根据Microsoft的说法,表类中的这两个空间属性均以千字节为单位报告详细信息,因此在脚本中,我们将其转换为兆字节。 如果我们在较大的数据环境中,我们可以转换为适当的度量,例如兆字节,千兆字节,PB等。最后,压缩信息报告我们是否有集群的列存储索引(使用压缩)和(或)是否有具有压缩分区。

Since we may want all this information, we can add one more piece to our switch statement:

由于我们可能需要所有这些信息,因此我们可以在switch语句中再添加一块:

"all"
{$all += "Rowcount: " + $gettableinfo.RowCount + ", SpaceUsed: " + ($gettableinfo.DataSpaceUsed + $gettableinfo.IndexSpaceUsed)/(1024) + ", ClusteredColumnStoreIndex: " + $gettableinfo.HasClusteredColumnStoreIndex + ", CompressedPartitions: " + $gettableinfo.HasCompressedPartitions$all
}

If we want to return everything from our script, we can add one more option in our choice parameter – all – and calling this will return all the information we have about the table. In the below example, we look at a couple of tables in the returned output:

如果我们想回到一切从我们的脚本,我们可以在我们选择的参数添加多一个选择- 所有 -和调用这个将返回所有我们对表中的信息。 在下面的示例中,我们在返回的输出中查看几个表:

While this script will return the information, if we want to take it a step further, we can retain this information in files or within a table by adding the output to a file or SQL insert statement. Getting information with a service account or SQL user with lower level permissions is one thing, though as we do more – such as save information, we’ll need the account executing the script to have more permissions.

虽然此脚本将返回信息,但是如果我们想更进一步,则可以通过将输出添加到文件或SQL插入语句中来将这些信息保留在文件中或表中。 使用服务帐户或具有较低权限SQL用户获取信息是一回事,尽管随着我们进行更多操作(例如保存信息),我们将需要执行脚本的帐户具有更多权限。

最后的想法 (Final thoughts)

In this tip, we looked at a PowerShell script which can return the row count, space used, and compression information about a table – with the option of also returning all these details. Depending on the standards we set along with the thresholds, we can get the information that we need quickly and determine the next course of action, or add that course of action within the script with additional permissions.

在本技巧中,我们研究了一个PowerShell脚本,该脚本可以返回有关表的行数,已用空间和压缩信息-还可以选择返回所有这些详细信息。 根据我们设置的标准以及阈值,我们可以快速获取所需的信息并确定下一个操作过程,或者在脚本中使用其他权限添加该操作过程。

翻译自: https://www.sqlshack.com/return-data-use-index-compression-row-information-powershell/

powershell 压缩

powershell 压缩_如何使用PowerShell返回数据使用,索引压缩和行信息相关推荐

  1. 利用lz78编码实现对某字符序列的二元压缩_多媒体笔记(1):文件压缩的原理...

    多媒体(multimedia) 融合两种或两种以上媒体的人机互动的信息交流和传播媒体. 超文本(hypertext) 包含指向其他文档或文档元素的指针的电子文档.传统文本以线性方式组织,超文本以非线性 ...

  2. powershell 编程_如何使用PowerShell以编程方式更改Visual Studio中的默认浏览器,并可能使自己陷入困境...

    powershell 编程 UPDATE: Why my own MacGyver solution was brilliant in its horrible way, the folks over ...

  3. powershell 使用_如何使用PowerShell生成随机名称和电话号码

    powershell 使用 When you need a data set for testing or demonstration, and that set needs to represent ...

  4. powershell 使用_如何使用PowerShell映射网络驱动器

    powershell 使用 In years past, automating network drive creation required the use of primitive batch f ...

  5. java flash 压缩_Java和flash通信中数据的zlib压缩与解压缩

    由于as3的bytearray支持compress和uncompress.所以我们可以在Java端将数据压缩,然后在flash端读取再解压缩,这样数据在传输过程中又会小很多. 下面就介绍使用方法,基于 ...

  6. java 文本压缩_[Java基础]Java使用GZIP进行文本压缩

    import java.io.IOException; import java.util.zip.GZIPOutputStream; import org.apache.commons.io.outp ...

  7. 橡胶柱压缩_你玩俄罗斯轮盘吗?剪切安全气囊压缩气瓶的隐患及注意事项

    剪切气囊压缩气瓶的隐患 大约 16 年前,当我开始在消防部门工作时,我们了解到:不要剪切安全气囊的压缩气瓶,不要靠近安全气囊的撞击区域,因为它们会让你丧命.那时,我只是接受了这一事实,从未想过它们为何 ...

  8. python分割压缩_【python 多线程】下载文件分批压缩

    大体做的功能为: 1.调用api接口,获取json数据:2.json 数据转换为一个csv文件:3.csv文件里的每行数据转换成单个xml文件:4.每5个xml文件进行打包 用到的模块为: csv,x ...

  9. 毫秒级返回数据,TDengine 在大疆车载智能驾驶云端平台上的应用

    小 T 导读:为了满足智能驾驶业务的数据处理需求,大疆车载试图从多家数据库中进行选型调研,帮助智能驾驶业务提升写入查询性能.降低运维成本.本文将分享大疆车载在数据库选型.系统搭建和业务迁移等方面的经验 ...

最新文章

  1. 【GNN】硬核!一文梳理经典图网络模型
  2. boost::math::barycentric_rational相关用法的测试程序
  3. django时间问题和时区设置
  4. html调用python_flask之模板html中调用python函数方法
  5. 主机屋linux怎么连,全网最详细的samba文件共享服务!
  6. iText in Action 2nd5.2节(Events for basic building blocks)读书笔记
  7. 大数据_Flink_数据处理_yarn和k8s平台的flink部署---Flink工作笔记0015
  8. 【亲测有效】Ubuntu18.04 sudo apt update无法解析域名的解决方案
  9. Scala学习数组/映射/元组
  10. 共享计算机桌面,DeskTopShare桌面屏幕共享软件
  11. Cisco ASDM-IDM 安装配置for Windows10
  12. vue源码 - 调试vue初始化过程
  13. Android WiFi开发总结
  14. python求15 17 23 65 97的因数_Python练习题
  15. 计算机专业考研复试流程,给未来的程序猿:2019计算机专业考研复试基本流程...
  16. ubuntu 18.04 下安装微信
  17. python名片管理系统难点总结_Python 知识要点:名片管理系统 2.0
  18. java.lang.IllegalArgumentException的问题解决
  19. 蛮力法(Brute Force)
  20. Python教程:函数多个返回值与丢弃返回值

热门文章

  1. 【LGP5161】WD与数列
  2. 敏捷个人课后练习五主题:改变
  3. 《网络攻防》第五周作业
  4. VS中的预先生成事件和后期生成事件
  5. Centos系统创建用户oracle后,用该用户登陆系统,页面加载报错GConf error
  6. POJ 1166 The Clocks (爆搜 || 高斯消元)
  7. Python标准库-string模块《未完待续》
  8. matlab 指定函数拟合,matlab中曲线拟合问题---使用指定函数进行曲线拟合
  9. 地推主管需要跑业务吗
  10. 父亲购房后去世,房产证还没办,后妈和父亲没结婚,户口簿上有父亲,儿子,妹妹,那房产证应该写谁的名字?