SQL Server Performance Common statements

Source: Internet
Author: User

  1. View the number of data rows for each table
    = i.id  'U'2ORDER by O.name;
  2. Calculate the space occupied by each row of records in the database
    --select *From Employees as E; CREATE TABLE #tablespaceinfo (nameinfo VARCHAR ( -), Rowsinfo BIGINT, reserved VARCHAR ( -), Datainfo VARCHAR ( -), Index_size VARCHAR ( -), unused VARCHAR ( -)) DECLARE @tablename VARCHAR (255); DECLARE info_cursor cursorfor SELECT'['+ [name] +']'From sys.tables WHERE type='U'; OPEN info_cursor FETCH NEXT from Info_cursor to @tablename while @ @FETCH_STATUS=0BEGIN INSERT into #tablespaceinfo EXEC sp_spaceused @tablename FETCH NEXT Fo_cursor into @tablename END CLOSE info_cursor deallocate info_cursor--Creating a temporary table create TABLE [#tmptb] (TableName VARCHAR ( -), Datainfo BIGINT, Rowsinfo BIGINT, Spaceperrow as (case Rowsinfo when 
          0Then0ELSE CAST (Datainfo asdecimal( -,2))/cast (Rowsinfo asdecimal( -,2) ) (END) PERSISTED)--insert data into temp table 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--Summary record SELECT [Tbspinfo].*, [TMPTB]. [Spaceperrow] as'approximately space per row of records (KB)'From [#tablespaceinfo] as Tbspinfo, [#tmptb] as Tmptbwhere [Tbspinfo]. [Nameinfo]=[TMPTB]. [TableName] ORDER by CAST (REPLACE ([tbspinfo].[ Reserved],'KB',"') as INT) DESC DROP table [#tablespaceinfo]drop table [#tmptb]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.