i am new DBA in a company using a tracking system for up than 1K device , and update for every 10 sec
the database is Postgresql v11 , but when we do some query it take time , is postgresql qualify for this kind of heavy duty ?? or should we move to sql server ?? is there any technic will enhance the performance ?? or should i start with the query ??
-- 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;
Query 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
But it take a long to excute for this simple query maybe locking ???
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 ::
-- 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;
thank you
您需要tc_devices.positionid
上的索引来提高查询 1 的性能。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.