Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store NoSQL data on SQL Server?

So ignore the fact that we should just use a NoSQL DB - client infrastructure requirements are getting the way.

We have data that obviously belongs in a non-relational model, but we have to use SQL Server 2014 for persistence. Is there a way to use the library for something like RavenDB or MongoDB with SQL Server for persistence? For instance, storing JSON or BSON in a SQL Server table but using Mongo or Raven to query and serialize it?

We were initially about to just store JSON data in a column, but I figured there had to be a more elegant solution. I saw that RavenDB supports SQL Server replication, but it doesn't look like it can be used for its primary persistence component.

We are running a C# ASP.NET MVC web app. The front end is a KnockoutJS SPA, so it would be happy to bind to JSON data.

like image 859
jrizzo Avatar asked Jul 17 '15 19:07

jrizzo


People also ask

Does SQL Server support NoSQL?

SQL Server and Azure SQL Database have native JSON functions that enable you to parse JSON documents using standard SQL language. You can store JSON documents in SQL Server or SQL Database and query JSON data as in a NoSQL database.

How is NoSQL data stored?

NoSQL databases store data in documents rather than relational tables. Accordingly, we classify them as “not only SQL” and subdivide them by a variety of flexible data models. Types of NoSQL databases include pure document databases, key-value stores, wide-column databases, and graph databases.

Why NoSQL is not good for transactions?

Unlike relational databases, which are built on sound mathematical and theoretical model [2], NoSQL databases do not have standard or widely accepted model. Despite the differences in the design of different NoSQL databases, they all have simple data model and do not support transactions.

Can you use SQL to query NoSQL database?

You can use standard SELECT statements to find data in the database. Or, you can use advanced SQL statements with JSON attributes to search Embedded Documents. Standard SQL statements allow your relational database application to easily migrate to a NoSQL database like NosDB.


2 Answers

For a much more sophisticated discussion of storing JSON in SQL Server as relational data and extracting it back as JSON please see this wonderful article by Phil Factor (thats the name he goes by), Producing JSON Documents from SQL Server queries via TSQL,https://www.simple-talk.com/sql/t-sql-programming/producing-json-documents-from-sql-server-queries-via-tsql/ .

Be careful about storing JSON as varchar with full text indexes or as xml type (which is not the same as JSON) with xml indexes. There can be severe performance issues when doing inserts on even a million row table so test carefully with realistic, for you, numbers of rows to see if an XML or varchar solution works for you.

If all you are going to do is stuff JSON data into varchars and back again then you should have no problems. Until the latest version of Mongo (3.04 or so) Mongo was not transactional based and one client of mine was always losing data and that caused a world of finger pointing. If the version of Mongo that you are using is not ACID compliant be very, very careful.

I am amending this answer because SQL Server 2016 now supports JSON in a big way. According to Microsoft it was one of the most requested features. Please see the following two articles:

  • https://msdn.microsoft.com/en-us/library/dn921897.aspx
  • https://blogs.msdn.microsoft.com/jocapc/2015/05/16/json-support-in-sql-server-2016/
like image 152
benjamin moskovits Avatar answered Sep 27 '22 22:09

benjamin moskovits


I don't know if this answers your question but what I've done in situations like this is to hybridize my SQL database. I store the JSON in something like an nvarchar(max) and add columns to the table that I will search on. So something like

  • my JSON document

    {Name: "name", CreatedBy: "name", Date: "date", other attributes... }
    
  • Table structure. Add columns that will be searched on as well as the entire JSON object

    NAME | DATE | CREATED_BY | JSON
    

It's not a pretty approach but it's worked so far.

like image 34
ThrowsException Avatar answered Sep 27 '22 21:09

ThrowsException