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)
 query method

Author  Topic 

flchico
Starting Member

46 Posts

Posted - 2007-05-17 : 11:12:23
Supposed I have 3 fields
cField1
cField2
cField3

and I have another field 'cFull' that has the data combined of field1+field2+field3.

so if:

cField1 = '111'
cField2 = '222'
cField3 = '333'
then
cFull = '111222333'

I have an index 'fields' (field1,field2,field3) and another index on field 'cFull'.

When doing a query what is faster and more efficient,
WHERE cField1 = 'xxx' AND cField2 = 'yyy' and cfield3 = 'zzz'

OR
WHERE cFull = 'xxxyyyzzz'

Thanks.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-17 : 11:29:21
WHERE cFull = 'xxxyyyzzz'

have you tried it, measured it?
what kind of results do you get?


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

flchico
Starting Member

46 Posts

Posted - 2007-05-17 : 11:35:54
Thanks for the response.

I haven't tested it with querying each field. My question was just a general rule about speed and how sql works best, basically about how the engine performs more efficiently.

I was hoping that the answer would have been going with the fields that way I avoid creating an extra field for the sake of speed in special circumstances.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-17 : 11:40:25
ohh... well then you create a covering index that contains all 3 columns.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

flchico
Starting Member

46 Posts

Posted - 2007-05-17 : 12:59:22
Thanks again for your help. Is there a good book you recommend regarding how the indexing works, how sql server uses the indexes, how it picks them. Lately I've done quite a few queries and it's funny how depending on certain things that you think would never affect the query it affects it greatly going from a index seek to a scan etc.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-17 : 14:03:23
these 3 will give you everything you need:
http://www.insidesqlserver.com/thebooks.html

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-05-17 : 14:04:49
Note that the selectivity of the FIRST key in the index may be decisive in whether SQL Server chooses to use that index, or not.

Kristen
Go to Top of Page
   

- Advertisement -