Faster data type for one-length values

Поиск
Список
Период
Сортировка
Искать

Faster data type for one-length values

От:
André Volpato <andre.volpato@ecomtecnologia.com.br>
Дата:
Hello,

I need to store one-length values like '1', '2', '3' or '4' .

Today, this field is indexed with btree and is of type smallint (int2). 
How much performance will be improved if I change it to "char" (1 byte 
length) ? Or int2 is the best solution in this case ?

Thanks !

-- 
[]´s,

André Volpato
ECOM Tecnologia Ltda
andre.volpato@ecomtecnologia.com.br



Re: Faster data type for one-length values

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
=?ISO-8859-1?Q?Andr=E9_Volpato?=  writes:
> I need to store one-length values like '1', '2', '3' or '4' .

> Today, this field is indexed with btree and is of type smallint (int2). 
> How much performance will be improved if I change it to "char" (1 byte 
> length) ?

The improvement will be zero.  Because of alignment restrictions, you
don't save any space from making an index entry smaller than 4 bytes.

If you have several such fields adjacent in a table row, making them all
narrower can save space, but it doesn't help for standalone index
entries.

			regards, tom lane

Re: Faster data type for one-length values

От:
André Volpato<andre.volpato@ecomtecnologia.com.br>
Дата:
 Tom Lane escreveu: 
André Volpato <andre.volpato@ecomtecnologia.com.br> writes: 
I need to store one-length values like '1', '2', '3' or '4' .   
 
Today, this field is indexed with btree and is of type smallint (int2). 
How much performance will be improved if I change it to "char" (1 byte 
length) ?   
The improvement will be zero.  Because of alignment restrictions, you
don't save any space from making an index entry smaller than 4 bytes.

If you have several such fields adjacent in a table row, making them all
narrower can save space, but it doesn't help for standalone index
entries.
		regards, tom lane 

Tom,

The ammount of space saved seems pretty clear to me.

What are you saying is that the index behaviour is the same, for all types smaller than 4 bytes ?  For query performance, in a search is based on that standalone indexed field, would be any difference using :
a. char(1) ;  b. int2;  c. "char"

Thanks again !

-- 
[]´s,

André Volpato
ECOM Tecnologia Ltda
andre.volpato@ecomtecnologia.com.br
(41) 3014 2322

Re: Faster data type for one-length values

От:
André Volpato<andre.volpato@ecomtecnologia.com.br>
Дата:
André Volpato escreveu:
> Tom Lane escreveu:
>> =?ISO-8859-1?Q?Andr=E9_Volpato?=  writes:
>>   
>>> I need to store one-length values like '1', '2', '3' or '4' .
>>>     
>>
>>   
>>> Today, this field is indexed with btree and is of type smallint (int2). 
>>> How much performance will be improved if I change it to "char" (1 byte 
>>> length) ?
>>>     
>>
>> The improvement will be zero.  Because of alignment restrictions, you
>> don't save any space from making an index entry smaller than 4 bytes.
>>
>> If you have several such fields adjacent in a table row, making them all
>> narrower can save space, but it doesn't help for standalone index
>> entries.
>>
>> 			regards, tom lane
>>   
Tom,

The ammount of space saved seems pretty clear to me.

What are you saying is that the index behaviour is the same, for all 
types smaller than 4 bytes ?  For query performance, in a search is 
based on that standalone indexed field, would be any difference using :

a. char(1) ;  b. int2;  c. "char"

Thanks again !

Andre Volpato

Re: Faster data type for one-length values

От:
Alvaro Herrera <alvherre@commandprompt.com>
Дата:
André Volpato wrote:

> The ammount of space saved seems pretty clear to me.

Yeah, zero most of the time due to alignment.

> What are you saying is that the index behaviour is the same, for all 
> types smaller than 4 bytes ?  For query performance, in a search is 
> based on that standalone indexed field, would be any difference using :
> 
> a. char(1) ;  b. int2;  c. "char"

char(1) takes at least five bytes (more if the char is multibyte), so it
would be slower than the other two, ceteris paribus.

-- 
Alvaro Herrera                                http://www.CommandPrompt.com/
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

Re: Faster data type for one-length values

От:
Ron Mayer <rm_pg@cheapcomplexdevices.com>
Дата:
Alvaro Herrera wrote:
> André Volpato wrote:
> 
>> The ammount of space saved seems pretty clear to me.
> 
> Yeah, zero most of the time due to alignment.

So trading off more I/O for less CPU?

I wonder if for any I/O bound database servers
it might be worth packing tightly rather than
aligning indexes on one-byte data.

Re: Faster data type for one-length values

От:
Lew <lew@nospam.lewscanon.com>
Дата:
Ron Mayer wrote:
> Alvaro Herrera wrote:
>> André Volpato wrote:
>>
>>> The ammount of space saved seems pretty clear to me.
>> Yeah, zero most of the time due to alignment.
> 
> So trading off more I/O for less CPU?
> 
> I wonder if for any I/O bound database servers
> it might be worth packing tightly rather than
> aligning indexes on one-byte data.

The OP didn't say one-byte data, they said one-char data.

-- 
Lew
FAQ