У меня проблема: если я пишу в таблицу (с использованием Linq-to-SQL), которая является зависимостью представления, а затем сразу же поворачивается и запрашивает это представление, чтобы проверить влияние записи (используя новое соединение с БД и, следовательно, новый контекст данных), воздействие записи не появляется сразу, а занимает до нескольких секунд. Иногда это случается (возможно, 10-20
раз за 10,000
или так пишет).
Это определение вида:
CREATE VIEW [Position].[Transactions]
WITH SCHEMABINDING
AS
(
SELECT
Account,
Book,
TimeAPIClient AS DateTimeUtc,
BaseCcy AS Currency,
ISNULL(QuantityBase, 0) AS Quantity,
ValueDate AS SettleDate,
ISNULL(CAST(0 AS tinyint), 0) AS TransactionType
FROM Trades.FxSpotMF
WHERE IsCancelled = 0
UNION ALL
SELECT
Account,
Book,
TimeAPIClient AS DateTimeUtc,
QuoteCcy AS Currency,
ISNULL(-QuantityBase * Rate, 0) AS Quantity,
ValueDate AS SettleDate,
ISNULL(CAST(0 AS tinyint), 0) AS TransactionType
FROM Trades.FxSpotMF
WHERE IsCancelled = 0
UNION ALL
SELECT
Account,
Book,
ExecutionTimeUtc AS DateTimeUtc,
BaseCcy AS Currency,
ISNULL(QuantityBase, 0) AS Quantity,
ValueDate AS SettleDate,
ISNULL(CAST(1 AS tinyint), 1) AS TransactionType
FROM Trades.FxSpotManual
WHERE IsCancelled = 0
UNION ALL
SELECT
Account,
Book,
ExecutionTimeUtc AS DateTimeUtc,
QuoteCcy AS Currency,
ISNULL(-QuantityBase * Rate, 0) AS Quantity,
ValueDate AS SettleDate,
ISNULL(CAST(1 AS tinyint), 1) AS TransactionType
FROM Trades.FxSpotManual
WHERE IsCancelled = 0
UNION ALL
SELECT
Account,
Book,
ExecutionTimeUtc AS DateTimeUtc,
BaseCcy AS Currency,
ISNULL(SpotQuantityBase, 0) AS Quantity,
SpotValueDate AS SettleDate,
ISNULL(CAST(2 AS tinyint), 2) AS TransactionType
FROM Trades.FxSwap
UNION ALL
SELECT
Account,
Book,
ExecutionTimeUtc AS DateTimeUtc,
QuoteCcy AS Currency,
ISNULL(-SpotQuantityBase * SpotRate, 0) AS Quantity,
SpotValueDate AS SettleDate,
ISNULL(CAST(2 AS tinyint), 2) AS TransactionType
FROM Trades.FxSwap
UNION ALL
SELECT
Account,
Book,
ExecutionTimeUtc AS DateTimeUtc,
BaseCcy AS Currency,
ISNULL(ForwardQuantityBase, 0) AS Quantity,
ForwardValueDate AS SettleDate,
ISNULL(CAST(2 AS tinyint), 2) AS TransactionType
FROM Trades.FxSwap
UNION ALL
SELECT
Account,
Book,
ExecutionTimeUtc AS DateTimeUtc,
QuoteCcy AS Currency,
ISNULL(-ForwardQuantityBase * ForwardRate, 0) AS Quantity,
ForwardValueDate AS SettleDate,
ISNULL(CAST(2 AS tinyint), 2) AS TransactionType
FROM Trades.FxSwap
UNION ALL
SELECT
Account,
c.Book,
TimeUtc AS DateTimeUtc,
Currency,
ISNULL(Amount, 0) AS Quantity,
SettleDate,
ISNULL(CAST(3 AS tinyint), 3) AS TransactionType
FROM Trades.Commission c
JOIN Trades.Payment p
ON c.UniquePaymentId = p.UniquePaymentId
AND c.Book = p.Book
)
в то время как это запрос, созданный Linq-to-SQL для записи в одну из следующих таблиц:
INSERT INTO [Trades].[FxSpotMF] ([UniqueTradeId], [BaseCcy], [QuoteCcy], [ValueDate], [Rate], [QuantityBase], [Account], [Book], [CounterpartyId], [Counterparty], [ExTradeId], [TimeAPIClient], [TimeAPIServer], [TimeExchange], [TimeHandler], [UniqueOrderId], [IsCancelled], [ClientId], [SequenceId], [ExOrdId], [TradeDate], [OrderCycleId], [CycleIndex])
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, @p14, @p15, @p16, @p17, @p18, @p19, @p20, @p21, @p22)
и это запрос, созданный Linq-to-SQL для проверки эффекта записи:
SELECT
SUM([t0].[Quantity]) AS [Item2],
[t0].[Currency] AS [Item1]
FROM [Position].[Transactions] AS [t0]
WHERE ([t0].[Book] = @p0)
AND ([t0].[DateTimeUtc] < @p1)
GROUP BY [t0].[Currency]
Кроме того, это код Linq-to-SQL, который генерирует запись (используя поставщиков типов F #):
type Schema = Microsoft.FSharp.Data.TypeProviders.DbmlFile<"TradeDb.dbml", ContextTypeName="TradeDb">
use db = new Schema.TradeDb(connectionString)
let trade = new Schema.Trades_FxSpotMF()
(* omitted: set object properties corresponding to column values here... *)
db.Trades_FxSpotMF.InsertOnSubmit(trade)
db.SubmitChanges()
тогда как это соответствует Linq-to-SQL, который генерирует чтение:
use db = new Schema.TradeDb(connectionString)
query { for t in db.Position_Transactions do
where ( t.Book = book &&
t.DateTimeUtc < df.MaxExecutionTimeExcl
)
groupBy t.Currency into group
let total = query { for x in group do sumBy x.Quantity }
select (group.Key, total)
}
|> Map.ofSeq
Я бы подумал, что System.Data.Linq.DataContext.SubmitChanges()
вернется только после завершения транзакции записи и что любой последующий запрос представления должен содержать эффект записи... что я теряю/делаю неправильно?