on 2010 Oct 11 5:39 PM
Any disadvantage, if a long varchar is used instead of a varchar with explizit length?
Request clarification before answering.
The short answer is no, a string is a string.
It is the actual length of each value, not whether it is declared as binary, character and/or long, that determines how the data is stored in the database.
The long answer is, there are many differences in the way strings are treated in SQL code, and in many applications, depending on binary versus character versus long versus NVARCHAR, which collation / character set, and so on.
Also, the declared maximum length of a colum does have some performance implications for indexing, function and subquery caching and column statistics used by the query optimizer. And LONG VARCHAR does have a declared maximum length, it just happens to be implicitly declared as 2147483648.
My book goes on for two pages on the picky details, and they've gotten more interesting since Version 9, but that's not what you asked for.
Here's how I choose the data type for an ordinary (text) string column: If there is an absolutely well known maximum length, I specify it; e.g., CHAR ( 200 ) for employee_name. If the column's going to be important for performance, such as a primary key column in a 20 million row table, I think long and hard about the maximum length. Otherwise, if there is ANY DOUBT about how long the string will grow, I use LONG VARCHAR; e.g., picture_description, message_text, geographic_location.
So... I have a lot of LONG VARCHAR columns in my databases AND variables in my SQL code, and they have caused zero problems.
Caveat: I write very little application code, where LONG VARCHAR can wreak havoc with the client-server interface.
And I do not have to deal with fascist database designers who insist that phone_number cannot be VARCHAR ( 200 ) but must be broken down into the constituent subfields like area_code VARCHAR ( 3 ), or that zip_code must be VARCHAR ( 5 ), or that first_name and last_name are used to validate a credit card... crap like that is what makes me abandon so many shopping carts (how do I enter 'BRECK CARTER RISINGROAD INC'?)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Breck, in you book you say, that the width of a column will determine if a compressed or a hash B-tree index is used. So what will be the case for a long varchar and is it better or not?
AFAIK LONG VARCHAR is not a good choice for an important index. FWIW the book was written for SQL Anywhere Version 9.0.0, and it was checked by Sybase for technical accuracy for that version.
I want to add a point to Breck's answer. Breck is largely correct in that, in the main, the SQL Anywhere server doesn't differentiate between VARCHAR and LONG VARCHAR types in a semantic way. However, there are circumstances where having a LONG type does matter. For example, the server memoizes prior executions of sub-selects so that the sub-select does not have to be executed on each invocation. However, to avoid expensive copying of BLOB (or CLOB) values at execution time, memoization will not occur if the expression's declared type is LONG.
The best answer, in my opinion, is to use the most appropriate type for every domain. In particular, in my view it is a mistake to use LONG types unnecessarily.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Do any of these bad things happen for, say, the declared type VARCHAR ( 32767 )? (which is the point past which you must use LONG VARCHAR)
Phrases like "the most appropriate domain" and "unnecessarily" are not helpful in the real world. Folks who don't develop actual business databases, but DO SEE existing databases that perform badly, tend to promote so-called "best practices" that are nanny-like in nature. My favorite example is "Never Use Select Star" which identifies the speaker as someone who does not write sophisticated business applications for a living, but sees a lot of badly-written ones.
I am unaware of any "bad things" that occur with VARCHAR(32K) or less.
Ah, yes, it is a fact that many (most? almost all?) client interfaces don't handle long strings very well. FWIW your answer talks only of performance.
@Breck: From an application developer's point of view, I would not use long varchar for, say, names of persons or streets and the like. It may be hard to set a limit but usually that comes by interactions with different systems (that usually have limits) or the fact that most apps just won't handle such very long names correctly (just as a report won't print names with 100 k chars...). And so I prefer a hardcoded limit in the database over the hidden assumption in the according apps (or the dev's minds) that the theoretical maximum length won't ever be used.
@Karim: you should change your comment to become an answer, because it has really important information and comments might be overlooked.
(As per Martin's request, I am turning my comment into an answer.)
I want to add that using long varchar/nvarchar/binary when it is not necessary will have a SERIOUS impact on the client side if your client is a JDBC application using the iAnywhere/SQL Anywhere JDBC driver. The iAnywhere/SA JDBC driver will do its very best to perform wide fetches (i.e. fetch multiple rows at a time) when retrieving result sets from the server. BUT, if any of the columns in the result set are declared long varchar/nvarchar/binary, then the JDBC driver will have no choice but to drop down into single fetch mode due to the fact that the long columns will need to be getdata'd.
By the same token, the Remote Data Access layer within SA will also attempt to perform wide fetches from the remote server over ODBC; but once again, if any of the columns are declared long, the Remote Data Access layer will also drop into single fetch mode in order to perform getdata's on the long columns.
Hence, from a JDBC client or Remote Data Access standpoint, declaring a column long when it really does not need to be will have fairly significant performance implications.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
64 | |
8 | |
7 | |
7 | |
6 | |
5 | |
5 | |
4 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.