Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Using Configurable Option in SProc

Author  Topic 

Kristen
Test

22859 Posts

Posted - 2004-09-27 : 06:58:59
I have a table which contains "options" - things that the application needs to be able to do "This way or That way". For example, number of products on a page; default sort order; that sort of thing.

CREATE TABLE MyOptions
(
MyKey varchar(10), -- PK
MyValue varchar(100)
)

INSERT INTO MyOptions
SELECT 'OPT1', '6' UNION ALL
SELECT 'OPT2', 'Price'

Now then, in an SProc lets say I want two options, what's the best way to get them?

SELECT @Opt1 = CASE WHEN MyKey = 'OPT1' THEN MyValue ELSE @Opt1 END,
@Opt2 = CASE WHEN MyKey = 'OPT2' THEN MyValue ELSE @Opt2 END
FROM MypOtions
WHERE MyKey IN ('OPT1', 'OPT2')
-- PK = Table 'MyOptions'. Scan count 2, logical reads 4, physical reads 0, read-ahead reads 0.
-- NoPK = Table 'MyOptions'. Scan count 1, logical reads 1, physical reads 0, read-ahead reads 0.

or
SELECT @Opt1 = O1.MyValue,
@Opt2 = O2.MyValue
FROM MyOptions O1,
MyOptions O2
WHERE O1.MyKey = 'OPT1'
AND O2.MyKey = 'OPT2'
-- PK = Table 'MyOptions'. Scan count 2, logical reads 4, physical reads 0, read-ahead reads 0.
-- NoPK = Table 'MyOptions'. Scan count 2, logical reads 2, physical reads 0, read-ahead reads 0.


or something else? (Such as selecting the appropriate rows into a @TableVar and JOINing them to subsequent queries.

Tests were done with 100 rows in the MyOptions table. It looks like the no Primary Key and Query 1 are the best - but it kinda goes against the grain!

Kristen

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-27 : 08:35:56
i may be wrong, but:
2nd is not an option because you do a cross join.
1st if the pk is there it scans the pk cache first, then the table, if no pk then just the table.

just my thought...

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-09-27 : 10:25:34
I don't think it comes out as a cros join does it? I explicitly set the PK field for each table O1 and O2 - so only one record will be selected from each table.

FROM MyOptions O1,
MyOptions O2
WHERE O1.MyKey = 'OPT1'
AND O2.MyKey = 'OPT2'

I need a bit of help understanding "1st if the pk is there it scans the pk cache first, then the table, if no pk then just the table" - so there appear to be two scans because of the PK cache scan then the Table scan? Why doesn't it just use the PK cache?

Hope I'm not being too thick!

Kristen
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2004-09-27 : 11:02:17
i didn't mean it comes out as a cross join. but you scan the table 2 times because of the join.

i would think that it does take the value from PK - it makes sense to me. it's just an idea.
i don't know if that's really what's going on...
isn't there a way to see what gets selected first???


Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -