2017-10-26 7 views
0

posgres에 배치 삽입을 최적화하고 싶습니다. 이렇게하려면 json 데이터의 최신 값을 찾아서 다른 json 데이터에서 사용해야합니다. 는 지금, 내 쿼리는 다음과 같습니다postgresql은 json을 삽입하기 위해 값으로 서브 쿼리를 사용합니다.

INSERT INTO audits ("audited_changes") 
VALUES 
    ('update', '{"step_id":[(SELECT ((audited_changes -> `step_id`)::json->>1) FROM audits WHERE auditable_id = 123 order by id desc limit 1)::int,3]}') 

하지만 포스트 그레스는 나를 JSON에서 하위 쿼리를 실행하지 않습니다. 어떻게하면됩니까?

+0

질문 [편집] (https://stackoverflow.com/posts/46360127/edit)에 몇 가지 샘플 데이터를 추가하고 (http://plaintexttools.github.io/plain-text-table/) 그 데이터에 근거한 예상 출력. [서식있는 텍스트] (http://stackoverflow.com/help/formatting)하시기 바랍니다. [스크린 샷 없음] (http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images -if-code-on-so-ask-a-question/285557 # 285557). – klin

답변

0

유일한 방법은 jsonjson_build_object()와 json_build_array() 함수를 사용하는 것이 었습니다

내 쿼리는 지금과 같이 보인다 :

INSERT INTO audits ("action", "audited_changes", "auditable_id", "auditable_type", "created_at", "version", "request_uuid") 
VALUES ('update', json_build_object('zone',json_build_array((SELECT ((audited_changes -> 'zone')::json->>1) FROM audits WHERE auditable_id = 1209103 order by id desc limit 1),'qwertyuiop')), 1209103, 'Followup', '2017-11-09 17:03:40 +0400', (SELECT max(version) FROM audits where auditable_id= 1209103)+1, 'cbd34343-1dc1-42b0-ade3-788dfb2c1172') 
0

난 당신이 시도 제안 : 내가 그것을 작동하게하는 것으로

INSERT INTO audits ("audited_changes") 
SELECT 'update' 
, '{"step_id":['||((audited_changes -> 'step_id')::json->>1)::int||',3]}' 
FROM audits 
WHERE auditable_id = 123 
order by id desc 
limit 1