0

query:

$msg = XYZ::where(function ($q) use ($chat) {
                    $q->where('msg->x', $chat['xId'])->where('msg->y', $chat['yId']);
                })->orWhere(function ($q) use ($chat) {
                    $q->where('msg->x', $chat['yId'])->where('msg->y', $chat['xId']);
                })->orderBy('msg->sTime')->first();

Index:

CREATE INDEX msg ON XYZ USING GIN ("msg" jsonb_path_ops);
CREATE INDEX msg_x ON XYZ USING gin (("msg" -> 'x'));
CREATE INDEX msg_y ON XYZ USING gin (("msg" -> 'y'));
CREATE INDEX msg_stime ON XYZ USING gin (("msg" -> 'sTime'));

i created index on jsonb field "msg" and i am trying to use @> notation in query in laravel. query in question works perfectly but i just want to reduce execution time duration by creating index.

1

1 Answer 1

0

You can go for following approach for example

$users = User::whereRaw("jsonb_exists(json -> 'skills', 'drink beer')")->get();

Since @> just checks if json contains, this will have similar effect.

There is this link https://laravel.com/api/5.6/Illuminate/Database/Query/Builder.html#method_whereJsonContains and with this, this is possible

User::WhereJsonContains('json->skills', 'drink beer')->get();

But i have not tried it yet.

Hope this helps you.

Sign up to request clarification or add additional context in comments.

2 Comments

query in question works perfectly but i just want to reduce execution time duration by creating index
i suggested you how to use @> and anyways your index would work

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.