我在表上使用select和insert查询。但是postgres的CPU使用率高达100%。我该如何优化呢
表结构和查询如下
CREATE TABLE ad_request
(
request_id bigserial NOT NULL,
client_id integer DEFAULT 0,
ad_type integer DEFAULT 0,
ad_unit integer DEFAULT 0,
host_url character varying(150) DEFAULT NULL::character varying,
ip_address character varying(20) DEFAULT NULL::character varying,
referer character varying(350) DEFAULT NULL::character varying,
platform character varying(20) DEFAULT NULL::character varying,
browser character varying(20) DEFAULT NULL::character varying,
served_campaign_id bigint DEFAULT 0,
status character(1) NOT NULL DEFAULT 'N'::bpchar,
created_time timestamp without time zone NOT NULL DEFAULT now(),
CONSTRAINT ad_request_pkey PRIMARY KEY (request_id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE ad_request
OWNER TO postgres;
SELECT served_campaign_id
FROM ad_request
WHERE client_id=1 AND ad_type=1 AND ad_unit=1 AND ip_address='192.168.1.1' AND created_time > current_timestamp - interval '24 HOUR';
INSERT INTO
ad_request(client_id,ad_type,ad_unit,host_url,ip_address,referer,platform,browser,served_campaign_id)
VALUES(?,?,?,?,?,?,?,?,?);
seelct查询的EXPLAIN ANALYZE输出为
Seq Scan on ad_request (cost=0.00..52629.15 rows=1 width=8) (actual time=245.512..245.512 rows=0 loops=1)
Filter: ((client_id = 1) AND (ad_type = 1) AND (ad_unit = 1) AND ((ip_address)::text = '192.168.1.1'::text) AND (created_time > (now() - '24:00:00'::interval)))
Total runtime: 245.544 ms
(3 rows)
和插入查询的输出是
Result (cost=0.00..0.02 rows=1 width=0) (actual time=0.092..0.094 rows=1 loops=1)
Total runtime: 0.198 ms
(2 rows)
和SELECT * FROM pg_stat_activity的输出pga内连接pg_locks pl on pga.procpid = pl.pid;是
datid | datname | procpid | usesysid | usename | current_query | waiting | xact_start
| query_start | backend_start | client_addr | client_port | locktype | database | relation | page | tuple | virtualxid | tran
sactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted
-------+---------+---------+----------+----------+----------------------- ------------------------------------------------------------+---------+--------- ---------------
-------+-------------------------------+-------------------------------+-------------+-------------+------------+----------+----------+------+-------+------------+-----
----------+---------+-------+----------+--------------------+------+-----------------+---------
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 20916 | 10969 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 0 | 1262 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 0 | 1260 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 0 | 2671 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 0 | 2676 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | virtualxid | | | | | 1/1272802 |
| | | | 1/1272802 | 1307 | ExclusiveLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 20916 | 11042 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 0 | 2672 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
20916 | adtag | 1307 | 10 | postgres | SELECT * FROM pg_stat_activity pga inner join pg_locks pl on pga.procpid=pl.pid ; | f | 2015-04-01 06:00:27.700
012+00 | 2015-04-01 06:00:27.700012+00 | 2015-04-01 04:04:49.810039+00 | | -1 | relation | 0 | 2677 | | | |
| | | | 1/1272802 | 1307 | AccessShareLock | t
(9 rows)