PostgreSQL高CPU使用率

时间:2017-07-23 11:29:52

标签: postgresql

我有一个表格如下:

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" where "phone" = '15828354860' order by "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)

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

0 个答案:

没有答案