2014-10-10 51 views
1

请让我来举个例子来解释一下:每次准备交换或多次连接的数据?

您需要向客户展示某些产品价格的实际信息。最终价格受到客户的好评,他的位置,运送产品的方式以及当前产品的供应商的影响。

产品数量超过100亿。除此之外,你应该以不同的货币显示价格。最后一个问题是,供应商每天都会在他们的产品上每天发送很多价格,由于在那里有很多职位,您可以在整天内申请(更新),而客户不希望等到更新完全他们想知道现在的价格(我知道这是不可能的,但我的意思是 - 客户不应该看到这样的消息:“对不起,你必须等到所有的价格更新”)。

我想要什么

我想将一个表ThePrice (productId, vendorId, usdPrice),其中每次使用一个客户使然

select .... from ThePrice p 
    inner join Vendors v ... 
    inner join VendorsPrices vp ... 
    inner join UserRatio ur ... 
    left join ShippingRatio ... 
    inner join Currencies ... 
    inner join PriceDependsOnLocation ... 

查询分为两个类似的表

FirstPrice (productId, vendorId, usdPrice, user1Ratio, user2Ratio, user3Ratio, shipppingRatio, euroPrice, localPrice)

and

SecondPrice(productId, vendorId, usdPrice, user1Ratio, user2Ratio, user3Ratio, shipppingRatio, euroPrice, localPrice)

userXRatio - 为客户privelege级别的所有可能的因素。在usdPrice上增加乘数。 shippingRatio - 定义客户是否需要运送。在usdPrice上增加乘数。 euroPrice,localPrice - 是以不同货币显示的价格与今日报价相关的价格。

这可能看起来像

if (@OneHourPassed = 1) 
    select ... from FirstPrice 
else 
    select ... from SecondPrice 

--not the best way but just to point out 

现在,这一切参数都在每次重新计算,因为价格可以在每一个查询更改,当将被更新以ThePrice表。这没有缓存。

这意味着我不时收到锁,因为当客户搜索产品时,他们也会在一整天内更新。想象一下 - 一分钟内可能有2000个来自客户的查询,供应商的一两个价格将被更新。 在同一张表中!

我想使用一个准备好的数据:虽然FirstPrice表用于读取,但SecondPrice表用于写入 - 更新价格。在某个时刻,他们互换,即在一个小时内。

而我只想知道 - 应用这个机制的机制是什么。使用IF-s?还是有另一种更优雅的方式?

+0

你的问题是非常广泛的,并没有提供很多关于你在这里做什么的细节。 – 2014-10-10 14:18:04

+0

我试图尽可能缩短,因为人们没有时间阅读大量文本。你建议添加什么细节更清晰和简单? – notricky 2014-10-10 15:52:25

回答

0

你的问题在太空中听起来太厉害了,但是从我能捕获的东西中,也许这样的东西可以帮助你。

create table FirstPrice (
    productId int, 
    vendorId int, 
    usdPrice money, 
    user1Ratio int, 
    user2Ratio int, 
    user3Ratio int, 
    shipppingRatio int, 
    euroPrice money, 
    localPrice money 
) 

create table SecondPrice (
    productId int, 
    vendorId int, 
    usdPrice money, 
    user1Ratio int, 
    user2Ratio int, 
    user3Ratio int, 
    shipppingRatio int, 
    euroPrice money, 
    localPrice money 
) 


CREATE TABLE LastExecution(execDate datetime default getdate()); 
insert into LastExecution(execDate) values (default); 
GO 

CREATE PROCEDURE SP_PRICE 
AS 
BEGIN 
    DECLARE @DATE DATETIME 
    SELECT @DATE=EXECDATE FROM LastExecution 

    IF (DATEDIFF(HOUR,@DATE,GETDATE())>0) 
    BEGIN 
     insert into LastExecution(execDate) values (default); 
     SELECT 'Second' as [table],* FROM SecondPrice; 
    END 
    ELSE 
    BEGIN 
     SELECT 'First' as [table],* FROM FirstPrice; 
    END 
END; 
+0

是的,这就是我想的,但我也需要它来做其余的计算。我的意思是我需要像视图这样的东西,在那里我可以绝对确定,直到X时间只有两张桌子中的一张将被使用(在一个计划中)。 IF-s对于程序是可以的(还应该检查计划)。 (我很抱歉 - 我以前的版本是关于这个问题的一部分)。有关此新信息的任何建议?不管怎样,谢谢! – notricky 2014-10-10 18:12:32

+0

Sorrry,这次我真的不理解你的问题。你能再解释一遍吗? – 2014-10-10 19:26:17

+0

我在说,这个解决方案很明确,但是我有一些更复杂的情况 - 有很多程序,使用这种查询的地方。我需要像view或synonims之类的东西。说真话 - 最后一个(synonim) - 我不知道从哪里开始。 – notricky 2014-10-11 21:04:59

0

我想这一个descision是两种方式:

1)使用Partitions,但与购买企业版,我还是不明白,此刻的交换表

2)使用View,这是可能的Alter,但你必须做一个控制表,在那里你应该持有一个切换表的标志,并在查询中有这个脚本无处不在:

while exists(select 1 from ControlTable where SwitchNow = 1) 
begin 
    waitfor delay '00:00:00.005' 
end 
-- this is for client-queries, who should release the View from being Altered 

或其他从一旁的方式

create table Switch(Part bit not null); 
insert Switch values(0) 
go 
create view PriceView 
with view_metadata 
as 
select * from Price1 where 1 = (select Part from Switch) 
union all 
select * from Price2 where 0 = (select Part from Switch) 

建议,但我认为,这不是一个SQL的策划者是一个好主意还是我把它错了吗?因为如果我错了,那么这是最好的方法。