PostgreSQL 8.4を使用し、次のようなテーブルを使用します。
create table log (
id bigint primary key,
first_sn bigint not null,
last_sn bigint not null
);
ここで、first_snとlast_snはシリアル番号の範囲を表し、100万を超える行を保持するテーブルでは、シリアル番号の範囲にシリアルのリスト内の要素が含まれるすべての行を検索する場合、どのような種類のインデックスとクエリを使用する必要がありますか数字。
たとえば、リスト[5348491、1230505、5882233]の場合、私は現在次のことを行っています。
select 5348491, *
from log
where 5348491 between first_sn and last_sn
union
select 1230505, *
from log
where 1230505 between first_sn and last_sn
union
select 5882233, *
from log
where 5882233 between first_sn and last_sn;
しかし、それは少し遅いです。
編集:そのようなクエリには約600ミリ秒かかり、10,000を超えるシリアル番号のリストで検索できるようにしたいと思います。
誰かがそれを要求したので、これが実際のテーブル、クエリ、および説明分析です(すべての列名がスペイン語であるため躊躇しましたが、前の例では「id」は「movimiento_id」になり、「first_sn」は「serial_inicial」になります、および'last_sn'は'serial_final'になります。'tipo_movimiento'はイベントのタイプであり、実際には結果セットをさらにフィルタリングする方法にすぎません):
CREATE TABLE movimiento
(
movimiento_id bigserial NOT NULL,
serial_inicial bigint NOT NULL,
serial_final bigint NOT NULL,
serial_chip bigint,
numero_telefono text,
fecha_movimiento timestamp without time zone DEFAULT now(),
producto_id integer NOT NULL,
usuario_id integer NOT NULL,
factura_proveedor text,
fecha_ingreso date,
fecha_venta date,
vendedor_id integer,
cliente_id integer,
tipo_movimiento text NOT NULL,
costo numeric(12,4),
precio numeric(10,2),
descuento double precision,
bodega_id integer NOT NULL DEFAULT 1,
fecha_activo timestamp without time zone,
factura text,
envio text,
documento text,
bodega_id_origen integer,
fecha date,
traslado_id integer,
detalle_factura_id bigint,
es_venta boolean DEFAULT false,
CONSTRAINT movimiento_pkey PRIMARY KEY (movimiento_id ),
CONSTRAINT movimiento_bodega_id_fkey FOREIGN KEY (bodega_id)
REFERENCES bodega (bodega_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT movimiento_bodega_id_origen_fkey FOREIGN KEY (bodega_id_origen)
REFERENCES bodega (bodega_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT movimiento_cliente_id_fkey FOREIGN KEY (cliente_id)
REFERENCES cliente (cliente_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT movimiento_producto_id_fkey FOREIGN KEY (producto_id)
REFERENCES producto (producto_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT movimiento_usuario_id_fkey FOREIGN KEY (usuario_id)
REFERENCES usuario (usuario_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT movimiento_vendedor_id_fkey FOREIGN KEY (vendedor_id)
REFERENCES vendedor (vendedor_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT movimiento_check CHECK (serial_final >= serial_inicial),
CONSTRAINT movimiento_costo_check CHECK (costo >= 0::numeric),
CONSTRAINT movimiento_descuento_check CHECK (descuento >= 0::double precision),
CONSTRAINT movimiento_precio_check CHECK (precio >= 0::numeric),
CONSTRAINT movimiento_tipo_movimiento_check CHECK (tipo_movimiento = ANY (ARRAY['Ingresado'::text, 'Vendido'::text, 'Entregado'::text, 'Regresado'::text, 'Eliminado'::text, 'Devuelto'::text, 'Inconforme'::text, 'Trasladado'::text, 'Consignado'::text, 'Devolucion Consignado'::text, 'Activado'::text, 'Devolucion Claro'::text, 'Asignado'::text, 'Fusion-Sale'::text, 'Fusion'::text, 'Separacion-Sale'::text, 'Separacion'::text]))
)
WITH (
OIDS=TRUE
);
クエリは次のとおりです。
explain analyze select 869461009867643, *
from movimiento
where (869461009867643 between serial_inicial and serial_final)
and tipo_movimiento = 'Ingresado'
union all
select 12121001477546, *
from movimiento
where 12121001477546 between serial_inicial and serial_final
and tipo_movimiento = 'Ingresado'
union all
select 354689040208615, *
from movimiento
where 354689040208615 between serial_inicial and serial_final
and tipo_movimiento = 'Ingresado';
そして、説明は分析します:
Append (cost=7542.94..185580.33 rows=232322 width=165) (actual time=93.222..571.928 rows=4 loops=1)
-> Bitmap Heap Scan on movimiento (cost=7542.94..61089.00 rows=90645 width=165) (actual time=93.220..206.248 rows=1 loops=1)
Recheck Cond: (tipo_movimiento = 'Ingresado'::text)
Filter: ((869461009867643::bigint >= serial_inicial) AND (869461009867643::bigint <= serial_final))
-> Bitmap Index Scan on tipo_movimiento_index (cost=0.00..7520.28 rows=375432 width=0) (actual time=66.445..66.445 rows=372409 loops=1)
Index Cond: (tipo_movimiento = 'Ingresado'::text)
-> Bitmap Heap Scan on movimiento (cost=7534.24..61080.30 rows=55815 width=165) (actual time=84.364..179.571 rows=2 loops=1)
Recheck Cond: (tipo_movimiento = 'Ingresado'::text)
Filter: ((12121001477546::bigint >= serial_inicial) AND (12121001477546::bigint <= serial_final))
-> Bitmap Index Scan on tipo_movimiento_index (cost=0.00..7520.28 rows=375432 width=0) (actual time=60.282..60.282 rows=372409 loops=1)
Index Cond: (tipo_movimiento = 'Ingresado'::text)
-> Bitmap Heap Scan on movimiento (cost=7541.75..61087.81 rows=85862 width=165) (actual time=173.876..186.082 rows=1 loops=1)
Recheck Cond: (tipo_movimiento = 'Ingresado'::text)
Filter: ((354689040208615::bigint >= serial_inicial) AND (354689040208615::bigint <= serial_final))
-> Bitmap Index Scan on tipo_movimiento_index (cost=0.00..7520.28 rows=375432 width=0) (actual time=60.294..60.294 rows=372409 loops=1)
Index Cond: (tipo_movimiento = 'Ingresado'::text)
Total runtime: 572.138 ms
a_horse_with_no_nameの例を使用したexplainanalyzeは次のとおりです。
Nested Loop (cost=7614.18..98703.44 rows=125144 width=173) (actual time=629.373..2919.334 rows=4 loops=1)
Join Filter: ((lista.serie >= movimiento.serial_inicial) AND (lista.serie <= movimiento.serial_final))
CTE lista
-> Values Scan on "*VALUES*" (cost=0.00..0.04 rows=3 width=8) (actual time=0.012..0.033 rows=3 loops=1)
-> Bitmap Heap Scan on movimiento (cost=7614.14..59283.04 rows=375432 width=165) (actual time=110.909..460.563 rows=372409 loops=1)
Recheck Cond: (tipo_movimiento = 'Ingresado'::text)
-> Bitmap Index Scan on tipo_movimiento_index (cost=0.00..7520.28 rows=375432 width=0) (actual time=107.182..107.182 rows=372409 loops=1)
Index Cond: (tipo_movimiento = 'Ingresado'::text)
-> CTE Scan on lista (cost=0.00..0.06 rows=3 width=8) (actual time=0.001..0.003 rows=3 loops=372409)
Total runtime: 2919.514 ms
したがって、a_horse_with_no_nameとCraig Ringerの提案を組み合わせて、3つのシリアル番号の検索は350ミリ秒未満で実行されました。10kで試してみましたが、3秒以上で完了しました。
create temporary table lista (
serie bigint
) on commit drop;
create index lista_index on lista using btree (serie);
insert into lista (select distinct serial_inicial from movimiento limit 10000);
analyze lista;
select serie, movimiento.*
from movimiento join lista on serie between serial_inicial and serial_final
where tipo_movimiento = 'Ingresado';