Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Indexes Expand / Collapse
Author
Message
Posted Saturday, September 5, 2009 2:33 PM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 6:34 AM
Points: 654, Visits: 265
Comments posted to this topic are about the item Indexes
Post #783392
Posted Saturday, September 5, 2009 2:38 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
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
Post #783393
Posted Monday, September 7, 2009 12:07 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 9:35 AM
Points: 1,475, Visits: 1,632
table scan....I think it will be index scan....and query 1 will give a index scan...

Regards,
Sqlfrenzy

Post #783624
Posted Monday, September 7, 2009 12:10 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Yesterday @ 9:35 AM
Points: 1,475, Visits: 1,632
table scan....I think it will be index scan....and query 1 will give a index scan...

Regards,
Sqlfrenzy

Post #783626
Posted Monday, September 7, 2009 3:15 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:21 AM
Points: 2,122, Visits: 306
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.
Post #783675
Posted Monday, September 7, 2009 5:05 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, June 11, 2014 9:54 AM
Points: 912, Visits: 654
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)

Post #783701
Posted Monday, September 7, 2009 5:17 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 1:05 AM
Points: 2,842, Visits: 3,875
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
Post #783709
Posted Monday, September 7, 2009 5:17 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:21 AM
Points: 2,122, Visits: 306
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.
Post #783710
Posted Monday, September 7, 2009 6:07 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, June 11, 2014 9:54 AM
Points: 912, Visits: 654
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


Post #783735
Posted Monday, September 7, 2009 6:17 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, March 3, 2014 12:21 AM
Points: 2,122, Visits: 306
After rebuilding the index I also get a table scan on querie 1.
Post #783740
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse