zoukankan      html  css  js  c++  java
  • SQL Server性能常用语句

    1. 查看各表的数据行数
      SELECT
      o.name, i. ROWS
      FROM sysobjects o, sysindexes i
      WHERE o.id = i.id  AND o.Xtype = 'U' AND i.indid < 2
      ORDER BY o.name;
    2. 计算数据库中各个表每行记录所占用空间
      --SELECT * FROM Employees AS e;
      
      CREATE TABLE #tablespaceinfo
          (
            nameinfo VARCHAR(50) ,
            rowsinfo BIGINT ,
            reserved VARCHAR(20) ,
            datainfo VARCHAR(20) ,
            index_size VARCHAR(20) ,
            unused VARCHAR(20)
          )  
       
      DECLARE @tablename VARCHAR(255);  
       
      DECLARE Info_cursor CURSOR
      FOR
          SELECT  '[' + [name] + ']'
          FROM    sys.tables
          WHERE   type = 'U';  
       
      OPEN Info_cursor  
      FETCH NEXT FROM Info_cursor INTO @tablename  
       
      WHILE @@FETCH_STATUS = 0
          BEGIN 
              INSERT  INTO #tablespaceinfo
                      EXEC sp_spaceused @tablename  
              FETCH NEXT FROM Info_cursor  
          INTO @tablename  
          END 
       
      CLOSE Info_cursor  
      DEALLOCATE Info_cursor  
       
      --创建临时表
      CREATE TABLE [#tmptb]
          (
            TableName VARCHAR(50) ,
            DataInfo BIGINT ,
            RowsInfo BIGINT ,
            Spaceperrow  AS ( CASE RowsInfo
                               WHEN 0 THEN 0
                               ELSE CAST(DataInfo AS decimal(18,2))/CAST(RowsInfo AS decimal(18,2))
                             END ) PERSISTED
          )
      
      --插入数据到临时表
      INSERT  INTO [#tmptb]
              ( [TableName] ,
                [DataInfo] ,
                [RowsInfo]
              )
              SELECT  [nameinfo] ,
                      CAST(REPLACE([datainfo], 'KB', '') AS BIGINT) AS 'datainfo' ,
                      [rowsinfo]
              FROM    #tablespaceinfo
              ORDER BY CAST(REPLACE(reserved, 'KB', '') AS INT) DESC  
      
      
      --汇总记录
      SELECT  [tbspinfo].* ,
              [tmptb].[Spaceperrow] AS '每行记录大概占用空间(KB)'
      FROM    [#tablespaceinfo] AS tbspinfo ,
              [#tmptb] AS tmptb
      WHERE   [tbspinfo].[nameinfo] = [tmptb].[TableName]
      ORDER BY CAST(REPLACE([tbspinfo].[reserved], 'KB', '') AS INT) DESC  
      
      DROP TABLE [#tablespaceinfo]
      DROP TABLE [#tmptb]
    3. 获取SQL语句执行时

        

    DECLARE @a DATETIME
    DECLARE @b DATETIME
    SELECT @a=GETDATE()
    exec sp_tables;        --查看表
    SELECT @b=GETDATE()
    SELECT @b-@a
     

      

  • 相关阅读:
    Arrays类和Timer类(定时调度)
    Centos6.3下Ganglia3.6.0安装配置
    【编程之美挑战赛第一场】树
    ORACLE物化视图具体解释
    让算法会说话之冒泡排序
    VS2008帮助"更新正在进行中"解决办法
    #问题
    sql server 2008 评估期已过期解决办法
    eclipse定义枚举类型错误
    C# 生产者和消费者问题使用Monitor同步
  • 原文地址:https://www.cnblogs.com/gossip/p/3830003.html
Copyright © 2011-2022 走看看