SELECT * FROM table1
WHERE (col1, col2) IN (($1, $2), ($3, $4))
ORDER BY col3
LIMIT 10;
输出EXPLAIN ANALYZE
:
Limit (cost=59174.75..59174.77 rows=10 width=113) (actual time=3632.627..3632.661 rows=10 loops=1)
-> Sort (cost=59174.75..59180.22 rows=2188 width=113) (actual time=3632.623..3632.634 rows=10 loops=1)
Sort Key: col3
Sort Method: top-N heapsort Memory: 27kB
-> Nested Loop (cost=2.62..59127.46 rows=2188 width=113) (actual time=0.234..3561.309 rows=38347 loops=1)
...........
Total runtime: 3632.818 ms
但是当我删除订单时:
SELECT * FROM table1 WHERE (col1, col2) IN (($1, $2), ($3, $4)) LIMIT 10;
Limit (cost=2.62..272.85 rows=10 width=105) (actual time=0.258..1.143 rows=10 loops=1)
-> Nested Loop (cost=2.62..59127.46 rows=2188 width=105) (actual time=0.255..1.115 rows=10 loops=1)
........
Total runtime: 1.306 ms
- 有一个复合
btree index on (col1, col2)
和一个btree index on col3
。 - 写入性能和存储不是优先事项。读取性能至关重要,需要尽可能快。
- 这必须能够支持使用 IN 子句进行查询:
WHERE (col1, col2) IN (($1, $2), ($3, $4)) ORDER BY col3 LIMIT 10;
. (查找总是带有一个 IN 子句然后排序。)
注意:是否可以在 (col1, col2, col3) 上创建索引?那将使用(col1, col2)
并col3
已订购...