简体   繁体   English

使用SQL Server 2008 R2中的临时表优化表的性能

[英]optimizing a table's performance using temporary table in sql server 2008 r2

Good day, I'm quite a newbie in optimizing database queries...actually it's my very first time... I've done my research and found out that using temporary tables could help me improve the queries performance.. Aside from other solutions like using index but though their are a lot of blogs about it I haven't yet to find a well detailed tutorial about it... I was hoping if you could kindly help. 美好的一天,我在优化数据库查询方面是个新手……实际上,这是我的第一次。我已经完成了研究,发现使用临时表可以帮助我提高查询性能。像使用索引这样的解决方案,但是尽管他们有很多关于它的博客,但我还没有找到关于它的详细教程...我希望可以为您提供帮助。 Given this sample table... 给定这个样本表...

Table Schemas:: 表模式::

    StudentInfo:
    StudentID 
    LAstname
    FirstName
    YearCode
    SectionCode

    TeacherInfo:
    SectionCode
    TeacherID
    TeacherName


    GradeInfo:
    StudentID
    AverageGrade

    CashierRecord:
    StudentID
    EnrolledDate
    ModeOfPayment
    AmountDue

Query I have Tried... 查询我尝试过...

 Select 
        s.StudentID,
        s.FirstName,
        s.LastName,
        t.Teacher,
        g.AverageGrade,
        c.EnrolledDate
    From StudentInfo s
    LEFT JOIN TeacherInfo t ON s.StudentID= t.StudentID
    LEFT JOIN GradeInfo g ON s.StudentID= g.StudentID
    LEFT JOIN GradeRecords c ON s.StudentID= c.StudentID

If the given query would retrieve a huge amount of data (ex. 100,000 records) the query would surely run low in performance... may you kindly explain how could I utilize TEMPORARY TABLES to store the data from multiple tables so that retrieval could be easier... 如果给定的查询将检索大量数据(例如100,000条记录),则该查询肯定会运行性能低下...请您解释一下我如何利用TEMPORARY TABLES存储来自多个表的数据,以便进行检索更轻松...

To create a table from a select result in SQL server you could use select into . 要根据SQL Server中的选择结果创建表,可以使用select into

Select 
    s.StudentID,
    s.FirstName,
    s.LastName,
    t.Teacher,
    g.AverageGrade,
    c.EnrolledDate
into TEMPTABLE
From StudentInfo s
LEFT JOIN TeacherInfo t ON s.StudentID= t.StudentID
LEFT JOIN GradeInfo g ON s.StudentID= g.StudentID
LEFT JOIN GradeRecords c ON s.StudentID= c.StudentID

Then you could use TEMPTABLE and then drop it using 然后,您可以使用TEMPTABLE ,然后使用

drop table TEMPTABLE

With this you have collected your data to one table. 这样,您已将数据收集到一张表中。 TEMPTABLE is not yet a real temporary table. TEMPTABLE尚不是真正的临时表。 Look here to find more on that ( Is it necessary to use # for creating temp tables in SQL server? ). 可以在这里找到更多信息( 是否需要使用#在SQL Server中创建临时表? )。 It comes down to write #TEMPTABLE . 归结为编写#TEMPTABLE

But to build this table you still have the processing time of your select. 但是要构建此表,您仍然可以选择处理时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM