【发布时间】:2014-07-17 05:45:04
【问题描述】:
我有以下查询,大约需要 10 分钟。我需要这个更快。
有什么想法可以调整此查询吗? r_pos_transaction_head 表的记录略低于 500,000 条,r_pos_transaction_detl 的记录略低于 900,000 条。
我在我认为合适的地方创建了索引(您可以在计划中看到这些索引的使用情况)。
truncate table t_retail_history
insert into t_retail_history
select
h.source_db as legacy_source_db,
h.company as legacy_company,
h.store_code as legacy_store_code,
h.register as legacy_register,
cast(h.register as char(1)) + '/' + cast(h.transaction_no as varchar(10)) as legacy_transaction_no,
t_warehouse.store_number as store_number,
h.transaction_no as reference,
convert(varchar(10),dbo.datefromdays(h.date),103) as transaction_date,
convert(varchar(5),dateadd(second,h.time,cast(cast(getdate() as date) as datetime)), 108) as transaction_time,
d.product_code as legacy_product_code,
coalesce(d.colour_no,0) as legacy_colour_no,
coalesce(g_colour_name_replacement.new_colour_name,s.colour_name,'') as legacy_colour_name,
coalesce(d.size_no,0) as legacy_size_no,
coalesce(s.size_code,'') as legacy_size_code,
d.price_inc_tax as legacy_price_inc_tax,
d.sku_no as legacy_sku_no,
null as barcode,
d.quantity as qty,
d.nett_total as sales_total,
null as person_code,
t_warehouse.destination_busdiv_prefix
from
svi.r_pos_transaction_head h
inner join
svi.r_pos_transaction_detl d on
d.company = h.company
and d.store_code = h.store_code
and d.register = h.register
and d.tx_code = h.transaction_no
inner join
svi.g_skus s on
s.company = h.company
and s.product_code = d.product_code
and (
s.colour_position = d.colour_no
or s.colour_position is null and d.colour_no = 0
)
and (
s.size_position = d.size_no
or s.size_position is null and d.size_no = 0
)
left outer join
g_colour_name_replacement on
g_colour_name_replacement.product_code = d.product_code
and g_colour_name_replacement.old_colour_name = s.colour_name
left outer join
t_warehouse on
t_warehouse.legacy_svi_code = right('000' + cast(h.store_code as nvarchar(5)),3)
where
d.quantity <> 0
and d.nett_total <> 0
任何帮助表示赞赏!
【问题讨论】:
-
您是否尝试在 Databse Engine Tuning Advisor 中对其进行分析?
-
你能告诉我们
SELECT statement是否也只接受10minutes吗?因为如果您要插入大约 1 亿行,则很难对其进行优化:p -
@user1994514- 不,我没有。我以前没用过,我可以看看。是的,
SELECT需要同样的时间,所以我不认为插入本身是这里的问题。 -
用您需要的其他表中的列替换所有“整个表”连接怎么样?或者甚至可能创建包含此类信息的临时表并将它们加入主查询可能会有所帮助?
-
您忘记在该查询中包含 r_pos_transaction_head 表。
标签: sql sql-server sql-server-2012