简体   繁体   English

是否有任何程序会缩小sql脚本文件的大小?

[英]Are there any programs that will shrink the size of a sql script file?

I have a SQL script which is extremely large (about 700 megabytes). 我有一个非常大的SQL脚本(大约700兆字节)。 I am wondering if there is a good way to reduce the size of the script? 我想知道是否有一种减少脚本大小的好方法?

I know there are code minimizers for JavaScript and am looking for one to use with SQL scripts. 我知道有JavaScript的代码最小化器,我正在寻找一个用于SQL脚本的代码。

I am not looking to get performance on the SQL script. 我不希望在SQL脚本上获得性能。 I am trying to make the file size smaller. 我试图让文件更小。 Removing excess whitespace. 删除多余的空格。 Keeping name-qualification down so that the script file sizes can be smaller. 保持名称限定,以便脚本文件大小可以更小。

If I attempt to load the file in SQL Server Management Studio I get this error. 如果我尝试在SQL Server Management Studio中加载该文件,我会收到此错误。

Not enough storage is available to process this command. 没有足够的存储空间来处理此命令。 (Exception from HRESULT: 0x80070008) (mscorlib) (HRESULT异常:0x80070008)(mscorlib)

What about breaking your script into several small files, and calling those files from a single master script? 如何将脚本分成几个小文件,并从单个主脚本调用这些文件?

This link describes how to do it from a stored procedure. 此链接描述了如何从存储过程执行此操作。

Or you can do it from a batch file like this: 或者您可以从这样的批处理文件中执行此操作:

REM =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
REM Define widely-used variables up here so they can be changed in one place
REM Search for "sqlcmd.exe" and make sure this path is valid for you
REM =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
set sqlcmd="C:\Program Files\Microsoft SQL Server\100\Tools\Binn\sqlcmd.exe"
set uname="your_uname_here"
set pwd="your_pwd_here"
set database="your_db_name_here"
set server="your_server_name_here"

%sqlcmd% -S %server% -d %database% -U %uname% -P %pwd% -i "c:\script1.sql"
%sqlcmd% -S %server% -d %database% -U %uname% -P %pwd% -i "c:\script2.sql"
%sqlcmd% -S %server% -d %database% -U %uname% -P %pwd% -i "c:\script3.sql"

pause

I like the batch file approach myself, because it is easier to tinker with it, and you can schedule it as a windows job. 我自己喜欢批处理文件方法,因为它更容易修改它,你可以将它安排为Windows作业。

Make sure the .BAT file is in a folder with the appropriate security restrictions, since it has your credentials in a plain text .BAT file. 确保.BAT文件位于具有相应安全限制的文件夹中,因为它具有纯文本.BAT文件中的凭据。

What's in this script of 700MB?! 这个700MB的脚本是什么?! I would hope that there are some similarities/repetitions that would allow it to shorten the file. 我希望有一些相似之处/重复可以缩短文件。

Just some guesses: 只是一些猜测:

  • Instead of inserting a million records using Insert statements, use a bulk loading tool 不使用Insert语句插入一百万条记录,而是使用批量加载工具
  • Instead of updating a number of individual records, try to batch updates to the same value into one (eg Update tab set col=1 where id in (..) instead of individual updates) 不要更新多个单独的记录,而是尝试将相同值的批量更新为一个(例如,更新选项卡设置col = 1,其中id为(..)而不是单个更新)
  • long manipulations can be defined as a stored procedure (before running the script) and the script would only have to call the stored proc 可以将长操作定义为存储过程(在运行脚本之前),并且脚本只需要调用存储过程

Of course, splitting the script up into smaller portions and calling each one from a simple batch file would work too. 当然,将脚本分成较小的部分并从一个简单的批处理文件中调用每个部分也会起作用。 But I'd be a little worried about performance (how long does the execution take?!) and would look for some faster ways. 但是我会对性能有点担心(执行需要多长时间?!)并且会寻找更快的方法。

gzip应该这样做。

SQL is much harder to shrink, the field, table names and commands need to be what they are. SQL更难收缩,字段,表名和命令必须是它们的本质。 Plus, you wouldn't just want to rewrite the commands as something shorter because it could have implications on performance. 另外,您不仅希望将命令重写为更短的命令,因为它可能会影响性能。

Depending on the DBMS that you use, it may allow short names for commands, and then there might be a converter. 根据您使用的DBMS,它可能允许命令的短名称,然后可能有转换器。

(Answering this because it is the top item returned when I searched for "SQL script size") (回答这个,因为它是我搜索“SQL脚本大小”时返回的顶级项目)

I got the same error when trying to load a large script into Management Studio. 尝试将大型脚本加载到Management Studio时,我遇到了同样的错误。 In my case I was trying to downgrade a database from SQL2008 R2 to SQL 2008 by using the SQL Server script generator, which created a 700mb structure and data .sql file. 在我的情况下,我试图通过使用SQL Server脚本生成器将数据库从SQL2008 R2降级到SQL 2008,该生成器创建了700mb结构和数据.sql文件。

To get around it I used the command line to run the script instead: 为了解决这个问题,我使用命令行来运行脚本:

C:>sqlcmd -S [SQLSERVER\\INSTANCE] -i [FILELOCATION\\FILENAME].sql C:> sqlcmd -S [SQLSERVER \\ INSTANCE] -i [FILELOCATION \\ FILENAME] .sql

Hopefully this helps someone else. 希望这有助于其他人。

Compress the sql file will have the most compression ratio. 压缩sql文件将具有最大的压缩比。

Minimizing the txt sql file will reduce some bytes/kilobytes per mega.. is not worth... 最小化txt sql文件将减少每兆的一些字节/千字节..不值得......

The better approach is to create a "function" to unzip and read the file. 更好的方法是创建一个“函数”来解压缩和读取文件。 The best benefit I guess. 我猜是最好的好处。

Today, filesize shouldn't be a problem. 今天,filesize应该不是问题。 Dial-up connection? 计算机)拨号连接? Floppy disks? 软盘?

pg-minify可以做到这一点,不仅适用于PostgreSQL,而且适用于大多数符号,包括MS,MySql等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM