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.