SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 SQL Server Administration (2008)
 Accessing data from the data pages
 New Topic  Reply to Topic
 Printer Friendly
Next Page
Author Previous Topic Topic Next Topic
Page: of 2

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/04/2013 :  09:19:58  Show Profile  Reply with Quote
Hello everyone..

Will the table scan and the index scan takes equal time to retrieve data from data pages?

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/04/2013 :  09:41:40  Show Profile  Reply with Quote
nope..they wont.
As table scan will scan through entire table data pages whereas index scan will only scan through index pages to reach upto leaf level nad then use pointers to scan sccoociated data pages so it would be less. I hope you meant non clustered index.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

denis_the_thief
Aged Yak Warrior

Canada
591 Posts

Posted - 12/04/2013 :  11:46:12  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

nope..they wont.
As table scan will scan through entire table data pages whereas index scan will only scan through index pages to reach upto leaf level nad then use pointers to scan sccoociated data pages so it would be less. I hope you meant non clustered index.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




hmmm

I think that provided the table is in the cache and the index is unique, yes they would be about the same. In that case if there are n rows in the table, SQL Server has to do n comparisons looking for a match wether it is an Index Scan or Table scan.

Also, if the Index is non-covering, SQL Server has the additional bookmark lookup with the Index scan.

If the table and the index are not in the cache, the Index Scan could be faster if the records are large. It could take much longer to import the Table to cache then it would the Index.

Here is someone who tried the experiment: http://architects.dzone.com/articles/full-table-scan-vs-full-index
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/04/2013 :  11:58:01  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

nope..they wont.
As table scan will scan through entire table data pages whereas index scan will only scan through index pages to reach upto leaf level nad then use pointers to scan sccoociated data pages so it would be less. I hope you meant non clustered index.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




No sir,
I dont mean non-clustered indexes.

Give me conformation in the below aspect..
will the table scan differ from index scan which is performed by a query which is not using the index on that table actually.(The column on which the index is present is not used in the where clause)

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 12/04/2013 :  12:25:35  Show Profile  Reply with Quote
If the clustered index fields are not in the WHERE clause then the Query Plan will show the "table scan" as an "index scan on the clustered index" won't it? When I see one of those my brain tells me SQL has used the wrong index, until I then realise that its the Clustered Index on the table

Sorry if I'm answering the wrong question
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/04/2013 :  12:56:42  Show Profile  Reply with Quote
quote:
Originally posted by Kristen

If the clustered index fields are not in the WHERE clause then the Query Plan will show the "table scan" as an "index scan on the clustered index" won't it? When I see one of those my brain tells me SQL has used the wrong index, until I then realise that its the Clustered Index on the table

Sorry if I'm answering the wrong question



I understood your point.
And also please also tell me when an index scan happens? I mean in which scenario?

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 12/04/2013 :  13:04:18  Show Profile  Reply with Quote
This would - I think! - someone else can confirm :)

WHERE ThisColumnIsIndexed BETWEEN @Start AND @End
Go to Top of Page

denis_the_thief
Aged Yak Warrior

Canada
591 Posts

Posted - 12/04/2013 :  13:05:08  Show Profile  Reply with Quote
quote:


I understood your point.
And also please also tell me when an index scan happens? I mean in which scenario?




Here is a possible scenario:

- you have a table with one million rows
- and 30 columns
- you have only one index (LastName, FirstName)
- your query is select LastName where FirstName = 'xxx'

SQL Server would have to do a scan since there is no index on FirstName. But the Index would have all it needs for this query (i.e. covering). Since there are 30 columns, it would be a lot of effort to bring the table from disk to cache. And there might be only a few bookmark look ups, so the IndexScan would be quicker than a TableScan.
Go to Top of Page

Kristen
Test

United Kingdom
22403 Posts

Posted - 12/04/2013 :  13:08:32  Show Profile  Reply with Quote
So was mine an Index Seek, rather than Index Scan, then?
Go to Top of Page

denis_the_thief
Aged Yak Warrior

Canada
591 Posts

Posted - 12/04/2013 :  13:20:59  Show Profile  Reply with Quote
quote:
Originally posted by Kristen

So was mine an Index Seek, rather than Index Scan, then?



I believe yes, it would be an Index Seek, unless SQL Server would predict a high number of matches (i.e. low selectivity), it may opt to use the Index Scan.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/05/2013 :  02:13:52  Show Profile  Reply with Quote
quote:
Originally posted by denis_the_thief

quote:
Originally posted by visakh16

nope..they wont.
As table scan will scan through entire table data pages whereas index scan will only scan through index pages to reach upto leaf level nad then use pointers to scan sccoociated data pages so it would be less. I hope you meant non clustered index.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




hmmm

I think that provided the table is in the cache and the index is unique, yes they would be about the same. In that case if there are n rows in the table, SQL Server has to do n comparisons looking for a match wether it is an Index Scan or Table scan.

Also, if the Index is non-covering, SQL Server has the additional bookmark lookup with the Index scan.

If the table and the index are not in the cache, the Index Scan could be faster if the records are large. It could take much longer to import the Table to cache then it would the Index.

Here is someone who tried the experiment: http://architects.dzone.com/articles/full-table-scan-vs-full-index



That article speaks from MySQL front. Will there any difference in MSSQL implementation?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/05/2013 :  04:57:05  Show Profile  Reply with Quote
quote:
Originally posted by Kristen

This would - I think! - someone else can confirm :)

WHERE ThisColumnIsIndexed BETWEEN @Start AND @End



No sir.
In this case index seek will take place.
Because, if the column (on which the index is created) is present in the "where" clause, then index seek will take place.

Else, that means there is an index on some column but we are not using that column in the where clause, then index scan will happen.

And finally when there is no index on a table, the table scan will take place.

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/05/2013 :  05:09:04  Show Profile  Reply with Quote
quote:
Originally posted by denis_the_thief

quote:
Originally posted by visakh16

nope..they wont.
As table scan will scan through entire table data pages whereas index scan will only scan through index pages to reach upto leaf level nad then use pointers to scan sccoociated data pages so it would be less. I hope you meant non clustered index.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




hmmm

I think that provided the table is in the cache and the index is unique, yes they would be about the same. In that case if there are n rows in the table, SQL Server has to do n comparisons looking for a match wether it is an Index Scan or Table scan.

Also, if the Index is non-covering, SQL Server has the additional bookmark lookup with the Index scan.

If the table and the index are not in the cache, the Index Scan could be faster if the records are large. It could take much longer to import the Table to cache then it would the Index.

Here is someone who tried the experiment: http://architects.dzone.com/articles/full-table-scan-vs-full-index




I feel that you told most valid points in the above post. Thank you sir.

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/05/2013 :  05:12:25  Show Profile  Reply with Quote
quote:
Originally posted by Kristen

So was mine an Index Seek, rather than Index Scan, then?



Yes it is an index seek.

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/05/2013 :  05:20:00  Show Profile  Reply with Quote
quote:
Originally posted by denis_the_thief

quote:


I understood your point.
And also please also tell me when an index scan happens? I mean in which scenario?




Here is a possible scenario:

- you have a table with one million rows
- and 30 columns
- you have only one index (LastName, FirstName)
- your query is select LastName where FirstName = 'xxx'

SQL Server would have to do a scan since there is no index on FirstName. But the Index would have all it needs for this query (i.e. covering). Since there are 30 columns, it would be a lot of effort to bring the table from disk to cache. And there might be only a few bookmark look ups, so the IndexScan would be quicker than a TableScan.




Thanks a lot for your support.
If there is an index(not covering index) on columns LastName and FirstName in this order, will the first column in the index (i.e LastName) is the only column which gets stored in stored in index pages?
No right?
If yes then why to mention second column in the index creation?

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/05/2013 :  05:26:25  Show Profile  Reply with Quote
quote:
Originally posted by denis_the_thief

quote:
Originally posted by Kristen

So was mine an Index Seek, rather than Index Scan, then?



I believe yes, it would be an Index Seek, unless SQL Server would predict a high number of matches (i.e. low selectivity), it may opt to use the Index Scan.



Do mean this?
Will the SQL Server
1. Selects index scan if there is more selectivity for the query submitted? and
2. Selects an index seek if there is less selectivity for the query submitted?

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

denis_the_thief
Aged Yak Warrior

Canada
591 Posts

Posted - 12/06/2013 :  14:21:37  Show Profile  Reply with Quote
quote:

That article speaks from MySQL front. Will there any difference in MSSQL implementation?




Good Point, I hadn't even noticed that.

I ran a test. It's not the be all and end all, so many factors could be at play.

I run the same query under 3 scenarios: Table Scan, Clustered Index Scan, Non-Clustered Index Scan. I ran each under both an empty cache and data-in-cache.

The table had a few million records. The table had 30 columns while the Non-Clustered Index had only 3 columns. Also, the index was covering so there was no bookmark lookup.

Here are my Results!:


Non-Clustered Index Scan Empty Cache		1,500ms
Clustered Index Scan Empty Cache	       16,500ms
Table Scan Empty Cache			       18,600ms

Non-Clustered Index Scan Data-In-Cache	          380ms
Clustered Index Scan Data-In-Cache		  570ms
Table Scan Data-In-Cache			  550ms


So the Index Scan was much faster than Table Scan when the cache was empty. It was also faster than the Table Scan when the data was in-cache, but not a huge amount.

I also found out that the Table Scan is only for the Heap.

Edited by - denis_the_thief on 12/06/2013 14:31:30
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/07/2013 :  08:36:03  Show Profile  Reply with Quote
yep..thats what I thought too. Just wanted someone to confirm that.
Yes..that true. Table Scan would be for heap. Otherwise it will be replaced by clustered index scan
------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs

Edited by - visakh16 on 12/07/2013 08:36:47
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/11/2013 :  09:22:01  Show Profile  Reply with Quote
quote:
Originally posted by denis_the_thief

quote:

That article speaks from MySQL front. Will there any difference in MSSQL implementation?




Good Point, I hadn't even noticed that.

I ran a test. It's not the be all and end all, so many factors could be at play.

I run the same query under 3 scenarios: Table Scan, Clustered Index Scan, Non-Clustered Index Scan. I ran each under both an empty cache and data-in-cache.

The table had a few million records. The table had 30 columns while the Non-Clustered Index had only 3 columns. Also, the index was covering so there was no bookmark lookup.

Here are my Results!:


Non-Clustered Index Scan Empty Cache		1,500ms
Clustered Index Scan Empty Cache	       16,500ms
Table Scan Empty Cache			       18,600ms

Non-Clustered Index Scan Data-In-Cache	          380ms
Clustered Index Scan Data-In-Cache		  570ms
Table Scan Data-In-Cache			  550ms


So the Index Scan was much faster than Table Scan when the cache was empty. It was also faster than the Table Scan when the data was in-cache, but not a huge amount.

I also found out that the Table Scan is only for the Heap.



Since it you are taking a covering non-clustered index, this non-clustered index is that efficient.

If you take a non-clustered(non-covering) index, then the decision of opting for a table or index scan depends on the number of rows that a query results right?

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/11/2013 :  12:26:17  Show Profile  Reply with Quote
Not only that some of other factors lie selectivity of data etc.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sgondesi
Posting Yak Master

India
194 Posts

Posted - 12/11/2013 :  12:34:11  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

Not only that some of other factors lie selectivity of data etc.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs




Ok sir.
Can you share some of the other factors that lie selectivity.

And i dint get your statement sir. "Table scan would be for heap". Can you go detail about this.

--
Thanks and Regards
Srikar Reddy Gondesi,
BTECH-IT 2013 Passed Out,
Trainee for SQL Server Administration,
Miracle Software systems, Inc.
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Next Page
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.41 seconds. Powered By: Snitz Forums 2000