Does size of a VARCHAR column matter when used in queries [duplicate]
Asked Answered
S

4

9

Possible Duplicate:
is there an advantage to varchar(500) over varchar(8000)?

I understand that a VARCHAR(200) column containing 10 characters takes same amount of space as a VARCHAR(20) column containing same data.

I want to know if changing a dozen VARCHAR(200) columns of a specific table to VARCHAR(20) would make the queries run faster, especially when:

  • These columns will never contain more than 20 characters
  • These columns are often used in ORDER BY clause
  • These columns are often used in WHERE clause
    • Some of these columns are indexed so that they can be used in WHERE clause

PS: I am using SQL Server 2000 but will upgrade to later versions of SQL anytime soon.

Shovelhead answered 15/12, 2012 at 17:36 Comment(1)
While waiting for an answer, you could always try it and see what happens.Araiza
F
18

Yes, the length of varchar affects estimation of the query, memory that will be allocated for internal operation (for example for sorting) and as consequence resources of CPU. You can reproduce it with the following simple example.

1.Create two tables:

create table varLenTest1
(
    a varchar(100)
)

create table varLenTest2
(
    a varchar(8000)
)

2. Fill both of them with some data:

declare @i int
set @i = 20000

while (@i > 0)
begin 
    insert into varLenTest1 (a) values (cast(NEWID() as varchar(36)))
    set @i = @i - 1
end 

3. Execute the following queries with "include actual execution plan":

select a from varLenTest1 order by a OPTION (MAXDOP 1) ;
select a from varLenTest2 order by a OPTION (MAXDOP 1) ;

If you inspect execution plans of these queries, you can see that estimated IO cost and estimated CPU cost is very different: enter image description here

Foliose answered 15/12, 2012 at 20:44 Comment(0)
S
2

It does matter for the query optimiser when it will evaluate the best query path to perform your query. When more than one path will be available, it will calculate an I/O cost and other various parameters based on your query and from these, chose the one that will appears to him as the least costly.

This is not an absolute calculation, it's only an approximation process. Therefore, it can easily be thrown off if the apparent mean size required to manipulate the records from one table in memory is much bigger then what will be really necessary and the optimiser might chose a less performing path based on what it thinks would have be necessary for the others paths.

Having a realistic max size is also usefull to any other programmer that will come along looking at your code. If I have a variable that I want to display in a GUI, I might allocate much more space than neededd if I see that is backed by something like nvarchar(200) or nvarchar(2000) instead of nvarchar(20) if its size is never greater than that.

Sinhalese answered 15/12, 2012 at 18:11 Comment(0)
P
1

Here's a blog post that explains under what circumstances and why there are performance differences when using different column sizes (with tests and technical details):

Advanced TSQL Tuning: Why Internals Knowledge Matters

Palaeogene answered 15/12, 2012 at 17:45 Comment(2)
Not that relevant to the question asked here. It doesn't look at varchar(x) vs varchar(y) at all but compares varchar(max), text and char(399) which are different datatypes all stored in different ways. This is more relevant as to why you should not over declare varchar width IMO and Adam Machanic's memory grant video is also relevantRox
Here's the link mentioned web.archive.org/web/20180424213540/sqlblog.com/blogs/paul_white/…Valeric
B
1

Size matters

Always use the smallest data size that will accommodate the largest possible value. If a column is going to store values between 1 and 5, use tinyint instead of int.

This rule also applies to character columns. The smaller the data size, the less there is to read, so performance, over all, benefits. In addition, smaller size reduces network traffic. With newer technology, this tip seems less relevant, but don’t dismiss it out of hand. You’ll won’t regret being efficient from the get-go.

For more info visit http://www.techrepublic.com/blog/10-things/10-plus-tips-for-getting-the-best-performance-out-of-your-sql-server-data-types/

Bernat answered 15/12, 2012 at 17:46 Comment(1)
A varchar(200) column containing a 10 character string won't use any more network traffic than a varchar(20) column containing the same data.Rox

© 2022 - 2024 — McMap. All rights reserved.