Re: Que es mejor NULL o CERO

From: Emanuel Calvo Franco <postgres(dot)arg(at)gmail(dot)com>
To: motum hesa <motums(at)gmail(dot)com>
Cc: pgsql-es-ayuda(at)postgresql(dot)org
Subject: Re: Que es mejor NULL o CERO
Date: 2009-06-26 13:43:30
Message-ID: f205bb120906260643x191d733cu70539aa97db05d20@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

El 25 de junio de 2009 20:52, motum hesa<motums(at)gmail(dot)com> escribió:
> Muchas gracias a los comentarios de todos, son muy interesantes, ya lei los
> links que me mandaron y tomare en cuenta las sugerencias, sin embargo me
> asalta otra duda sobre lo mismo,... si declaro un int de 4 bytes y guardo un
> 0 es seguro que se reservan los 4 bytes.. pero si guardo un null tambien se
> reservan los 4 bytes?, saludos y gracias
>
>

Realicé unos bench (obviamente fueron hechos on-the-fly :) = propenso a errores.

CREATE TABLE pruebaNULL(
noNULL int NOT NULL default round(random()*1000)::int,
siNULL int)

-- sinull es la columna que contendrá los valores NULL.

insert into pruebaNULL(siNULL) values(select
round(random()*1000)::int); --varias veces

insert into pruebaNULL(siNULL)
(
SELECT CASE
WHEN (generate_series::int > 256 and generate_series::int < 2000) THEN NULL
ELSE generate_series::int
END
FROM generate_series(1,100000)
)

parapruebas=# create index noNULL_ix on pruebanull (noNULL);
CREATE INDEX
parapruebas=# create index siNULL_ix on pruebanull (siNULL);
CREATE INDEX

REINDEX TABLE pruebaNULL;

parapruebas=# explain analyze select * from pruebanull where siNULL = 1890;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on pruebanull (cost=12.13..480.62 rows=500 width=8)
(actual time=0.312..0.312 rows=0 loops=1)
Recheck Cond: (sinull = 1890)
-> Bitmap Index Scan on sinull_ix (cost=0.00..12.01 rows=500
width=0) (actual time=0.305..0.305 rows=0 loops=1)
Index Cond: (sinull = 1890)
Total runtime: 0.368 ms
(5 rows)

parapruebas=# explain analyze select * from pruebanull where noNULL = 1890;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on pruebanull (cost=12.13..480.62 rows=500 width=8)
(actual time=0.149..0.149 rows=0 loops=1)
Recheck Cond: (nonull = 1890)
-> Bitmap Index Scan on nonull_ix (cost=0.00..12.01 rows=500
width=0) (actual time=0.143..0.143 rows=0 loops=1)
Index Cond: (nonull = 1890)
Total runtime: 0.216 ms
(5 rows)

parapruebas=# explain analyze select * from pruebanull where siNULL = 2500;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on pruebanull (cost=12.13..480.62 rows=500 width=8)
(actual time=0.126..0.155 rows=2 loops=1)
Recheck Cond: (sinull = 2500)
-> Bitmap Index Scan on sinull_ix (cost=0.00..12.01 rows=500
width=0) (actual time=0.108..0.108 rows=2 loops=1)
Index Cond: (sinull = 2500)
Total runtime: 0.243 ms
(5 rows)

parapruebas=# explain analyze select * from pruebanull where noNULL = 2500;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------
Bitmap Heap Scan on pruebanull (cost=12.13..480.62 rows=500 width=8)
(actual time=0.063..0.063 rows=0 loops=1)
Recheck Cond: (nonull = 2500)
-> Bitmap Index Scan on nonull_ix (cost=0.00..12.01 rows=500
width=0) (actual time=0.055..0.055 rows=0 loops=1)
Index Cond: (nonull = 2500)
Total runtime: 0.131 ms
(5 rows)

Tengo entendido que en la mayoría de los motores, los valores NULL
afectan los indices.
Si hay algo de este bench que no concuerde haganmelo saber.

--
Emanuel Calvo Franco
ArPUG [www.arpug.com.ar] / AOSUG Member
www.emanuelcalvofranco.com.ar

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message FRANK RADA 2009-06-26 14:25:07 soporte pgsql para PHP en servidor apache (FGS) bajo ubuntu linux
Previous Message Emanuel Calvo Franco 2009-06-26 13:15:45 Re: [pgsql-es-ayuda] necesitamosl la versión 8.1.9 de postgresql, pero no está en el respositorio