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.
json sql-server
add a comment |
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.
json sql-server
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 indexingCustomer
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
add a comment |
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.
json sql-server
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
json sql-server
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 indexingCustomer
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
add a comment |
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 indexingCustomer
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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