24

Can anyone provide any concrete evidence of performance when comparing

int = int

and:

string = string

in WHERE clause in MS-SQL.

For example

select *
from Samples
where SamplesID = 5

and:

select *
from Samples
where Name = 'Shampoo'

Is there difference or is performance the same?

4
  • 1
    On a samples table I would expect the SamplesId to be the clustered index. The presence and or types of index have a massive effect. Is this a real or a theoretical question? Nov 14, 2011 at 16:17
  • @SteveHenderson change your comment to be "expect the SamplesId to be the primary key", and I'd agree with you 100% :) Id columns usually designate keys, which may or may not be the clustered index. Of course, that's just an assumption... Nov 14, 2011 at 16:23
  • @StuartAinsworth No - I would also expect it to be the clustered index. With a simple lookup table (Id and Name, with some other stuff) I would expect the physical ordering of that lookup table to be by the Id (Primary key and clustered index). Nov 14, 2011 at 16:30
  • I think that any test to answer this question should be between a lookup on an indexed int column and a lookup on an indexed varchar (or char) column. In any situation where the performance matters, you would likely want to have an index on the column being used for the lookup. I expect that a lookup on an int column will almost always be faster, but how much faster will probably also depend greatly on the kind of data. If the string column contains a lot of rows that all start off with the same sequence of characters, then I think the index on that string column will be less effective. Nov 14, 2011 at 17:03

4 Answers 4

12

OK interesting question always took it as read that integer was quicker and never actually tested it. I took 1M Random Surnames and types from a list of Contacts from my data into a scratch database with no indices or primary key just raw data. No measurement was made over the range of my data in either of the columns has not been standardised so reflects the reality of my database rather than a pure statistical set.

select top 100 * from tblScratch where contactsurname = '<TestSurname>' order by NEWID()
select top 100 * from tblScratch where contacttyperef = 1-22 order by NEWID()

The Newid is there to randomise the data list out each time. Quickly ran this for 20 surnames and 20 types. Queries were run surname than ref then surname. Searching for the reference number was almost 4x quicker and used about 1/2 so the books were right all those years ago.

String -
SELECT TOP 100 * FROM tblScratch WHERE contactsurname = 'hoare' ORDER BY NEWID()

Duration 430ms
Reads 902
CPU 203

Integer -
SELECT TOP 100 * FROM tblScratch WHERE contacttyperef = 3 ORDER BY NEWID()

Duration 136ms
Reads 902
CPU 79
2
  • so this means that comparing integers is less CPU intensive thus it resolves in better performance. is there any documentations from Microsoft on how comparison actually works?
    – user275683
    Nov 14, 2011 at 16:46
  • 11
    It's not really a good test (sorry), and potentially misleading because ordering by NewID() introduces a totally unrealistic element - it generates and orders rows by a GUID. The optimiser will not necessarily do this after interpreting the WHERE statement. Can you take the TOP and the Order By NEWID() out and retest? Nov 14, 2011 at 16:47
3

Put both of your queries in the same query window. At the very top (before either of these queries) put: SET STATISTICS IO ON

When you run the code run it with the option "Include Actual Execution Plan" (an icon on your toolbar which looks like three little boxes, about 7 icons to the right of the Execute button)

This will result in three tabs in your results: Results, Messages, Plan. The Messages and the Plan will show you the IO cost and the full Execution cost.

The query with the bigger numbers has the highest cost! This method will allow you to prove to yourself which query has the lowest cost (highest performance)

1
  • 1
    BTW, the reason I say to test it, rather than providing a textbook answer is that the optimiser runs a probability-based plan, and depending on size and indexes (as Stuart Ainsworth points out) is that sometimes it is faster to do a full table scan (especially with a SELECT *) than to perform an index lookup before getting the data, so the optimiser would ignore the index. Take textbook advice as a good starting point, but you really have to test it, and retest as table sizes and structures change Nov 14, 2011 at 16:44
3

As Steve pointed out in the comments, the presence and constitution of indexes will greatly impact your results; however, since SQL Server works with pages to lookup data, and narrower column types can store more data per page, using a narrow type can perform better than a wider type where there are more values to consider.

So, if you have a small table (few rows), it probably doesn't matter; big table? Put an index on the int column and it will probably out-perform an indexed varchar column.

Of course, what constitutes large or small tables depends on your hardware.

2
  • 3
    This is also only true if he has long varchar fields. since an int is 8 bytes, he needs to average over 6 characters per row of varchcar for int to be smaller
    – JNK
    Nov 14, 2011 at 16:40
  • 1
    Specifics often ruin a good rule-of-thumb, but you can't answer every question with "it depends". Tinyints (an integer value but not an int) are 1 byte, which is less than any varchar column (and equivalent to a char(1)). The rule still applies; the narrower the type, the better potential for performance. Jul 27, 2016 at 18:55
1

Comparing integers should be faster, at the very low level it will end up with 1 cmp instruction. Comparing string involves more instructions, and as a result, worse performance.
I assume you either have or don't have indexes on both fields, indexes are equally selective, number of records also the same

2
  • i understand that it should be faster but i'm trying to figure out if there is concrete way to prove that it is? does this also means that different OS system or even hardware might approach differently when comparing strings versus comparing integers
    – user275683
    Nov 14, 2011 at 16:38
  • I think the only 100% undeniable way to prove or refute it is to look into SQLServer source code that handles string and number comparision. Personally, I don't see how comparing non-empty strings (which is comparing of 2 arrays of numbers) can be faster then comparing just 2 numbers.
    – a1ex07
    Nov 14, 2011 at 16:53

Your Answer

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