DB2/400 SQL : "ALLOCATE" instruction - sql

A DB2 / 400 SQL question: In the statement below, when creating a column with the "ALLOCATE"
statement, does this mean that the database engine creates the column with an initial size of 20 Mega?
Because the analysis of the system table indicates that the column with a column of 2G, 7M.
Does the size indicated in the "LENGHT" column correspond to the size allocated, or to the maximum size of the column ?

Db2 for IBM i stores table data in two parts.
The Fixed length row-buffer table space and an overflow space.
If a table just has fixed length columns, all the data is in the table space.
ALLOCATE(x) means that the Db allocates x bytes in the table space for the column and the rest is stored in the overflow space.
The default for varying types is allocate(0), so theoretically the entire varying value is store in the overflow space.
Reality is varchar(30) or smaller is stored in the fixed length table space for performance, unless you explicitly specify allocate(0).
The reason it matters, if a query access both the fixed length table space and the overflow space, then 2 I/Os are required to retrieve all the data.
IBM recommends using an allocate(x) where x is large enough to handle at least 80% of values you have.
As you can see for yourself, length is the maximum size for the column.
IBM ACS's schema tool, for one example, shows you the allocated length...
create table mytable (
mykey integer
, myclob clob(200000) allocate(2000)
);

Related

Should I define a column type from actual length or nth power of 2(Sql Server )?

Should I define a column type from actual length to nth power of 2?
The first case, I have a table column store no more than 7 charactors,
will I use NVARCHAR(8)? since there maybe implicit convert inside Sql
server, allocate 8 space and truncate automatic(heard some where).
If not, NCHAR(7)/NCHAR(8), which should be(assume the fixed length is 7)
Any performance differ on about this 2 cases?
You should use the actual length of the string. Now, if you know that the value will always be exactly 7 characters, then use CHAR(7) rather than VARCHAR(7).
The reason you see powers-of-2 is for columns that have an indeterminate length -- a name or description that may not be fixed. In most databases, you need to put in some maximum length for the varchar(). For historical reasons, powers-of-2 get used for such things, because of the binary nature of the underlying CPUs.
Although I almost always use powers-of-2 in these situations, I can think of no real performance differences. There is one. . . in some databases the actual length of a varchar(255) is stored using 1 byte whereas a varchar(256) uses 2 bytes. That is a pretty minor difference -- even when multiplied over millions of rows.

Memory reserved according to the defined field size or just the size of the data inside?

In HANA, there's a column of type NVARCHAR(4000) with value ThisISaString, is the RAM that is being used = 4000 or 13?
If it reserves 4000, then this space could really add up when you have a lot of records.
I am trying to decide how big I should make my text fields.
What I make of your question in its current form is how SAP HANA handles variable length strings when it comes to presenting it to the client (I take from your intention to reserve a buffer.
Thus, I'm not going to discuss what happens inside of HANA when you enter a value into a table - this is rather complex and depends on the table type used (column, row, external, temporary...)
So, for the client application, a (N)VARCHAR
will result in a string with the length of the stored value, i.e. no padding (with spaces at the end) will happen.

Oracle: How to convert row count into data size

I would like to know how do i convert number of row into size like in MB or KB?
Is there a way to do that or formula?
The reason I doing this is because I would like to know given this set of data but not all in tablespace, how much size is used by this set of data.
Thanks,
keith
If you want an estimate, you could multiple the row count with the information from user_table.avg_row_len for that table.
If you want the real size of the table on disk, this is available user_segments.bytes. Note that the smallest unit Oracle will use is a block. So even for an empty table, you will see a value that is bigger tzen zero in that column. That is actual size of the space reserved in the tablespace for that table.

What is the correct way to implement a BLOB field in DB2/iSeries?

We are implementing a file upload and storage field in our DB2 database. Right now the file upload column is defined as BLOB(5242880) as follows:
CREATE TABLE MYLIB.MYTABLE (
REC_ID FOR COLUMN RECID DECIMAL(10, 0) GENERATED ALWAYS AS IDENTITY (
START WITH 1 INCREMENT BY 1
NO MINVALUE NO MAXVALUE
NO CYCLE NO ORDER
CACHE 20 )
,
[other fields snipped]
FORM_UPLOAD FOR COLUMN F00001 BLOB(5242880) DEFAULT NULL ,
CONSTRAINT MYLIB.MYCONSTRAINT PRIMARY KEY( REC_ID ) )
RCDFMT MYTABLER ;
Is this the correct way to do this? Should it be in its own table or defined a different way? I'm a little nervous that it's showing it as a five-megabyte column instead of a pointer to somwhere else, as SQL Server does (for example). Will we get into trouble defining it like this?
There's nothing wrong with storing the BLOB in a DB2 column, but if you prefer to store the pointer, look at DataLinks. http://pic.dhe.ibm.com/infocenter/iseries/v7r1m0/topic/sqlp/rbafyusoocap.htm
Unless you specify the ALLOCATE clause, the data itself is stored in the "variable length" aka "overflow" space of the table. Not the fixed length space where the rest of the row is.
So if you don't have ALLOCATE and the file is only 1MB, you only use 1MB of space to store it, not the 5MB max you've defined.
Note this means means the system has to do twice the I/O when accessing data from both areas.
Datalinks have the same I/O hit.
From a performance standpoint,
- Make sure you only read the BLOB if you need to.
- if 90% or more of the BLOBs are say < 1MB, you could improve performance at the cost of space by saying ALLOCATE(1048576) while still allowing for the full 5MB to be stored. The first 1MB would be in the row the last 4MB in the overflow.
Charles

Is there a way around the 8k row length limit in SQL Server?

First off, I know that in general having large numbers of wide columns is a bad idea, but this is the format I'm constrained to.
I have an application that imports CSV files into a staging table before manipulating them and inserting/updating values in the database. The staging table is created on the fly and has a variable number of NVARCHAR colums into which the file is imported, plus two INT columns used as row IDs.
One particular file I have to import is about 450 columns wide. With the 24 byte pointer used in a large NVARCHAR column, this adds up to around 10k by my calculations, and I get the error Cannot create a row of size 11166 which is greater than the allowable maximum row size of 8060.
Is there a way around this or are my only choices modifying the importer to split the import or removing columns from the file?
You can use text/ntext which uses 16 bytes pointer. Whereas varchar/nvarchar uses 24bytes pointer.
NVARCHAR(max) or NTEXT can store the data more than 8kb but a record size can not be greater than 8kb till SQL Server 2012. If Data is not fitted in 8kb page size then the data of larger column is moved to another page and a 24 bytes(if data type is varchar/nvarchar) pointer is used to store as reference pointer in main column. if it is text/ntext data type then 16 bytes poiner is used.
For Details you can Visit at following links :
Work around SQL Server maximum columns limit 1024 and 8kb record size
or
http://msdn.microsoft.com/en-us/library/ms186939(v=sql.90).aspx
If you are using SQL Server 2005, 2008 or 2012, you should be able to use NVARCHAR(max) or NTEXT which would be larger than 8,000 characters. MAX will give you 2^31 - 1 characters:
http://msdn.microsoft.com/en-us/library/ms186939(v=sql.90).aspx
I agree that Varchar or nvarchar (Max) is a good solution and will probably work for you, but completeness I will suggest that you can also create more than one table with the two tables having a One-to-One relationship.