|
|
|
SSChasing Mays
      
Group: General Forum Members
Last Login: Saturday, April 06, 2013 12:20 AM
Points: 649,
Visits: 263
|
|
| Comments posted to this topic are about the item Indexes
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 3:05 PM
Points: 3,789,
Visits: 5,547
|
|
Data type "bool"? What are you running?
Also, your statement is only true for the small number of rows in your example. Its a different story with real world volumes. Remember, SQL will choose what it feels is an optimal execution plan based on statistics.
Run the code below and compare the execution plan for your example against the execution plan for a table with even 100 rows. You'll see what I mean.
create table #t (id int,ch char,na varchar(20),flag char(1)) insert into #t values (2,'A','jack','Y') insert into #t values (5,'b','amy','N') insert into #t values (1,'$','adams','N') insert into #t values (3,'*','anna','Y') insert into #t values (7,'@','rose','N') insert into #t values (4,'&','smith','Y') insert into #t values (6,'!','sue','Y') create nonclustered index nc_t on #t (id,ch,na) -- query 1 select na from #t where ch = '!' -- query 2 select na from #t where id = 6 and ch = '!' -- query 3 select na from #t where ch = '!' and id = 6 -- query 4 select na from #t where flag = 'Y' and id = 6 and ch = '!'
;with tally (N) as (select row_number() over(order by id) from master..syscolumns) select N as ID, ch, na, flag into #bigT from tally cross join #t t where N <=100
create nonclustered index nc_bigT on #bigT (id,ch,na)
-- query 1 select na from #bigT where ch = '!' -- query 2 select na from #bigT where id = 6 and ch = '!' -- query 3 select na from #bigT where ch = '!' and id = 6 -- query 4 select na from #bigT where flag = 'Y' and id = 6 and ch = '!'
drop table #t drop table #bigT
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 12:47 AM
Points: 1,431,
Visits: 1,540
|
|
table scan....I think it will be index scan....and query 1 will give a index scan...
Regards, Sqlfrenzy
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 12:47 AM
Points: 1,431,
Visits: 1,540
|
|
table scan....I think it will be index scan....and query 1 will give a index scan...
Regards, Sqlfrenzy
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 3:27 AM
Points: 1,864,
Visits: 297
|
|
Can you explain what you mean with "Also, your statement is only true for the small number of rows..."?
I ran both your example and that from the question. On all queries the execution plan looks about the same, saying query 4 scans the table, while the others scan/seek the index with a dozen rows and 250+ rows in the table.
Since 3 columns (id,ch,na) are part of the index, no table scan should occure unless the forth column is addressed in the where clause.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 3:39 AM
Points: 857,
Visits: 584
|
|
Hmmm... I played with this a little and the number of rows certainly makes a difference.
I added 10,000 rows (actually 10,001 - as below) and this time query 1 gave the only table scan and query 4 went for nested loops joining an Index Seek and a record id lookup, which is what I had expected it to do when I saw the question.
For the question as asked, I accept I got it wrong (I said Q1 as I thought all the others would perform an index seek but Q1 could not), but it is interesting how the question is not a simple one of the structure leading to a deterministic result, but the optimiser may take very different routes in the same database structures depending on other factors such as data volumes.
WITH cte (Num) AS ( SELECT 0 Num UNION ALL SELECT Num + 1 FROM cte WHERE Num < 10000 ) INSERT INTO t (id, ch, na, flag) SELECT num , CHAR(Num%128 + 50) , CAST(num AS VARCHAR), CASE WHEN Num%2 = 1 THEN 'Y' ELSE 'N' END FROM cte OPTION(maxrecursion 10000)
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 6:12 AM
Points: 2,526,
Visits: 3,620
|
|
Please note that the data of this table fits in one data page - therefore a table scan is nothing more than reading one page of data. And doing row-id lookups is definitively more expensive than a single page read.
When you add more rows, the picture gets different. If you expect only one row to be returned in a table with say 70000 rows, then the index seek + row id lookup (for one row) is much less expensive than a table scan (assume approx. 357 data pages) used for the 70000 rows.
Best Regards,
Chris Büttner
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 3:27 AM
Points: 1,864,
Visits: 297
|
|
| Now that's interesting. I filled "t" with your script. Including my initial data, there are 10288 rows. I ran the four queries and still get the only table scan on #4. This is on SS2008 Developer Ed.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Friday, May 03, 2013 3:39 AM
Points: 857,
Visits: 584
|
|
ma (9/7/2009) Now that's interesting. I filled "t" with your script. Including my initial data, there are 10288 rows. I ran the four queries and still get the only table scan on #4. This is on SS2008 Developer Ed.
Did you rebuild the index?
What I found was:
Initial conditions: 1) Index Scan 4) Table Scan
After adding 10001 rows: 1) Index Scan 4) Index Seek & RowID lookup
After rebuilding index 1) Table Scan 4) Index Seek & RowID lookup
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Monday, May 06, 2013 3:27 AM
Points: 1,864,
Visits: 297
|
|
| After rebuilding the index I also get a table scan on querie 1.
|
|
|
|