【发布时间】:2016-04-01 16:32:18
【问题描述】:
所以我有三个表: 产品、优惠和offer_lines
offer_lines 是一个连接表,用于建立一个拥有和属于多个关系:
- 产品 has_many offer_lines
- offers has_many offer_lines
offer_lines 有一个名为 calculated_price 的列。这是基于 offer 表的列 discount_percentage 和 products 表的列 price。
我想创建一个sql语句,可以根据offers表中存储的折扣百分比计算产品的折扣价格。
这是我目前得到的:
UPDATE offer_lines
SET discounted_price = (products.price - (products.price * offers.discount_percentage / 100))
FROM offer_lines AS o
INNER JOIN products ON o.product_id = products.id
INNER JOIN offers ON o.offer_id = offers.id
WHERE offer_lines.offer_id = 2;
这似乎工作正常,只是它需要大约一分钟才能运行。
这里是解释:
Update on offer_lines (cost=77.16..1131.37 rows=10670 width=87)
-> Hash Join (cost=77.16..1131.37 rows=10670 width=87)
Hash Cond: (o.product_id = products.id)
-> Hash Join (cost=9.44..836.90 rows=10670 width=77)
Hash Cond: (o.offer_id = offers.id)
-> Seq Scan on offer_lines o (cost=0.00..620.74 rows=26674 width=14)
-> Hash (cost=9.38..9.38 rows=4 width=71)
-> Nested Loop (cost=0.29..9.38 rows=4 width=71)
-> Index Scan using index_offer_lines_on_offer_id on offer_lines (cost=0.29..8.30 rows=1 width=53)
Index Cond: (offer_id = 13)
-> Seq Scan on offers (cost=0.00..1.04 rows=4 width=18)
-> Hash (cost=62.88..62.88 rows=388 width=18)
-> Seq Scan on products (cost=0.00..62.88 rows=388 width=18)
有什么想法可以让这个运行得更快吗?
【问题讨论】:
-
桌子上有索引吗?
-
是的,在 offer_lines offer_id 和 offer_lines product_id 上
标签: sql postgresql