Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 跟踪系统数据库_Sql Server_Postgresql_Gps - Fatal编程技术网

Sql server 跟踪系统数据库

Sql server 跟踪系统数据库,sql-server,postgresql,gps,Sql Server,Postgresql,Gps,我是一家公司的新DBA,该公司对超过1K的设备使用跟踪系统,每10秒更新一次 数据库是Postgresql v11,但是当我们进行一些查询时需要时间, postgresql有资格承担这种重担吗??还是我们应该转向sql server?? 是否有任何技术可以提高性能?? 还是从查询开始 -- View: public.api_devicelist_v2 -- DROP VIEW public.api_devicelist_v2; CREATE OR REPLACE VIEW public.ap

我是一家公司的新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
但是执行这个简单的查询需要很长时间,可能是锁定

表::

-- 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的性能。

您可以在查询分析中包括计划、执行时间和总时间吗?计划已添加到每个查询的链接中。我看不到计划/执行/总时间。运行explain analyze时应位于底部。我确信查询和索引可以改进,但我倾向于建议您查看NoSql数据库以获取此类数据。Postgres也是一个事务数据库Mssql,它们可能不是最好的工具。
-- 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;