GVKun编程网logo

(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解

9

本篇文章给大家谈谈(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解,同时本文还将给你拓展c#–如何使用foreach或foreach迭代IEnu

本篇文章给大家谈谈(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解,同时本文还将给你拓展c# – 如何使用for each或foreach迭代IEnumerable集合?、C#在foreach中重用变量是否有原因? - Is there a reason for C#''s reuse of the variable in a foreach?、foreach Transform 同时chils.setParent引起的bug、Increase memory usage for InnoDB MySQL database to improve p_MySQL等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解

(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解

 

推荐(sqlServer)不公开存储过程


sp_Msforeachtablesp_Msforeachdb详解


——通过知识共享树立个人品牌。

 

一.简要介绍:

系统存储过程sp_MSforeachtablesp_MSforeachdb,是微软提供的两个不公开的存储过程。从mssql6.5开始,存放在sql Server的MASTER数据库中。可以用来对某个数据库的所有表或某个sql服务器上的所有数据库进行管理,下面将对此进行详细介绍。

作为数据库管理者或开发者等经常会检查整个数据库或用户表。

如:检查整个数据库的容量,看指定数据库所有用户表的容量,所有表的记录数等等,我们一般处理这样的问题都是通过游标来达到要求。

如果我们用sp_MSforeachtable就可以非常方便的达到相同的目的,

如:SQL查询所有用户表的列表,详细信息,如:记录数,表占用大小等

EXEC sp_MSforeachtable " EXECUTE sp_spaceused  ' ? '"

二.各参数说明:

  

   @command1  nvarchar( 2000),           -- 第一条运行的sql指令
   @replacechar  nchar( 1= N ' ? ',       -- 指定的占位符号
   @command2  nvarchar( 2000) =  null,     -- 第二条运行的sql指令
   @command3  nvarchar( 2000)   =  null,   -- 第三条运行的sql指令
   @whereand  nvarchar( 2000)   =  null,   -- 可选条件来选择表
   @precommand  nvarchar( 2000) =  null,   -- 执行指令前的操作(类似控件的触发前的操作)
   @postcommand  nvarchar( 2000) =  null   -- 执行指令后的操作(类似控件的触发后的操作)

 以后为sp_MSforeachtable的参数,sp_MSforeachdb不包括参数@whereand

 我们在master数据库里执行下面的语句可以看到两个proc详细的代码

use master
exec sp_helptext sp_MSforeachtable
exec sp_helptext sp_Msforeachdb

三、使用举例:

   --统计数据库里每个表的详细情况:

   exec sp_MSforeachtable  @command1 ="sp_spaceused  ' ? '"
 
   -- 获得每个表的记录数和容量:
   EXEC sp_MSforeachtable  @command1 =" print  ' ? '",
                          @command2 ="sp_spaceused  ' ? '",
                          @command3 = " SELECT  count( *FROM ? "
 
   -- 获得所有的数据库的存储空间:
   EXEC sp_MSforeachdb  @command1 =" print  ' ? '",
                       @command2 ="sp_spaceused "
 
   -- 检查所有的数据库
   EXEC sp_MSforeachdb  @command1 =" print  ' ? '",
                       @command2 =" DBCC CHECKDB (?) "
 
   -- 更新PUBS数据库中已t开头的所有表的统计:
   EXEC sp_MSforeachtable    @whereand =" and name  like  ' t% '",
                            @replacechar = ' * ',
                            @precommand =" print  ' Updating Statistics..... '  print  ''",
                            @command1 =" print  ' * '  update  statistics  * ",
                            @postcommand = " print '' print  ' Complete Update Statistics! '"
 
   -- 删除当前数据库所有表中的数据
  sp_MSforeachtable  @command1 = ' Delete from ? '
  sp_MSforeachtable  @command1  = " TruncATE  TABLE ?"
 
-- 查询数据库所有表的记录总数
CREATE  TABLE # temp (TableName  VARCHAR ( 255), RowCnt  INT)
EXEC sp_MSforeachtable  ' INSERT INTO #temp SELECT  '' ? '' , COUNT(*) FROM ? '
SELECT TableName, RowCnt  FROM # temp  ORDER  BY TableName
DROP  TABLE # temp
 
-- 检查数据库里每个表或索引视图的数据、索引及text、ntext 和image 页的完整性
--
下列语句需在单用户模式下执行(sp_dboption 'db_name', 'single user', 'true')
--
,将true改成false就又变成多用户了
exec sp_msforeachtable " dbcc checktable( ' ? ',repair_rebuild)"

4.参数@whereand的用法:

 @whereand参数在存储过程中起到指令条件限制的作用,具体的写法如下:

 @whereend,可以这么写

@whereand = '  AND o.name in ( '' Table1 '' , '' Table2 '' ,.......) ' 

又如:

我想更新Table1/Table2/Table3中NOTE列为NULL的值

 sp_MSforeachtable  @command1 = ' Update ? Set NOTE= ''''  Where NOTE is NULL '
                 ,@whereand = '  AND o.name in ( '' Table1 '' , '' Table3 '' ) '

c# – 如何使用for each或foreach迭代IEnumerable集合?

c# – 如何使用for each或foreach迭代IEnumerable集合?

我想逐个添加值但在for循环中如何迭代
通过逐个值并将其添加到字典中.

IEnumerable<Customer> items = new Customer[] 
{ 
     new Customer { Name = "test1",Id = 111},new Customer { Name = "test2",Id = 222} 
};

我想在i = 0时添加{Name =“test1”,Id = 111}
并且当i = 1 n时,想要添加{Name =“test2”,Id = 222}.

现在我正在每个键中添加完整的集合.(希望使用foreach或forloop实现此目的)

public async void Set(IEnumerable collection)
{
   RedisDictionary<object,IEnumerable <T>> dictionary = new RedisDictionary>(Settings,typeof(T).Name);
// Add collection to dictionary;
   for (int i = 0; i < collection.Count(); i++)
   { 
     await dictionary.Set(new[] { new keyvaluePair<object,IEnumerable <T>  ( i,collection) });
   }
}

解决方法

新版本

var dictionary = items.Zip(Enumerable.Range(1,int.MaxValue - 1),(o,i) => new { Index = i,Customer = (object)o });

顺便说一句,字典是一些变量的坏名字.

C#在foreach中重用变量是否有原因? - Is there a reason for C#''s reuse of the variable in a foreach?

C#在foreach中重用变量是否有原因? - Is there a reason for C#''s reuse of the variable in a foreach?

问题:

When using lambda expressions or anonymous methods in C#, we have to be wary of the access to modified closure pitfall. 在C#中使用lambda表达式或匿名方法时,我们必须警惕对修改后的闭包陷阱的访问 For example: 例如:

foreach (var s in strings)
{
   query = query.Where(i => i.Prop == s); // access to modified closure
   ...
}

Due to the modified closure, the above code will cause all of the Where clauses on the query to be based on the final value of s . 由于修改了闭包,上面的代码将使查询上的所有Where子句都基于s的最终值。

As explained here , this happens because the s variable declared in foreach loop above is translated like this in the compiler: 正如解释在这里 ,这是因为在s中声明的变量foreach上面的循环被翻译这样的编译:

string s;
while (enumerator.MoveNext())
{
   s = enumerator.Current;
   ...
}

instead of like this: 而不是像这样:

while (enumerator.MoveNext())
{
   string s;
   s = enumerator.Current;
   ...
}

As pointed out here , there are no performance advantages to declaring a variable outside the loop, and under normal circumstances the only reason I can think of for doing this is if you plan to use the variable outside the scope of the loop: 如此处所指出的, 在循环外声明变量没有任何性能优势,在正常情况下,我可以想到的唯一原因是如果您打算在循环范围外使用变量:

string s;
while (enumerator.MoveNext())
{
   s = enumerator.Current;
   ...
}
var finalString = s;

However variables defined in a foreach loop cannot be used outside the loop: 但是,在foreach循环中定义的变量不能在循环外使用:

foreach(string s in strings)
{
}
var finalString = s; // won''t work: you''re outside the scope.

So the compiler declares the variable in a way that makes it highly prone to an error that is often difficult to find and debug, while producing no perceivable benefits. 因此,编译器以某种方式声明该变量,使其极易出现通常难以查找和调试的错误,同时不会产生明显的收益。

Is there something you can do with foreach loops this way that you couldn''t if they were compiled with an inner-scoped variable, or is this just an arbitrary choice that was made before anonymous methods and lambda expressions were available or common, and which hasn''t been revised since then? 是否可以使用foreach循环以这种方式进行处理,如果它们是使用内部作用域变量编译的,则无法做到,还是只是在匿名方法和lambda表达式可用或通用之前做出的任意选择,以及从那以后没有修改过?


解决方案:

参考一: https://stackoom.com/question/bL13/C-在foreach中重用变量是否有原因
参考二: https://oldbug.net/q/bL13/Is-there-a-reason-for-C-s-reuse-of-the-variable-in-a-foreach

foreach Transform 同时chils.setParent引起的bug

foreach Transform 同时chils.setParent引起的bug

Transform继承自IEnumerable,可以对它进行迭代。但当你在迭代的同时,又对child进行setParent操作时,会出现意想不到的结果。

下面是我使用foreach和getchild得到的bug,及解决办法。

使用foreach

当在使用foreach获取所有的child,并且同时修改child的parent为其它,会出现只能修改部分,但不会报错。

foreach (var tran in rideEffect.InstanceAsset.transform)
{
    var child = tran as Transform;
    if (child == null)
    {
        continue;
    }
    KTool.SetChild(child,boneTrans.transform);
}

 

使用GetChild

使用getchild获取每一个child,同时设置child的parent为其它时,会报:Transform child out of bounds

var childCount = rideEffect.InstanceAsset.transform.childCount; for (int idx = 0; idx < childCount; idx++) { var child = rideEffect.InstanceAsset.transform.GetChild(idx); KTool.SetChild(child,boneTrans.transform); }

 

解决办法

添加一个扩展方法获取所有的childs,存起来。

或者也可以不写扩展方法,直接使用List<Transform>存child。

public static IEnumerable<Transform> GetChildren(this Transform tr) { List<Transform> children = new List<Transform>(); foreach (Transform child tr) { children.Add(child); } // You can make the return type an array or a list or else. return children as IEnumerable<Transform>; }

调用方法,这样就可以修改完全部的child

var childs = rideEffect.InstanceAsset.transform.GetChildren(); var child childs) { KTool.SetChild(child,boneTrans.transform); }

Increase memory usage for InnoDB MySQL database to improve p_MySQL

Increase memory usage for InnoDB MySQL database to improve p_MySQL

if your mysql database tables still run on the myisam engine (formerly the default), you may want to consider switching to the innodb engine instead, for better reliability and scalability. to update a table from myisam to innodb you can run this sql:

ALTER TABLE table_name ENGINE = InnoDB;
登录后复制

Once you’ve switched all your tables to InnoDB, you can adjust some memory usage settings.

Update MySQL memory usage settings for InnoDB

Firstly, check the current settings for innerdb_buffer_pool_size . You can view these settings by running the following SQL (you can run this in phpMyAdmin):

SHOW VARIABLES;
登录后复制
登录后复制

Look for innerdb_buffer_pool_size . You’ll see it’s been assigned a particular number of bytes. This allocated cache stores table and index data, and keeps queries and query results in memory for faster lookup. So the more memory you can afford to dedicate to it the better – MySQL recommends to use 80% of the available memory. You can read about it here .

I had 2GB of server memory to play with, so I chose a moderate 1GB to allocate to the innerdb_buffer_pool_size . To add this setting, we’ll create and load our own custom MySQL cnf file, which will house some extra settings.

On Linux Ubuntu , add a new cnf file here:

sudo nano /etc/mysql/conf.d/innodb.cnf
登录后复制

The file name must end in .cnf , but call it whatever you like, so long as it’s not clashing with another file name.

Inside of this file, we add our new memory allocation:

[mysqld]innodb_buffer_pool_size = 1024Mkey_buffer_size = 8M
登录后复制

I’ve also added a new key_buffer_size value of 8MB. If you’ve deprecated your use of the MyISAM engine, it’s recommended to reduce this memory allocation. Previously I had 16M for key_buffer_size , so I decided to half it.

Finish off by restarting MySQL so that the changes can be applied:

sudo service mysql restart
登录后复制

If you check the MySQL variables once more:

SHOW VARIABLES;
登录后复制
登录后复制

You’ll hopefully now have some new and importantly increased memory values for innodb_buffer_pool_size and key_buffer_size

innodb_buffer_pool_size

myisam_key_buffer_size

You’ve successfully optimised your MySQL database a bit more!

关于(SqlServer)不公开存储过程sp_Msforeachtable与sp_Msforeachdb详解的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c# – 如何使用for each或foreach迭代IEnumerable集合?、C#在foreach中重用变量是否有原因? - Is there a reason for C#''s reuse of the variable in a foreach?、foreach Transform 同时chils.setParent引起的bug、Increase memory usage for InnoDB MySQL database to improve p_MySQL等相关知识的信息别忘了在本站进行查找喔。

本文标签: