我有两个数据库,每个数据库都包含数据和“标签”(用表示)。在一个数据库中,标签与数据一起嵌入(存储在表中),而在另一个数据库中,标签存储在不同的表中,因此需要一个连接来访问标签。对于大多数查询,除了一个查询外,使用嵌入标签的变体速度更快。我想知道是否有人能告诉我为什么会这样,我自己对postgres,sql或数据库的细节不是很熟悉?我在下面列出了“解释-分析”输出。提前谢谢。
数据库由与这些位置点相关联的位置和用户组成,数据用\p字段标记。这两个数据库的区别在于,一个数据库中的标签与数据嵌入在同一个表中,另一个数据库中的标签存储在不同的表中,因此需要额外的联接。除此之外,两个查询做的事情完全相同。对于大多数查询,我们看到嵌入标签的方法速度更快,但是对于这个特定的查询,速度较慢,如果可能的话,我想知道原因。我不是真的在寻找一个详细的答案,我只是想知道是否有什么明显的原因导致一个查询计划比另一个慢。
外部标签查询:
SELECT firstname, lastname, latitude, longitude
FROM locations INNER
JOIN users ON locations.userid = users.id
JOIN users_p users0x ON users.id = users0x.users_id
JOIN locations_p locations0x ON locations.id = locations0x.locations_id
WHERE country = ? AND date_part('year', age(birthdate)) > 18
AND (date_part('year', to_date(timestamp, 'YYYY-MM-DD')) BETWEEN 2010 AND 2019)
AND (locations0x.longitude_p & '2') != 0 AND (users0x.lastname_p & '2') != 0 AND (users0x.firstname_p & '2') != 0 AND (users0x.birthdate_p & '2') != 0 AND (locations0x.timestamp_p & '2') != 0
AND (users0x.country_p & '2') != 0 AND (locations0x.latitude_p & '2') != 0
解释分析输出:
Nested Loop (cost=0.42..10116.71 rows=12 width=22) (actual time=317.811..331.306 rows=954 loops=1)
-> Nested Loop (cost=0.00..10051.48 rows=12 width=26) (actual time=317.793..328.701 rows=954 loops=1)
Join Filter: (locations.userid = users0x.users_id)
Rows Removed by Join Filter: 94446
-> Seq Scan on users_p users0x (cost=0.00..4.00 rows=96 width=4) (actual time=0.007..0.051 rows=100 loops=1)
Filter: (((country_p & 2) <> 0) AND ((birthdate_p & 2) <> 0) AND ((lastname_p & 2) <> 0) AND ((firstname_p & 2) <> 0))
-> Materialize (cost=0.00..10030.23 rows=12 width=34) (actual time=0.000..3.209 rows=954 loops=100)
-> Nested Loop (cost=0.00..10030.17 rows=12 width=34) (actual time=0.034..317.266 rows=954 loops=1)
Join Filter: (locations.userid = users.id)
Rows Removed by Join Filter: 98382
-> Seq Scan on users (cost=0.00..6.25 rows=1 width=18) (actual time=0.019..0.043 rows=2 loops=1)
Filter: ((country = 'Colombia'::text) AND (date_part('year'::text, age((('now'::cstring)::date)::timestamp with time zone, (to_date(birthdate, 'YYYY-MM-DD'::text))::timestamp with time zone)) > '18'::double precision))
Rows Removed by Filter: 98
-> Seq Scan on locations (cost=0.00..10008.30 rows=1250 width=16) (actual time=0.007..155.286 rows=49668 loops=2)
Filter: ((date_part('year'::text, (to_date("timestamp", 'YYYY-MM-DD'::text))::timestamp without time zone) >= '2010'::double precision) AND (date_part('year'::text, (to_date("timestamp", 'YYYY-MM-DD'::text))::timestamp without time zone) <= '2019'::double precision))
Rows Removed by Filter: 200332
-> Index Scan using locations_purpose_index on locations_p locations0x (cost=0.42..5.43 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=954)
Index Cond: (locations_id = locations.id)
Filter: (((latitude_p & 2) <> 0) AND ((longitude_p & 2) <> 0) AND ((timestamp_p & 2) <> 0))
Planning time: 0.555 ms
Execution time: 331.449 ms
嵌入式标签查询:
SELECT firstname, lastname, latitude, longitude
FROM locations INNER JOIN users ON locations.userid = users.id
WHERE country = ? AND date_part('year', age(birthdate)) > 18
AND (date_part('year', to_date(timestamp, 'YYYY-MM-DD')) BETWEEN 2010 AND 2019)
AND (users.firstname_p & '2') != 0
AND (locations.timestamp_p & '2') != 0
AND (users.country_p & '2') != 0 AND (locations.longitude_p & '2') != 0
AND (locations.latitude_p & '2') != 0
AND (users.lastname_p & '2') != 0 AND (users.birthdate_p & '2') != 0
解释分析输出:
Nested Loop (cost=0.00..13782.09 rows=12 width=22) (actual time=0.113..421.690 rows=954 loops=1)
Join Filter: (locations.userid = users.id)
Rows Removed by Join Filter: 98382
-> Seq Scan on users (cost=0.00..8.25 rows=1 width=18) (actual time=0.062..0.087 rows=2 loops=1)
Filter: ((country = 'Colombia'::text) AND ((country_p & 2) <> 0) AND ((birthdate_p & 2) <> 0) AND ((lastname_p & 2) <> 0) AND ((firstname_p & 2) <> 0) AND (date_part('year'::text, age((('now'::cstring)::date)::timestamp with time zone, (to_date(birthdate, 'YYYY-MM-DD'::text))::timestamp with time zone)) > '18'::double precision))
Rows Removed by Filter: 98
-> Seq Scan on locations (cost=0.00..13758.45 rows=1231 width=12) (actual time=0.018..207.065 rows=49668 loops=2)
Filter: (((latitude_p & 2) <> 0) AND ((longitude_p & 2) <> 0) AND ((timestamp_p & 2) <> 0) AND (date_part('year'::text, (to_date("timestamp", 'YYYY-MM-DD'::text))::timestamp without time zone) >= '2010'::double precision) AND (date_part('year'::text, (to_date("timestamp", 'YYYY-MM-DD'::text))::timestamp without time zone) <= '2019'::double precision))
Rows Removed by Filter: 200332
Planning time: 0.811 ms
Execution time: 421.820 ms
1条答案
按热度按时间mw3dktmi1#
第一个对吗?它似乎忽略了月份和日期。考虑取curdate,减去18年,然后与
birthdate
. 看到了吗https://en.wikipedia.org/wiki/sargable第二个根本不需要转换时间戳。相反,只需要与之兼容的日期范围文本。
这是怎么回事
& 2
? 闻起来像个表演杀手。然后考虑这些综合指数: