Need an efficient way to store / query json in SQL database

I am implementing a service in which each user must have their own json / document database. In addition to the fact that the user can request json documents using an example, the database must also support ACID transactions involving several documents, so I canceled the use of Couch / Mongo or other NoSQL databases (cannot use RavenDB, since it should work in Unix- systems).

With that in mind, I'm trying to figure out a way to implement this in addition to the SQL database. Here is what I have come up with so far:

CREATE TABLE documents (
  id INTEGER PRIMARY KEY,
  doc TEXT
);

CREATE TABLE indexes (
  id INTEGER PRIMARY KEY,
  property TEXT,
  value TEXT,
  document_id INTEGER
)

Each user will have a database with these two tables, and the user will have to indicate which fields he needs to request so that the system can correctly fill in the Indexes table. Therefore, if user "A" sets up his account to include queries by "name" and "age", each time the user inserts a document with the property "name" or "age", the system also inserts the record into "indexes", table, where the column โ€œpropertyโ€ will contain the name / age, โ€œvalueโ€ will contain the value of the property, and โ€œdocument_idโ€ indicates the corresponding document.

For example, let's say a user inserts the following document:

'{"name" : "Foo", "age" 43}'

"" "":

INSERT INTO documents (id,doc) VALUES (1, '{"name" : "Foo", "age" 43}');
INSERT INTO indexes (property, value, document_id) VALUES ('name', 'foo', 1);
INSERT INTO indexes (property, value, document_id) VALUES ('age', '43', 1);

, 'A' :

'{"name": "Foo", "age": 43}' //(the queries are also json documents).

SQL:

SELECT doc FROM documents
WHERE id IN (SELECT document_id FROM indexes
             WHERE document_id IN (SELECT document_id FROM indexes
                                   WHERE property = 'name' AND value = 'Foo')
             AND property = 'age' AND value = '43') 

:

  • , (, 20-30 ), , , SELECT (postgres, mysql...)?
  • , / json-?
  • ?
  • , ACID Unix?
+5
1

indexes - , Entity-Attribute-Value.

EAV , . ( indexes, document_id.)

: Entity. , . (, name=foo), .

, :
1. , , n n. n.
2. , / .

, EAV, .


, , SQL . - , . :
- (fieldX, fieldY, fieldZ) , .
- , fieldZ.


, - , .

( properties, ) / , , , EAV. , "" .

. EAV, ?

  SELECT
    document_id
  FROM
    indexes
  WHERE
       (property = 'name' AND value = 'Foo')
    OR (property = 'age'  AND value = '43' )
  GROUP BY
    document_id
  HAVING
    COUNT(*) = 2

, (document_id, property, value) . ('name', 'foo') , COUNT(*).

+5
source

All Articles