我有SQL Server 2016,我正在尝试为视图中的json列创建索引。
视图本身是在json上构建的,如下所示
SELECT
_id,
JSON_VALUE(trade, '$.direction') AS direction,
JSON_VALUE(trade, '$.entryprice') AS entryprice,
JSON_VALUE(trade, '$.exitprice') AS exitprice,
JSON_VALUE(trade, '$.potentialtarget') AS potentialtarget,
FORMAT(CAST(JSON_VALUE(trade, '$.entrytime') AS datetimeoffset), 'yyyy-MM-dd HH:mm:ss') AS entrytime,
FORMAT(CAST(JSON_VALUE(trade, '$.exittime') AS datetimeoffset), 'yyyy-MM-dd HH:mm:ss') AS exittime,
JSON_VALUE(trade, '$.maxfavourable') AS maxfavourable,
JSON_VALUE(trade, '$.maxagainst') AS maxagainst,
JSON_VALUE(trade, '$.signal') AS signal,
JSON_VALUE(trade, '$.instrument') AS instrument,
JSON_VALUE(trade, '$.account') AS account,
JSON_VALUE(trade, '$.quantity') AS quantity,
JSON_VALUE(trade, '$.gains') AS gains,
JSON_VALUE(trade, '$.hitedge') AS hitedge,
JSON_VALUE(trade, '$.RealizedProfitLoss') AS RealizedProfitLoss,
JSON_VALUE(trade, '$.CashValue') AS CashValue,
JSON_VALUE(trade, '$.BuyingPower') AS BuyingPower,
JSON_VALUE(trade, '$.maxagainst1ATR') AS maxagainst1ATR,
JSON_VALUE(trade, '$.maxagainst2ATR') AS maxagainst2ATR,
JSON_VALUE(trade, '$.Powerbars') AS powerbars,
JSON_VALUE(trade, '$.PowerbarsTime[0]') AS PowerbarsTime1,
JSON_VALUE(trade, '$.PowerbarsTime[1]') AS PowerbarsTime2,
JSON_VALUE(trade, '$.PowerbarsTime[2]') AS PowerbarsTime3,
JSON_VALUE(trade, '$.PowerbarsTime[3]') AS PowerbarsTime4,
JSON_VALUE(trade, '$.PowerbarsTime[4]') AS PowerbarsTime5,
JSON_VALUE(trade, '$.Powerbarsdetails') AS Powerbarsdetails,
JSON_VALUE(trade, '$.entrysignal') AS entrysignal,
JSON_VALUE(trade, '$.exitsignal') AS exitsignal
FROM
dbo.Trades
我正在尝试在_id和一些datetime列上构建索引。我收到一个错误:
为Index' NonClusteredIndex-20161124-152652'创建失败。 (Microsoft.SqlServer.Smo)
执行Transact-SQL语句或批处理时发生异常。 (Microsoft.SqlServer.ConnectionInfo)
无法在观看' dbo.vTrades'上创建索引。它没有唯一的聚簇索引。 (Microsoft SQL Server,错误:1940)
答案 0 :(得分:0)
只需在表/视图上创建一个唯一的聚簇索引。
CREATE UNIQUE CLUSTERED INDEX cu_indx_ID ON vTrades (id);
然后你应该能够创建你想要的非聚集索引。
<强>更新强>
请务必查看我的回答中的评论,以了解有价值的其他详细信息。