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 Answers1

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.

syam
  • 892
  • 8
  • 19