0

I have a table 'Tab' with data such as:

 id |  value
---------------
 1  |  Germany
 2  |  Argentina
 3  |  Brasil
 4  |  Holland

What way of select is better by perfomane?

1. SELECT * FROM Tab WHERE value IN ('Argentina', 'Holland')

or

2. SELECT * FROM Tab WHERE id IN (2, 4)

I suppose that second select would be faster, because int comparison is faster than string. Is that true for MS SQL?

2
  • 1
    You should use query analyzer. Is the Id field a Primary Key? Commented Jul 10, 2014 at 11:16
  • 2
    Not enough information to answer. Is there an index on the table? Primary Key? How large is this table? Have you looked at the execution plan? The example given here is so trivial it won't make a difference. Commented Jul 10, 2014 at 11:18

2 Answers 2

3

This is a premature optimization. The comparison between integers and strings is generally going to have a minimal impact on query performance. The drivers of query performance are more along the lines of tables sizes, query plans, available memory, and competition for resources.

In general, it is a good idea to have indexes on columns used for either comparison. The first column looks like a primary key, so it automatically gets an index. The string column should have an index built on it. In general, indexes built on an integer column will have marginally better performance compared to integers built on variable length string columns. However, this type of performance difference really makes a difference only in environments with very high levels of transactions (think thousands of data modification operations per second).

You should use the logic that best fits the application and worry about other aspects of the code.

Sign up to request clarification or add additional context in comments.

Comments

0

To answer the simple question yes option 2 SELECT * FROM Tab WHERE id IN (2, 4) would be faster as you said because int comparison is faster.

One way to speed it up is to add indexes to your columns to speed up evaluation, filtering, and the final retrieval of results.

If this table was to grow even more you should also not SELECT * but SELECT id, value otherwise you may be pulling more data than you need.

You can also speed up your query's buy adding WITH(NOLOCK) as the speed of your query might be affected by other sessions accessing the tables at the same time. For example SELECT * FROM Tab WITH(NOLOCK) WHERE id IN (2, 4) . As mentioned below though adding nolock is not a turbo and should only be used in appropriate situations.

3 Comments

NOLOCK is not a magic turbo button and should not be used unless you know exactly why you're using it and the data integrity ramifications of it. Even if you're fine with dirty reads, it's a bad idea and bad practice to fall into.
Agreed, I have just added some of this information to my answer. In my situation if I did not have NOLOCK on my query's the speeds would be considerably slower.
If you need NOLOCK on all of your queries to get acceptable performance (at the expense of dirty reads and the other risks), your database is not configured or designed well. Fix the root cause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.