给定下表car_year
的汽车型号年份:
| id | maker | model | year |
----------------------------------
| 6 | Audi | Allroad | 2001 |
| 12 | Audi | A8 | 2008 |
| 14 | Ford | Mustang | 1996 |
| 15 | Honda | Civic | 2000 |
| 19 | Honda | Insight | 2000 |
| 22 | Ford | F150 | 2009 |
| 24 | Honda | Accord | 2000 |
| 28 | Ford | F150 | 2007 |
| 34 | Audi | S8 | 2002 |
| 48 | Ford | Expedition | 2011 |
| 62 | Ford | Escort | 2004 |
| 81 | Ford | Explorer | 2007 |
| 84 | Ford | Escape | 2006 |
| 93 | Honda | Accord | 1995 |
我想有一个“最早的制造商模型”的覆盖指数。我的解决方案是创建一个物化视图:
CREATE MATERIALIZED VIEW earliest AS
SELECT DISTINCT ON(maker) maker, model
FROM car_year
ORDER BY maker, year
然后是覆盖索引:
CREATE INDEX earliest_index ON earliest(maker) INCLUDE (model);
有用!但是物化视图是无用的(对我来说),因为我只会使用覆盖索引。我是否缺少更优雅的解决方案,或者我不知道的(Postgre)SQL 功能?