SQL Server - Indexed View with FOR JSON









up vote
0
down vote

favorite












I'm testing different ways that I can implement CQRS. One of my test uses FOR JSON queries to bypass any type of ORM, much like described here. Another implementation I wanted to test was, instead of creating the JSON data on-the-fly, to create an key-value type indexed view where the key is some identifier and the data is the JSON data created from the 'actual' data. For data stores that are updated sparingly but read frequently, this would seem like a smart thing to do. The problem is how to create the indexed view, something like the following.



CREATE TABLE [dbo].[JsonTest]
(
[Number] [NVARCHAR](50) NULL,
[Date] [DATETIME] NULL,
[Customer] [NVARCHAR](50) NULL,
[Price] [MONEY] NULL,
[Quantity] [INT] NULL
) ON [PRIMARY]

CREATE VIEW dbo.CustomerOrders
WITH SCHEMABINDING
AS
(
SELECT
customer,
(SELECT
Customer AS AccountNumber,
Number AS [Order.Number],
Date AS [Order.Date],
Price AS 'Item.UnitPrice',
Quantity AS 'Item.Qty'
FROM
dbo.JsonTest
WHERE
Customer = j.Customer
FOR JSON PATH, ROOT('Orders')) AS data
FROM
dbo.JsonTest j);


The problem is you can't create an indexed view from a view with subqueries. When using FOR JSON the entire query result is converted to JSON. What I need is the ability to convert the data in some columns into JSON.










share|improve this question























  • Not possible unless you start mucking about with triggers, and I don't recommend that. This is too difficult for the engine to do automatically; if such a thing were permitted it would effectively be required to redo the entire query if a single row was changed in any way. Consider just indexing Customer if it's not indexed already and see if that's already enough; the bottleneck will be reading the row data, not producing the JSON.
    – Jeroen Mostert
    yesterday











  • @Jeroen That's what I was afraid of. I was hoping an indexed view would give me a shortcut to a separate read store that gets automatically updated behind the scenes. A trigger would achieve the same thing but implementationwould be a bit more involved.
    – Rubio
    yesterday














up vote
0
down vote

favorite












I'm testing different ways that I can implement CQRS. One of my test uses FOR JSON queries to bypass any type of ORM, much like described here. Another implementation I wanted to test was, instead of creating the JSON data on-the-fly, to create an key-value type indexed view where the key is some identifier and the data is the JSON data created from the 'actual' data. For data stores that are updated sparingly but read frequently, this would seem like a smart thing to do. The problem is how to create the indexed view, something like the following.



CREATE TABLE [dbo].[JsonTest]
(
[Number] [NVARCHAR](50) NULL,
[Date] [DATETIME] NULL,
[Customer] [NVARCHAR](50) NULL,
[Price] [MONEY] NULL,
[Quantity] [INT] NULL
) ON [PRIMARY]

CREATE VIEW dbo.CustomerOrders
WITH SCHEMABINDING
AS
(
SELECT
customer,
(SELECT
Customer AS AccountNumber,
Number AS [Order.Number],
Date AS [Order.Date],
Price AS 'Item.UnitPrice',
Quantity AS 'Item.Qty'
FROM
dbo.JsonTest
WHERE
Customer = j.Customer
FOR JSON PATH, ROOT('Orders')) AS data
FROM
dbo.JsonTest j);


The problem is you can't create an indexed view from a view with subqueries. When using FOR JSON the entire query result is converted to JSON. What I need is the ability to convert the data in some columns into JSON.










share|improve this question























  • Not possible unless you start mucking about with triggers, and I don't recommend that. This is too difficult for the engine to do automatically; if such a thing were permitted it would effectively be required to redo the entire query if a single row was changed in any way. Consider just indexing Customer if it's not indexed already and see if that's already enough; the bottleneck will be reading the row data, not producing the JSON.
    – Jeroen Mostert
    yesterday











  • @Jeroen That's what I was afraid of. I was hoping an indexed view would give me a shortcut to a separate read store that gets automatically updated behind the scenes. A trigger would achieve the same thing but implementationwould be a bit more involved.
    – Rubio
    yesterday












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm testing different ways that I can implement CQRS. One of my test uses FOR JSON queries to bypass any type of ORM, much like described here. Another implementation I wanted to test was, instead of creating the JSON data on-the-fly, to create an key-value type indexed view where the key is some identifier and the data is the JSON data created from the 'actual' data. For data stores that are updated sparingly but read frequently, this would seem like a smart thing to do. The problem is how to create the indexed view, something like the following.



CREATE TABLE [dbo].[JsonTest]
(
[Number] [NVARCHAR](50) NULL,
[Date] [DATETIME] NULL,
[Customer] [NVARCHAR](50) NULL,
[Price] [MONEY] NULL,
[Quantity] [INT] NULL
) ON [PRIMARY]

CREATE VIEW dbo.CustomerOrders
WITH SCHEMABINDING
AS
(
SELECT
customer,
(SELECT
Customer AS AccountNumber,
Number AS [Order.Number],
Date AS [Order.Date],
Price AS 'Item.UnitPrice',
Quantity AS 'Item.Qty'
FROM
dbo.JsonTest
WHERE
Customer = j.Customer
FOR JSON PATH, ROOT('Orders')) AS data
FROM
dbo.JsonTest j);


The problem is you can't create an indexed view from a view with subqueries. When using FOR JSON the entire query result is converted to JSON. What I need is the ability to convert the data in some columns into JSON.










share|improve this question















I'm testing different ways that I can implement CQRS. One of my test uses FOR JSON queries to bypass any type of ORM, much like described here. Another implementation I wanted to test was, instead of creating the JSON data on-the-fly, to create an key-value type indexed view where the key is some identifier and the data is the JSON data created from the 'actual' data. For data stores that are updated sparingly but read frequently, this would seem like a smart thing to do. The problem is how to create the indexed view, something like the following.



CREATE TABLE [dbo].[JsonTest]
(
[Number] [NVARCHAR](50) NULL,
[Date] [DATETIME] NULL,
[Customer] [NVARCHAR](50) NULL,
[Price] [MONEY] NULL,
[Quantity] [INT] NULL
) ON [PRIMARY]

CREATE VIEW dbo.CustomerOrders
WITH SCHEMABINDING
AS
(
SELECT
customer,
(SELECT
Customer AS AccountNumber,
Number AS [Order.Number],
Date AS [Order.Date],
Price AS 'Item.UnitPrice',
Quantity AS 'Item.Qty'
FROM
dbo.JsonTest
WHERE
Customer = j.Customer
FOR JSON PATH, ROOT('Orders')) AS data
FROM
dbo.JsonTest j);


The problem is you can't create an indexed view from a view with subqueries. When using FOR JSON the entire query result is converted to JSON. What I need is the ability to convert the data in some columns into JSON.







json sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday









marc_s

564k12510871240




564k12510871240










asked yesterday









Rubio

5521620




5521620











  • Not possible unless you start mucking about with triggers, and I don't recommend that. This is too difficult for the engine to do automatically; if such a thing were permitted it would effectively be required to redo the entire query if a single row was changed in any way. Consider just indexing Customer if it's not indexed already and see if that's already enough; the bottleneck will be reading the row data, not producing the JSON.
    – Jeroen Mostert
    yesterday











  • @Jeroen That's what I was afraid of. I was hoping an indexed view would give me a shortcut to a separate read store that gets automatically updated behind the scenes. A trigger would achieve the same thing but implementationwould be a bit more involved.
    – Rubio
    yesterday
















  • Not possible unless you start mucking about with triggers, and I don't recommend that. This is too difficult for the engine to do automatically; if such a thing were permitted it would effectively be required to redo the entire query if a single row was changed in any way. Consider just indexing Customer if it's not indexed already and see if that's already enough; the bottleneck will be reading the row data, not producing the JSON.
    – Jeroen Mostert
    yesterday











  • @Jeroen That's what I was afraid of. I was hoping an indexed view would give me a shortcut to a separate read store that gets automatically updated behind the scenes. A trigger would achieve the same thing but implementationwould be a bit more involved.
    – Rubio
    yesterday















Not possible unless you start mucking about with triggers, and I don't recommend that. This is too difficult for the engine to do automatically; if such a thing were permitted it would effectively be required to redo the entire query if a single row was changed in any way. Consider just indexing Customer if it's not indexed already and see if that's already enough; the bottleneck will be reading the row data, not producing the JSON.
– Jeroen Mostert
yesterday





Not possible unless you start mucking about with triggers, and I don't recommend that. This is too difficult for the engine to do automatically; if such a thing were permitted it would effectively be required to redo the entire query if a single row was changed in any way. Consider just indexing Customer if it's not indexed already and see if that's already enough; the bottleneck will be reading the row data, not producing the JSON.
– Jeroen Mostert
yesterday













@Jeroen That's what I was afraid of. I was hoping an indexed view would give me a shortcut to a separate read store that gets automatically updated behind the scenes. A trigger would achieve the same thing but implementationwould be a bit more involved.
– Rubio
yesterday




@Jeroen That's what I was afraid of. I was hoping an indexed view would give me a shortcut to a separate read store that gets automatically updated behind the scenes. A trigger would achieve the same thing but implementationwould be a bit more involved.
– Rubio
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222191%2fsql-server-indexed-view-with-for-json%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53222191%2fsql-server-indexed-view-with-for-json%23new-answer', 'question_page');

);

Post as a guest














































































這個網誌中的熱門文章

Is there any way to eliminate the singular point to solve this integral by hand or by approximations?

Why am i infinitely getting the same tweet with the Twitter Search API?

Solve: $(3xy-2ay^2)dx+(x^2-2axy)dy=0$