簡體   English   中英

簡單查詢性能不佳

[英]Poor performance on simple query

我在函數中有一個查詢來選擇頂部行,並為最后一行選擇另一個查詢,每個查詢大約需要300毫秒才能執行,並且此查詢執行了很多次,使該函數無用

這是查詢(這是一個測試,在函數參數中進行更改):

SELECT the_geom
FROM "Entries" 
WHERE taxiid= 366 and timestamp between '2008-02-06 16:00:00' and timestamp '2008-02-06 16:00:00' + interval '5 minutes' 
ORDER BY entryid DESC 
LIMIT 1;;

這是查詢的EXPLAIN ANALYZE輸出:

QUERY PLAN                                                           

---------------------------------------------------------------------     -------------------------------------------------------------------------------------------------------
Seq Scan on "Entries"  (cost=0.00..63538.80 rows=70 width=51) (actual time=184.409..342.049 rows=56 loops=1)
Filter: (("timestamp" >= '2008-02-06 16:00:00'::timestamp without time zone) AND ("timestamp" <= '2008-02-06 16:05:00'::timestamp without time zone
) AND (taxiid = 366))
Rows Removed by Filter: 2128847
Planning time: 0.191 ms
Execution time: 342.088 ms
(5 rows)

有沒有更好的方法來獲得第一和最后一行?

編輯:感謝Drunix,這確實有所幫助,但是,我無法理解的事情正在發生,隨着索引的增加,我能夠從300毫秒變為0.2毫秒

但是如果我將添加到時間戳的時間間隔更改為120分鍾,則不使用該索引,它會持續花費300毫秒

這是證明(5分鍾間隔):

snowflake=# explain analyze Select the_geom from "Entries" 
where taxiid= 366 and "timestamp" between '2008-02-06 16:00:00' and "timestamp" '2008-02-06 16:00:00' + interval '5 minutes'
ORDER BY entryid ASC 
LIMIT 1;

QUERY PLAN                                                   

-------------------------------------------------------------------------
Limit  (cost=149.52..149.52 rows=1 width=55) (actual time=0.129..0.129 rows=1 loops=1)
->  Sort  (cost=149.52..149.70 rows=73 width=55) (actual time=0.127..0.127 rows=1 loops=1)
     Sort Key: entryid
     Sort Method: top-N heapsort  Memory: 25kB
     ->  Index Scan using entriesindex on "Entries"  (cost=0.43..149.15 rows=73 width=55) (actual time=0.045..0.090 rows=56 loops=1)
           Index Cond: ((taxiid = 366) AND ("timestamp" >= '2008-02-06 16:00:00'::timestamp without time zone) AND ("timestamp" <= '2008-02-06 16:
05:00'::timestamp without time zone))
Planning time: 0.266 ms
Execution time: 0.180 ms
(8 rows)

另一個(間隔120分鍾):

snowflake=# explain analyze Select the_geom from "Entries" 
where taxiid= 366 and "timestamp" between '2008-02-06 16:00:00' and "timestamp" '2008-02-06 16:00:00' + interval '120 minutes' 
ORDER BY entryid ASC 
LIMIT 1;

QUERY PLAN                                                        

-------------------------------------------------------------------------
Limit  (cost=0.43..60.02 rows=1 width=55) (actual time=245.570..245.570 rows=1 loops=1)
->  Index Scan using "Entries_pkey" on "Entries"  (cost=0.43..97542.75 rows=1637 width=55) (actual time=245.568..245.568 rows=1 loops=1)
     Filter: (("timestamp" >= '2008-02-06 16:00:00'::timestamp without time zone) AND ("timestamp" <= '2008-02-06 18:00:00'::timestamp without tim
e zone) AND (taxiid = 366))
     Rows Removed by Filter: 853963
Planning time: 0.277 ms
Execution time: 245.616 ms

好的,將我的評論改為答案:

除非已經擁有它,否則應該創建一個復合索引:

create index somename on Entries(taxiid, timestamp);

根據您的執行計划,這些字段的組合應相當有選擇性,因此索引掃描應更有效。 請注意, (timestamp, taxiid)上的索引可能用處不大,因為它僅用於按時間戳限制行。 在類似情況下,將檢查相等性的列放在前面。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM