hasura / hasura/graphql-engine

Remote ralationship SQL query provide to slow performance

Open
#9,506 0 comments 0 reactions 0 assignees View on GitHub
k/bug t/native-dbs
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Version Information

Server Version: 2.18.0
CLI Version (for CLI related issue): 2.18.0

### Environment

Ubuntu 22.04 / Linux

### What is the current behaviour?

I have 2 databases and create remote relationships like this
![image](https://user-images.githubusercontent.com/34309587/226296041-a0df476d-c5d9-4f83-87c4-13fc92103bbe.png)

When I try to execute a query it works extreamly slow:
```
query getUserQuery_3($user_id: Int, $action_type: Int, $last_id: Int, $limit: Int) {
users_action(where: {
user_id: {_eq: $user_id},
action_type: {_eq: $action_type},
id: {_lt: $last_id}
}, order_by: {id: desc}, limit: $limit) {
id
video {
object_id
owner_id
duration
title
}
}
}
```
Afrer research I discovered that the reason is that hasura builds very inefficient SQl using LIMIT 1:
```
SELECT coalesce(json_agg("root"), '[]') AS "root"
FROM (
SELECT row_to_json(
(
SELECT "_e"
FROM (
SELECT "_root.base"."argument_id" AS "argument_id",
"_root.or.f"."f" AS "f"
) AS "_e"
)
) AS "root"
FROM (
SELECT *
FROM "pg_catalog"."jsonb_to_recordset"(($2)::jsonb) AS "_pg_catalog.jsonb_to_recordset"(
"argument_id" bigint,
"video_object_id" integer,
"video_owner_id" integer
)
WHERE ('true')
) AS "_root.base"
LEFT OUTER JOIN LATERAL (
SELECT row_to_json(
(
SELECT "_e"
FROM (
SELECT "_root.or.f.base"."object_id" AS "object_id",
"_root.or.f.base"."owner_id" AS "owner_id",
"_root.or.f.base"."duration" AS "duration",
"_root.or.f.base"."title" AS "title"
) AS "_e"
)
) AS "f"
FROM (
SELECT *
FROM "public"."videos_video"
WHERE (
(("_root.base"."video_object_id") = ("object_id"))
AND (("_root.base"."video_owner_id") = ("owner_id"))
)
LIMIT 1
) AS "_root.or.f.base"
) AS "_root.or.f" ON ('true')
) AS "_root"

```
### What is the expected behaviour?

I woud like to know is there any way to make hasura use another approach to query objects, e.g. using **IN** operator?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.