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 2008 Forums
 Transact-SQL (2008)
 varchar compare vs int compare

Author  Topic 

aschaeffer
Starting Member

2 Posts

Posted - 2010-04-21 : 16:05:50

I have a table that currently contains a varchar(50) column that is used to categorize the record.

tbl_transaction
(
id PRIMARY,
transaction_type varchar(20),
...
)

This table is fairly large and is getting bigger (300,000 rows currently). Many of my queries use the transaction_type field to include/exclude records. For example

select * from tbl_transaction
where transaction_type in (select transaction_type from tbl_transaction_group where group = 'foobar')

I have noticed some performance issues in the more complicated queries. I have considered modifying this table to have transaction_type_id (INT) instead of transaction_type. transaction_type_id would have a foreign key to a tbl_transaction_type table.

Would this change increase the performance of my queries that do a lot of the varchar comparisons?

Thanks!


tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-21 : 16:14:08
Yes that should increase the performance.

Your query should be rewritten to use a join instead:

select t.* --explicitly list the columns here, do not use *
from tbl_transaction t
join tbl_transaction_group tg
on t.transaction_type = tg.transaction_type
where tg.group = 'foobar'

Is the transaction_type column indexed in both tables?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

aschaeffer
Starting Member

2 Posts

Posted - 2010-04-21 : 16:34:35
Thanks for your prompt response.

We have a Non_unique, Non-Clustered index on transaction_type along with other columns that are very often used to filter the result sets further.

The group column in tbl_transaction_group is indexed but not the transaction_type column.

I am going to modify my queries to use joins and index the transaction_type column in tbl_transaction_group. The switch to using ids instead of varchar in tbl_transaction is a slightly bigger change that we will work on in the next iteration.

Thanks for your help!

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-21 : 16:55:03
You're welcome.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -