2017-05-14 52 views
0

我有40個表 - 表1,表2,表3,表40,全部具有相同的列。 這些表格包含不同的數據,並確定每個表格的數據,我有一列Reporting_Type其中每個表格具有不同的值。在SQL Server中全部聯合查看性能問題

例如:Reporting_Type列的Table1的值爲Reportin_Type1等等。

請注意,每個表格包含2-3萬條記錄。

我需要創建一個視圖,它結合了所有表中的數據。

我只是應用於UNION ALL,把下面的查詢:

CREATE VIEW ALL DATA 
AS 
    SELECT 
     COLUMN1, COLUMN2, COLUMN3, REPORTING_TYPE ..., COLUMN 40 
    FROM 
     TABLE1 

    UNION ALL 

    SELECT 
     COLUMN1, COLUMN2, COLUMN3, REPORTING_TYPE ..., COLUMN 40 
    FROM 
     TABLE2 

    UNION ALL 

    SELECT 
     COLUMN1, COLUMN2, COLUMN3, REPORTING_TYPE ..., COLUMN 40 
    FROM 
     TABLE3 

    .... 

    SELECT 
     COLUMN1, COLUMN2, COLUMN3, REPORTING_TYPE ..., COLUMN 40 
    FROM 
     TABLE40 

上面的查詢需要大量的時間來從所有表獲取數據。

任何請建議任何性能調諧查詢或任何其他方式來創建此視圖在SQL Server 2012中?

道歉,如果有人發現這個問題太天真。我是數據庫新手。請讓我知道是否需要更多信息。

+0

解釋做這個表有什麼索引? – gofr1

+0

@ gofr1。不,先生。他們沒有任何索引。 – Harsh

+0

這可能是一個問題。考慮添加索引。另外'髒讀'可能會有所幫助。在運行視圖時嘗試使用表提示WITH WITH(NOLOCK)(f.e.'FROM TABLE40 WITH(NOLOCK)')或添加'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'。還要看看索引視圖([msdn](https://msdn.microsoft.com/en-us/library/dd171921(v = SQL.100).aspx)) – gofr1

回答

0

我懇請推薦這些表上的Clustered Indexes,所以在所有表​​中都應該有主鍵,如果有這些表的主鍵,嘗試重建或重新組織索引,重建和重新組織您也可以參考下面腳本。如果沒有任何羣集索引,並且條件爲掃描,但使用羣集索引,則會獲得更好的性能。

-- Make Sure you have write USE <databasename> statement before executing statement. 
-- USE <databasename> 
SET NOCOUNT ON; 
DECLARE @objectid int; 
DECLARE @indexid int; 
DECLARE @partitioncount bigint; 
DECLARE @schemaname nvarchar(500); 
DECLARE @objectname nvarchar(500); 
DECLARE @indexname nvarchar(500); 
DECLARE @partitionnum bigint; 
DECLARE @partitions bigint; 
DECLARE @frag float; 
DECLARE @command nvarchar(4000); 
-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function 
-- and convert object and index IDs to names. 
SELECT 
    object_id AS objectid, 
    index_id AS indexid, 
    partition_number AS partitionnum, 
    avg_fragmentation_in_percent AS frag 
INTO #work_to_do 
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') 
WHERE index_id > 0; 

-- Declare the cursor for the list of partitions to be processed. 
DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do; 

-- Open the cursor. 
OPEN partitions; 

-- Loop through the partitions. 
WHILE (1=1) 
    BEGIN; 
     FETCH NEXT 
      FROM partitions 
      INTO @objectid, @indexid, @partitionnum, @frag; 
     IF @@FETCH_STATUS < 0 BREAK; 
     SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name) 
     FROM sys.objects AS o 
     JOIN sys.schemas as s ON s.schema_id = o.schema_id 
     WHERE o.object_id = @objectid; 
     SELECT @indexname = QUOTENAME(name) 
     FROM sys.indexes 
     WHERE object_id = @objectid AND index_id = @indexid; 
     SELECT @partitioncount = count (*) 
     FROM sys.partitions 
     WHERE object_id = @objectid AND index_id = @indexid; 

     -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding. 
     IF @frag < 30.0 OR @frag >= 5.0 
      SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE'; 
     IF @frag >= 30.0 OR @frag < 5.0 
      SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REBUILD'; 
     IF @partitioncount > 1 
      SET @command = @command + N' PARTITION=' + CAST(@partitionnum AS nvarchar(10)); 
     EXEC (@command); 
     PRINT N'Executed: ' + @command; 
    END; 

-- Close and deallocate the cursor. 
CLOSE partitions; 
DEALLOCATE partitions; 

-- Drop the temporary table. 
DROP TABLE #work_to_do; 
GO 

我還建議使用交叉應用,這是在StackOverflow's another thread