Database query optimization--Indexing temporary tables

Source: Internet
Author: User

When querying the database in order to query more quickly, generally use temporary tables, such as SELECT * into #t from TableA, but if the data is too large, but the temporary may also be very slow, this time can be a temporary table index, such as CREATE INDEX ix_temptable On #T (Field 1, Field 2, Field 3). If the temporary table field is unknown, how to index it, you can use the following SQL to find out all the fields of the temporary tables, to index all the fields

DECLARE @COL VARCHAR (1000)
SET @COL = ' '
SELECT @COL = @COL + column_name + ', ' from
TEMPDB.INFORMATION_SCHEMA.COLUMNS A
INNER JOIN (SELECT [NAME] from TEMPDB.dbo.sysobjects WHERE [ID] = object_id (N ' TEMPDB: #T ')) B
On a.table_name = B.[name] and a.ordinal_position <= 3
ORDER by Ordinal_position
Print @COL
SET @COL = Left (@COL, LEN (@COL)-1)
EXEC (' CREATE INDEX ix_temptable on #T (' + @COL + ') ')

Database query optimization--Indexing temporary tables

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.