4

I am using oracle 12c and Sql Developer with json

For this example I have the follow JSON:

   {
        "id": "12",
        "name": "zhelon"
   }

So I have created the follow table for this:

create table persons
id number primary key,
person clob,
constraint person check(person is JSON);

The idea is persist in person column the previous JSON and use a the follow query to get that data

SELECT p.person FROM persons p WHERE json_textvalue('$name', 'zhelon')

Talking about perfonce, I am intresting to extract some json field and add new a colum to the table to improve the response time (I don't know if that is possible)

create table persons
id number primary key,
name varchar(2000),
person clob,
constraint person check(person is JSON);

To do this:

SELECT p.person FROM persons p WHERE p.name = 'zhelon';

My question is:

What's the best way to make a query to get data? I want to reduce the response time.

Which query get the data faster ?

SELECT p.person FROM persons p WHERE json_textvalue('$name', 'zhelon') 

or

SELECT p.person FROM persons p WHERE p.name = 'zhelon';

1 Answer 1

2

You can create a virtual column like this:

ALTER TABLE persons ADD (NAME VARCHAR2(100) 
   GENERATED ALWAYS AS (JSON_VALUE(person, '$name' returning VARCHAR2)) VIRTUAL);

I don't know the correct syntax of JSON_VALUE but I think you get an idea.

If needed you can also define a index on such columns like any other column.

However, when you run SELECT p.person FROM persons p WHERE p.name = 'zhelon'; I don't know which value takes precedence, p.person from JSON or the column.

Better use a different name in order to be on the safe side:

ALTER TABLE persons ADD (NAME_VAL VARCHAR2(100) 
   GENERATED ALWAYS AS (JSON_VALUE(person, '$name' returning VARCHAR2)) VIRTUAL);

SELECT p.person FROM persons p WHERE p.NAME_VAL= 'zhelon';
Sign up to request clarification or add additional context in comments.

3 Comments

If you create an index on column NAME the performance will be much better.
the index always improve the perfonamce, but for this case what happend if I index the clob collumn? @WernfriedDomscheit
Index on CLOB does not make any sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.