SQL Server2008 INNER JOIN practices in multiple ways

Source: Internet
Author: User
Tags getdate

These days of study, only to find that the SQL originally was so not understanding. Have always thought that they can easily deal with a variety of complex SQL queries, but once mentioned efficiency, may be more dumbfounded, and sometimes complain that the client server is not working.

As for the inner join three ways: Loop,merge, hash, there are many articles in the garden, say to see many times than the actual operation of their own. Let's try it today. Note: There are about 46,000 rows of data in the T (4) table, and about 41,000 rows of data in the T (1) Table

First Type: Merge

DECLARE @begin datetimedeclare @end datetimeset @begin = GETDATE ()
SET STATISTICS IO onSelect * fromdbo. [T (4)] inner merge join dbo. [T (1)] ON dbo. [T (4)].keyword0 = dbo. [T (1)].keyword0
Set STATISTICS IO offset @end = GETDATE () print DateDiff (millisecond, @begin, @end)

The second type: hash

Declare @begin datetimedeclare @end datetimeset @begin = GETDATE ()
SET STATISTICS IO onSelect * fromdbo. [T (4)] inner hash join dbo. [T (1)] ON dbo. [T (4)].keyword0 = dbo. [T (1)].keyword0
Set STATISTICS IO offset @end = GETDATE () print DateDiff (millisecond, @begin, @end)

The results of the two SQL executions are shown separately:

(239657 rows affected)
Table ' T (4) '. Scan Count 10, logical read 3,236 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' T (1) '. Scan Count 10, logical read 3,064 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' worktable '. Scan Count 12, logical read 430 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Time: 22006


(239657 rows affected)
Table ' T (4) '. Scan Count 10, logical read 3,236 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' T (1) '. Scan Count 10, logical read 3,064 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' worktable '. Scan count 0, logical read 0 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Time: 17026

From the above can be seen, the merge is more than the hash of the operation, that is, the operation of the worktable, more time-consuming should be consumed here.

The CPU time is as follows:

(239657 rows affected)

SQL Server Execution Time:
CPU time = 12763 milliseconds, elapsed time = 24520 milliseconds.

(239657 rows affected)

SQL Server Execution Time:
CPU time = 11702 milliseconds, elapsed time = 17587 milliseconds.

I wanted to look at the memory overhead, and I didn't know what a good way to look at the memory overhead. We'll be here for the time being today.

SQL Server2008 INNER JOIN practices in multiple ways

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.