【发布时间】:2020-04-23 14:05:31
【问题描述】:
我正在处理一个复杂的规范化数据库,并生成一个动态产品。
Current 查询运行比较快,但生成了 1K+ 行,我需要实现分页解决方案。
在查询中获取构建动态数据所需的所有信息相对复杂
FROM pulse_templates pt
INNER JOIN taggings ptags on ptags.taggable_type = 'PulseTemplate' AND ptags.taggable_id =
pt.id AND ptags.context = 'available_for'
INNER JOIN schools s on s.id = 1
LEFT JOIN mascots m on m.id = s.mascot_id
LEFT JOIN pulse_designs pd on pd.id = m.silhouette_id
INNER JOIN colors_pulse_templates cpt ON cpt."pulse_template_id" = pt."id"
INNER JOIN items i on (pt.template_type = 'static' AND cpt."color_id" = i."primary_color_id") OR (pt.template_type = 'primary' AND s.primary_color_id = i.primary_color_id AND i.primary_color_id = cpt.color_id) or (pt.template_type = 'standard' AND i.primary_color_id = cpt.color_id AND (i.primary_color_id = s.primary_color_id OR i.primary_color_id = s.secondary_color_id OR i.primary_color_id = s.tertiary_color_id))
INNER JOIN taggings itags on itags.taggable_type = 'Item' AND itags.taggable_id = i.id AND itags.tag_id = ptags.tag_id AND ptags.context = 'available_for'
INNER JOIN tags tag on tag.id = itags.tag_id
INNER JOIN categories c on (c.id = i.category_id)
INNER JOIN pulse_products pp on pp.id = i.pulse_product_id
INNER JOIN product_lines pl on pl.id = i.product_line_id
WHERE
s.id = 1 AND ((pt.all_identifiers LIKE '%mascot_name%' AND pd.id IS NOT NULL) OR pt.all_identifiers NOT LIKE '%mascot_name%')
GROUP BY s.id,pp.id,i.id,pt.id,pl.price
性能还可以
Total query runtime: 1 secs 810 msec.
1365 rows affected.
但是当我尝试使用 LIMIT/OFFSET 实现分页时,它会消失。
Total query runtime: 10 secs 737 msec.
25 rows affected.
由于这种性能损失,我几乎可以将分页移出数据库并移入服务器代码。
有没有一种很好的方法来为一个没有这么多额外处理时间的公共网站实施分页?
【问题讨论】:
-
"最好将分页移出数据库,移入服务器代码。"确实是的。这是一个很好的地方。
-
如果您需要帮助以使其在数据库中更快,请在快速和慢速版本中显示
EXPLAIN (ANALYZE, BUFFERS)。
标签: postgresql performance pagination