Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
PostgreSQL高CPU使用率_Postgresql - Fatal编程技术网

PostgreSQL高CPU使用率

PostgreSQL高CPU使用率,postgresql,Postgresql,我有一个如下模式的表: location=# \d locations Table "public.locations" Column | Type | Modifiers -----------+--------------------------+-------------------------------------------

我有一个如下模式的表:

location=# \d locations
                                   Table "public.locations"
  Column   |           Type           |                       Modifiers
-----------+--------------------------+--------------------------------------------------------
 id        | integer                  | not null default nextval('locations_id_seq'::regclass)
 phone     | text                     | not null
 longitude | text                     | not null
 latitude  | text                     | not null
 date      | text                     | not null
 createdAt | timestamp with time zone |
 updatedAt | timestamp with time zone |
Indexes:
    "locations_pkey" PRIMARY KEY, btree (id)
    "createdAt_idx" btree ("createdAt")
    "phone_idx" btree (phone)
它有14928439行:

location=# select count(*) from locations;
  count
----------
 14928439
我有一个http api用于通过电话查询用户最新上传的坐标,但它是用sql慢慢查询的:
select*from“locations”,其中“phone”=“15828354860”按“createdAt”desc limit 1排序

然后我解释:

location=# EXPLAIN ANALYZE select * from "locations" where "phone" = '15828354860' order by "createdAt" desc limit 1;
                                                                        QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit  (cost=0.43..5.22 rows=1 width=74) (actual time=4779.584..4779.584 rows=1 loops=1)
   ->  Index Scan Backward using "createdAt_idx" on locations  (cost=0.43..663339.70 rows=138739 width=74) (actual time=4779.583..4779.583 rows=1 loops=1)
         Filter: (phone = '15828354860'::text)
         Rows Removed by Filter: 2027962
 Planning time: 0.101 ms
 Execution time: 4779.612 ms
(6 rows)

it执行4.7s,如何提高查询速度?

上的索引
(“phone”,“createdAt”desc)
可能会有帮助。@Abelisto,它确实有帮助,并将时间缩短到0.046ms,谢谢!索引
“phone\u idx”btree(phone)
现在不需要了,因为可以使用新创建的来代替它。
(“phone”,“createdAt”desc)
上的索引可能会有所帮助。@Abelisto,它实际上很有帮助,并将时间减少到0.046ms,谢谢!现在不需要索引“phone_idx”btree(phone)
,因为可以使用新创建的索引来代替它。