126

I'm in the process of creating a table and it made me wonder.

If I store, say cars that has a make (fx BMW, Audi ect.), will it make any difference on the query speed if I store the make as an int or varchar.

So is

SELECT * FROM table WHERE make = 5 AND ...;

Faster/slower than

SELECT * FROM table WHERE make = 'audi' AND ...;

or will the speed be more or less the same?

googletorp
  • 33,075
  • 15
  • 67
  • 82

10 Answers10

116

Int comparisons are faster than varchar comparisons, for the simple fact that ints take up much less space than varchars.

This holds true both for unindexed and indexed access. The fastest way to go is an indexed int column.


As I see you've tagged the question postgreql, you might be interested in the space usage of different date types:

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
  • 15
    You are referring to pg 7.4. In modern versions, they take up 1byte+length if you have <126 bytes. Also note that the reason strings are much slower is often that collation-sensitive comparison is hugely expensive - not that the string takes more space. But the end result is the same, of course. – Magnus Hagander Feb 27 '10 at 12:32
  • @Magnus - thanks for the heads-up. Feel free to edit my answer as I see you have enough rep points. – Robert Munteanu Feb 27 '10 at 21:48
  • "not that the string takes more space"... strings of characters above minimal sizes take up a heck of a lot more space than even high-precision numbers, because a number (singular) has a fixed unit, strings are always aggregate types. 8 bytes for a 64-bit number 4 bytes per-character in a string, including either a length byte or struct; or another terminator character for incredibly naive implementations... – MrMesees May 13 '16 at 04:59
  • @RobertMunteanu Hey Robert, apologies I know this is an old post but can I kindly check...on the following: in order to query integers, i have to link each string column to another table (relationship). however, that means more joining operations are required for each query. How do i determine if this trade-off is worth it? Thank you! – AiRiFiEd Mar 06 '17 at 08:45
  • 4
    "Int comparisons are faster than varchar comparisons, for the simple fact that ints take up much less space than varchars" - this is NOT true _in general_. Depending on the DBMS you use and the exact data types and strings you want to insert, it may turn out that your (say) 8-byte ints are longer than ascii varchars holding some textual IDs of avg length 3-4 chars. So, this answer - being imprecise and lacking any specific context or experimental results - doesn't really answer the question. Everyone knows that varchars are _allowed_ to take much more space than ints, but they do NOT have to. – Marcin Wojnarski Apr 05 '19 at 12:08
  • 2
    Can you back up your claim here about indexed access? Every benchmark I've seen posted online says that varchar vs int are identical for indexed access, and you are posting no data nor reference to back up your claim. https://stackoverflow.com/a/48583244/834393 – Brettins Sep 16 '19 at 18:01
  • One simple thing that suggest search would be faster with integers is due to the comparison between 2 integer vs 2 string (varchar). the compare operation is constant .i.e. O(1) in case of integers but with strings its not constant (but depends on the length of the string). – Manjeet Rulhania Jul 22 '20 at 21:29
  • Computers are not that simple to deduce more memory => less performance. Indexed columns are highly processed datastructures whose size might not be a function of the width of the columns: When columns are small, indexes are alrger than the data in the columns to increase performance, and when they're large, they're smaller to increase performance. Further, data loading is completely pararell up to a certain degree. This answer should not be accepted. – Nearoo Jan 01 '22 at 16:42
46

Some rough benchmarks:

4 million records in Postgres 9.x

Table A = base table with some columns
Table B = Table A + extra column id of type bigint with random numbers
Table C = Table A + extra column id of type text with random 16-char ASCII strings

Results on 8GB RAM, i7, SSD laptop:

Size on disk:                A=261MB        B=292MB        C=322MB
Non-indexed by id: select count(*), select by id: 450ms same on all tables
Insert* one row per TX:       B=9ms/record        C=9ms/record
Bulk insert* in single TX:    B=140usec/record    C=180usec/record
Indexed by id, select by id:  B=about 200us       C=about 200us

* inserts to the table already containing 4M records

so it looks like for this setup, as long as your indexes fit in RAM, bigint vs 16-char text makes no difference in speed.

Grzegorz Luczywo
  • 9,962
  • 1
  • 33
  • 22
  • 9
    Very interesting. How come the difference is negligible? – Chibueze Opata Sep 09 '17 at 16:30
  • 1
    @ChibuezeOpata Before data can be compared, it has to be loaded into caches closer to the CPU than RAM. The overall comparison time is likely completely dominated by that loading. [i7 L1 & L2 caches are 64 bytes in size](https://stackoverflow.com/questions/14707803/line-size-of-l1-and-l2-caches), and you can likely load 64 succing bytes from RAM at once. Hence, as long as your data is smaller than 32 bytes (compare 32 to bytes to each other), the only difference is the cycles the CPU need for comparison, which is negligible. Just a guess, the actual answer is probably super complex. – Nearoo Jan 01 '22 at 16:22
  • As I see on your benchmarks sample you only use simple comparison operator but how about using complex comparison operator and other predefined functions for where clause and aggregate functions, I think this will show the best performance on what to use. – espongha Sep 14 '22 at 02:05
21

It will be a bit faster using an int instead of a varchar. More important for speed is to have an index on the field that the query can use to find the records.

There is another reason to use an int, and that is to normalise the database. Instead of having the text 'Mercedes-Benz' stored thousands of times in the table, you should store it's id and have the brand name stored once in a separate table.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Could you explain more? Do you mean instead of `Mercedes-Benz` to store thousands of times id `1`. For example table `car_brands`, columns `Brands` and `Id`. Row `Mercedes-Benz` and `1`. And in main table column `Brands` and value `1`. And when `SELECT`, then at first get `Id` from table `car_brands` and then `SELECT Something FROM main_table WHERE Brands = (SELECT Id FROM car_brands WHERE Brands = Mercedes-Benz)`. Or some other approach? – Andris Jan 12 '15 at 04:56
  • 3
    @user2118559: Yes, that is how you would store it. To get the data you would generally use a join rather than a subquery: `select something from main_table c inner join car_brands b on b.Id = c.Brands where b.Brands = 'Mercedes-Benz'`. – Guffa Jan 12 '15 at 09:12
  • 2
    Why the downvote? If you don't explain what it is that you think is wrong, it can't improve the answer. – Guffa Mar 29 '16 at 21:43
10

Breaking down to the actual performance of string comparison versus non-floats, in this case any size unsigned and signed does not matter. Size is actually the true difference in performance. Be it 1byte+(up to 126bytes) versus 1,2,4 or 8 byte comparison... obviously non-float are smaller than strings and floats, and thus more CPU friendly in assembly.

String to string comparison in all languages is slower than something that can be compared in 1 instruction by the CPU. Even comparing 8 byte (64bit) on a 32bit CPU is still faster than a VARCHAR(2) or larger. * Again, look at the produced assembly (even by hand) it takes more instructions to compare char by char than 1 to 8 byte CPU numeric.

Now, how much faster? depends also upon the volume of data. If you are simply comparing 5 to 'audi' - and that is all your DB has, the resulting difference is so minimal you would never see it. Depending upon CPU, implementation (client/server, web/script, etc) you probably will not see it until you hit few hundred comparisons on the DB server (maybe even a couple thousand comparisons before it is noticeable).

  • To void the incorrect dispute about hash comparisons. Most hashing algorithms themselves are slow, so you do not benefit from things like CRC64 and smaller. For over 12 years I developed search algorithms for multi-county search engines and 7 years for the credit bureaus. Anything you can keep in numeric the faster... for example phone numbers, zip codes, even currency * 1000 (storage) currency div 1000 (retrieval) is faster than DECIMAL for comparisons.

Ozz

Ozz Nixon
  • 159
  • 1
  • 8
7

Index or not, int is a lot faster (the longer the varchar, the slower it gets).

Another reason: index on varchar field will be much larger than on int. For larger tables it may mean hundreds of megabytes (and thousands of pages). That makes the performance much worse as reading the index alone requires many disk reads.

Konrad Garus
  • 53,145
  • 43
  • 157
  • 230
  • 3
    For example of 5 millions records of "audi", wouldn't the index only hold only one copy of string of "audi" and 5 millions integers of primary_key? Would the size difference really be that large, be it vchar or integer? – lulalala Mar 10 '17 at 03:03
  • You are right lulalala but for a column which is going to contain random strings the answer is fair enough. – Awais fiaz Jun 18 '19 at 08:22
6

In general the int will be faster. The longer is the varchar the slower it gets

anthares
  • 11,070
  • 4
  • 41
  • 61
3

Hint: If the possible values for the field make will never (or rarely) change, you can use ENUM as a compromise. It combines good speed with good readability.

Thomas Schaub
  • 418
  • 2
  • 9
  • 1
    Interesting, How will the speed difference be between ENUM and int? – googletorp Feb 27 '10 at 10:20
  • Does PostgresSQL have an `enum` data type? I though it was MySQL specific. – Robert Munteanu Feb 27 '10 at 10:26
  • Postgres has ENUM, but I don't think it's implemented quite the same way as MySQL. http://www.postgresql.org/docs/current/static/datatype-enum.html – googletorp Feb 27 '10 at 11:02
  • 2
    Performance wise, ENUM should perform more or less the same as int in the search field, but as varchar in the target list (because it has to transfer the whole string to the client for matched rows, not just the int) – Magnus Hagander Feb 27 '10 at 12:33
  • 2
    [Here an interesting read](http://komlenic.com/244/8-reasons-why-mysqls-enum-data-type-is-evil/) on why NOT to use enum in MySQL (just to add some fuel to the fire :D ) – Wilt Aug 17 '16 at 07:49
1

If you turn on indexing on either of the fields, it will be faster. As for your question, i think intis faster than varchar.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

Somewhat relative. Yes, INTs will be faster, but the question is if it is noticeable in your situation. Are the VARCHARs just some small words, or longer texts? and how many rows are in the table? If there are just a few rows it will most likely be entirely buffered in memory (when requested often), in that case you wont notice much difference. Then of course there is indexing, which gets more important when the table grows. Using SSD's might be faster then HD's with optimized queries. Also good disk-controllers sometimes speed up queries >10x . This might leave room for just using VARCHARs which makes reading and writing queries easier (no need to write complex joins) and speed up development. Purists however will disagree and always normalize everything.

Alex
  • 5,759
  • 1
  • 32
  • 47
0

as a developer of databases, our databases will use heap-based sorting algorithms in order to decrease extra memory-consuming. but when we use bucket-based sorting algorithm to improve it, in int32 and int64, it works(40% time off, with 0.1-billion records). But in varchar, it works even worse and seems hardly unchanged. even in Oracle, it is also seems to let VARCHAR-SORTING algorithm to be more quicker than now with their complicated sorting-rule in different languages.

Don
  • 1