【额 原来ms sqlserver 中的视图果然是“虚表”哈】

在视图中查询数据的时候,会不会使用实体表中的列上的索引呢?会 。。。。

测试结果

测试脚本

DECLARE @len INT =100;
WHILE @len>0
BEGIN
    
    INSERT INTO Teachers
            ( TeacherName, Sex, MoneyPay )
    VALUES  ( 
               CAST(@len AS NVARCHAR), -- TeacherName - nvarchar(50)
              NULL, -- Sex - bit
              10000  -- MoneyPay - decimal
              )
    
    SET @len-=1;
END

go


DECLARE @len INT =1000;
WHILE @len>0
BEGIN
    
    INSERT INTO Students
            ( StuName, Address, Birthday )
    VALUES  ( CAST(@len AS NVARCHAR )+'-'+CAST(@len AS NVARCHAR ), -- StuName - nvarchar(50)
              N'', -- Address - nvarchar(200)
              GETDATE()  -- Birthday - datetime
              )
          
    SET @len-=1;
END

go


DECLARE @len_t INT =1;
DECLARE @len_stu INT =1;
WHILE @len_t<=100
BEGIN
    SET @len_stu=@len_t;
    
    WHILE @len_stu>0
     BEGIN
        INSERT INTO TeachAndStudent
                ( TeacherId, StudentId, CreateTime )
        VALUES  ( @len_t, -- TeacherId - int
                  @len_stu, -- StudentId - int
                   GETDATE() -- CreateTime - datetime
                  )
         SET @len_stu-=1;
     END
    
    SET @len_t+=1;
END
------------test:--------------
--SELECT * FROM Teachers
--GO
--SELECT * FROM Students
--GO
--SELECT COUNT(*) FROM TeachAndStudent
GO
--CREATE VIEW VTeacherAndStudents
--AS 
--SELECT 
--a.id AS tid,
--c.Id AS stuid,
--a.TeacherName AS tname,
--c.StuName AS sname

-- FROM Teachers A
--INNER JOIN TeachAndStudent B ON B.TeacherId=A.Id
--LEFT JOIN Students C ON C.Id = B.StudentId

GO

SELECT * FROM VTeacherAndStudents a
WHERE a.tid>10 AND a.tid<20
原文地址:https://www.cnblogs.com/micro-chen/p/5507481.html