[英]AVL tracking system database
我是一家公司的新 DBA,使用跟踪系统超过 1K 设备,每 10 秒更新一次
数据库是 Postgresql v11,但是当我们做一些查询时需要时间,postgresql 有资格承担这种重任吗?? 还是我们应该转移到 sql server ?? 有没有什么技术可以提高性能? 还是应该从查询开始?
-- View: public.api_devicelist_v2
-- DROP VIEW public.api_devicelist_v2;
CREATE OR REPLACE VIEW public.api_devicelist_v2 AS
SELECT COALESCE(tc_positions.attributes -> 'c_door'::text, '0'::jsonb)::integer AS door_icon,
CASE
WHEN age(now(), tc_positions.fixtime::timestamp with time zone) > '01:00:00'::interval AND tc_positions.speed > 0::double precision THEN 5
WHEN age(now(), tc_positions.fixtime::timestamp with time zone) > '01:00:00'::interval AND tc_positions.speed = 0::double precision THEN 6
WHEN tc_positions.fixtime IS NULL THEN 7
WHEN COALESCE(tc_positions.attributes -> 'c_ign'::text, '0'::jsonb) = '0'::jsonb AND tc_positions.speed > 0::double precision THEN 1
WHEN COALESCE(tc_positions.attributes -> 'c_ign'::text, '0'::jsonb) = '1'::jsonb AND tc_positions.speed > 0::double precision THEN 2
WHEN COALESCE(tc_positions.attributes -> 'c_ign'::text, '0'::jsonb) = '0'::jsonb AND tc_positions.speed = 0::double precision THEN 3
WHEN COALESCE(tc_positions.attributes -> 'c_ign'::text, '0'::jsonb) = '1'::jsonb AND tc_positions.speed = 0::double precision THEN 4
ELSE 0
END AS icon_motion,
tc_devices.id,
tc_devices.name,
date_part('epoch'::text, timezone('utc'::text, tc_positions.servertime))::integer AS servertime,
date_part('epoch'::text, timezone('utc'::text, tc_positions.fixtime))::integer AS fixtime,
COALESCE(tc_positions.latitude, '0'::double precision) AS latitude,
COALESCE(tc_positions.longitude, '0'::double precision) AS longitude,
COALESCE(tc_positions.speed, '0'::double precision) AS speed,
COALESCE(tc_positions.course, '0'::double precision) AS course,
COALESCE(tc_positions.attributes -> 'c_ign'::text, '0'::jsonb) AS engine,
COALESCE(tc_positions.attributes -> 'c_door'::text, '0'::jsonb) AS door,
COALESCE(tc_positions.attributes -> 'c_doorMove'::text, '0'::jsonb) AS door_move,
COALESCE(tc_positions.attributes -> 'c_ls_door'::text, 'false'::jsonb) AS prev_door,
COALESCE(tc_positions.attributes -> 'c_ls_ign'::text, 'false'::jsonb) AS prev_engine,
COALESCE(tc_positions.valid, false) AS valid,
tc_devices.type,
tc_devices.uniqueid
FROM tc_devices
JOIN tc_positions ON tc_devices.positionid = tc_positions.id
WHERE tc_devices.positionid IS NOT NULL;
ALTER TABLE public.api_devicelist_v2
OWNER TO postgres;
查询 2
SELECT id, protocol, deviceid, servertime, devicetime, fixtime, valid, latitude, longitude, altitude, speed, course, address, attributes2, accuracy, network, attributes
FROM public.tc_positions where deviceid=98743
order by id desc limit 1
但是这个简单的查询可能需要很长时间才能执行锁定???
SELECT id, protocol, deviceid, servertime, devicetime, fixtime, valid, latitude, longitude, altitude, speed, course, address, attributes2, accuracy, network, attributes
FROM public.tc_positions where deviceid=98743
and fixtime between '2019-05-05 14:08:22' and '2019-09-06 14:08:22'
桌子 ::
-- Table: public.tc_positions
CREATE TABLE public.tc_positions
(
id integer NOT NULL DEFAULT nextval('tc_positionsne_id_seq'::regclass),
protocol character varying(128) COLLATE pg_catalog."default",
deviceid integer NOT NULL,
servertime timestamp without time zone NOT NULL DEFAULT now(),
devicetime timestamp without time zone NOT NULL,
fixtime timestamp without time zone NOT NULL,
valid boolean NOT NULL,
latitude double precision NOT NULL,
longitude double precision NOT NULL,
altitude double precision NOT NULL,
speed double precision NOT NULL,
course double precision NOT NULL,
address character varying(512) COLLATE pg_catalog."default",
attributes2 character varying(4000) COLLATE pg_catalog."default",
accuracy double precision NOT NULL DEFAULT 0,
network character varying(4000) COLLATE pg_catalog."default",
attributes jsonb,
CONSTRAINT id PRIMARY KEY (id),
CONSTRAINT fk_positions_deviceid FOREIGN KEY (deviceid)
REFERENCES public.tc_devices (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.tc_positions
OWNER to postgres;
-- Index: device_id_id
CREATE INDEX device_id_id
ON public.tc_positions USING btree
(id DESC, deviceid)
TABLESPACE pg_default;
ALTER TABLE public.tc_positions
CLUSTER ON device_id_id;
-- Index: device_id_idx2
CREATE INDEX device_id_idx2
ON public.tc_positions USING btree
(id)
TABLESPACE pg_default;
-- Index: deviceid_fixtime
CREATE INDEX deviceid_fixtime
ON public.tc_positions USING btree
(deviceid, fixtime)
TABLESPACE pg_default;
-- Index: devicevalidtime2
CREATE INDEX devicevalidtime2
ON public.tc_positions USING btree
(deviceid, devicetime, valid)
TABLESPACE pg_default;
-- Index: fixture
CREATE INDEX fixture
ON public.tc_positions USING btree
(fixtime, deviceid, valid)
INCLUDE(valid)
TABLESPACE pg_default;
-- Index: tc_positions_devicetime_idx2
-- DROP INDEX public.tc_positions_devicetime_idx2;
CREATE INDEX tc_positions_devicetime_idx2
ON public.tc_positions USING btree
(devicetime DESC)
TABLESPACE pg_default;
谢谢你
您需要tc_devices.positionid
上的索引来提高查询 1 的性能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.