PgHero

Queries

Total Time Average Time Calls
699 min 54% 805 ms 52,110 postgres
-- scheduler/jobs
      SELECT id
      FROM jobs
      WHERE rrule IS NOT NULL
      AND status = $1
      AND (now() >= next_run OR next_run IS NULL)
      ORDER BY next_run ASC NULLS FIRST, updated_at ASC
      FOR UPDATE SKIP LOCKED
189 min 15% 16 ms 721,139 postgres
select distinct on (u.id) u.id
    from users u
    join sessions s on s.user_id = u.id
    join counters c on c.id = s.counter_id
    join branch_sessions bs on bs.id = s.branch_session_id
    where u.space_id = $1
    and s.status = $3
    and s.branch_id = $2
    and s.counter_id is not null
    and bs.current = $4
    and not exists (
      select $5
      from tickets t
      where t.space_id = u.space_id
      and t.user_id = u.id
      and t.branch_session_id = s.branch_session_id
      and t.state in ($6)
      limit $7
    )
    and (
      select us.status
      from user_statuses us
      where us.user_id = u.id
      and us.space_id = u.space_id
      order by us.created_at desc
      limit $8
    ) = $9
118 min 9% 60 ms 118,134 postgres
with operators as (
      select
        distinct on (u.id) u.id as user_id,
        s.id as session_id,
        c.id as counter_id,
        (case when c.override_user_services then cs.priority else $5 end) as counter_service_priority,
        us.priority as user_service_priority
      from users u
      join sessions s on s.user_id = u.id
      join counters c on c.id = s.counter_id
      join branch_sessions bs on bs.id = s.branch_session_id
      join counters_services cs on cs.counter_id = c.id and cs.service_id = $1
      left join users_services us on us.user_id = u.id and us.service_id = $2
      where u.space_id = $3
      
      and s.status = $6
      and s.branch_id = $4
      and s.counter_id is not null
      and bs.current = $7
      and not exists (
        select $8
        from tickets t
        where t.space_id = u.space_id
        and t.user_id = u.id
        and t.branch_session_id = s.branch_session_id
        and t.state in ($9)
        limit $10
      )
      and (
        select us.status
        from user_statuses us
        where us.user_id = u.id
        and us.space_id = u.space_id
        order by us.created_at desc
        limit $11
      ) = $12
    )
    select user_id, session_id, counter_id
    from operators
    order by counter_service_priority desc nulls last, user_service_priority asc
    limit $13
29 min 2% 10 ms 177,410 postgres
select * from user_statuses
      where user_id = $1
      and space_id = $2
      order by created_at desc
      limit $3
21 min 2% 1 ms 1,261,696 postgres
select "id", "name", "metadata", "created_at", "updated_at", "api_key", "enabled", "host", "email", "settings", "data", "dashboard_id" from "spaces" where "api_key" = $1
15 min 1% 5 ms 189,250 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
12 min 0.9% 2 ms 484,610 postgres
select "ticket_meetings".* from "ticket_meetings" where "ticket_meetings"."ticket_id" in ($1)
Details
CREATE INDEX CONCURRENTLY ON ticket_meetings (ticket_id)
11 min 0.8% 5 ms 133,926 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7, $8) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
10 min 0.8% 4 ms 152,304 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
10 min 0.8% 1 ms 838,881 postgres
SELECT *
      FROM applications
      WHERE referer IS NOT NULL
      AND $1 ^@ referer
9 min 0.7% 0 ms 1,480,681 postgres
SELECT    branch_id as id
      FROM      users_branches
      WHERE     space_id = $1 AND user_id = $2
8 min 0.6% 4 ms 119,654 postgres
with duration as (
        select id, sum((entry->$4)::float) as seconds
        from tickets, jsonb_array_elements(path) as entry
        where space_id = $1
        and service_id = $2
        and branch_session_id = $3
        and entry->>$5 = $6 and entry ? $7
        group by 1
      )
      select json_build_object($8, coalesce(avg(seconds), $9)) as stats
      from duration
8 min 0.6% 5 ms 108,075 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
8 min 0.6% 6 ms 85,724 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
8 min 0.6% 11 ms 42,597 postgres
with _data1 as (
      select
        service_id,
        coalesce(jsonb_agg(label order by updated_at desc) filter (where state in ($6, $7)), jsonb_build_array()) as attending,
        coalesce(count(id) filter (where state = $8), $9::int) as waiting_count,
        coalesce(count(id) filter (where state = $10), $11::int) as attending_count,
        coalesce(round(extract($12 from avg(now() - created_at) filter (where state = $13))), $14::int) as waiting_time
      from tickets
      where space_id = $1
      group by 1
    ),
    _data2 as (
      select
        services.id,
        services.name,
        services.code,
        services.branch_id,
        coalesce(jsonb_path_query_array(_data1.attending, $15), jsonb_build_array()) as attending,
        coalesce(_data1.waiting_count, $16::int) as waiting_count,
        coalesce (_data1.attending_count, $17::int) as attending_count,
        coalesce(_data1.waiting_time, $18::int) as waiting_time
      from services
      left join _data1 on services.id = _data1.service_id
      where services.space_id = $2
    ),
    _data3 as (
      select
        branch_id,
        jsonb_agg(jsonb_build_object(
        $19, id, 
        $20, name, 
        $21, code, 
        $22, attending, 
        $23, waiting_count,
        $24, attending_count,
        $25, waiting_time)) as services
      from _data2
      group by 1
    ),
    _data4 as (
      select
        s.branch_id,
        count(*) filter (where t.state = $26) as waiting_count,
        count(*) filter (where t.state = $27) as attending_count,
        coalesce(
          round(extract($28 from avg(now() - t.created_at) filter (where t.state = $29))),
          $30::int
        ) as waiting_time
      from services s
      left join tickets t on s.id = t.service_id
      where s.space_id = $3
      group by s.branch_id
    ),
    _data5 as (
      select
        branch_id,
        id,
        label,
        priority,
        service_id,
        service->>$31 as service_code,
        service->>$32 as service_name,
        counter_id,
        counter->>$33 as counter_code,
        counter->>$34 as counter_name
      from tickets
      where space_id = $4
      and state = $35
      order by created_at asc
    ),
    _data6 as (
      select
        branch_id,
        jsonb_agg(
          jsonb_build_object(
            $36, id,
            $37, label,
            $38, priority,
            $39, jsonb_build_object($40, service_id, $41, service_code, $42, service_name),
            $43, jsonb_build_object($44, counter_id, $45, counter_code, $46, counter_name)
          )
        ) as attending
      from _data5
      group by 1
    )
    select
      branches.id,
      branches.name,
      branches.code,
      coalesce(_data3.services, jsonb_build_array()) as services,
      jsonb_build_object($47, coalesce(_data6.attending, jsonb_build_array())) as tickets,
      jsonb_build_object(
      $48, coalesce(_data4.waiting_count, $49),
      $50, coalesce(_data4.attending_count, $51),
      $52, coalesce(_data4.waiting_time, $53)
    ) as stats
    from branches
    left join _data3 on branches.id = _data3.branch_id
    left join _data6 on branches.id = _data6.branch_id
    left join _data4 on branches.id = _data4.branch_id
    where branches.space_id = $5
8 min 0.6% 7 ms 66,142 postgres
-- filter tickets
      SELECT tickets.*
      FROM tickets
      
      WHERE tickets.space_id = $1
      AND tickets.branch_session_id = $2
      
      AND tickets.state NOT IN ($3, $4)
      
      ORDER BY tickets.created_at ASC
7 min 0.5% 9 ms 41,945 postgres
select "tickets".* from "tickets" inner join "tickets_sessions" on "tickets"."id" = "tickets_sessions"."ticket_id" where "tickets_sessions"."session_id" = $1 order by "created_at" asc
6 min 0.4% 5 ms 74,993 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
6 min 0.4% 7 ms 46,212 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
5 min 0.4% 0 ms 721,139 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "channel" = $3 order by "priority" desc, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
5 min 0.4% 3 ms 91,590 postgres
update "tickets" set "state" = $1, "transition" = $2, "session_id" = $3, "counter_id" = $4, "counter" = $5, "user_id" = $6, "user" = $7, "sm_transition" = $8, "reason" = $9, "updated_at" = $10, "stats" = jsonb_set(stats, $12, $13) || jsonb_set(stats,  $14, $15) where "id" = $11 returning *
5 min 0.4% 5 ms 61,175 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
5 min 0.3% 0 ms 6,844,834 postgres
select "branches".* from "branches" where "id" = $1
4 min 0.3% 0 ms 8,294,558 postgres
select "sessions".* from "sessions" where "token" = $1
4 min 0.3% 0 ms 1,680,435 postgres
select "devices".* from "devices" where "id" = $1
3 min 0.3% 0 ms 556,143 postgres
SELECT    branch_id as id
      FROM      users_branches
      WHERE     user_id = $1 AND space_id = $2
      ORDER BY  created_at
3 min 0.3% 8 ms 26,007 postgres
-- ticket/evaluate_postpone_action
      SELECT space_id, id
      FROM tickets
      WHERE available_actions#>>$1 = $2
      AND stats ? $3
      AND (stats#>>$4)::timestamptz + interval $5 * (service#>>$6)::integer <= now()
      FOR UPDATE
3 min 0.2% 2 ms 87,087 postgres
update "tickets" set "state" = $1, "transition" = $2, "session_id" = $3, "counter_id" = $4, "counter" = $5, "user_id" = $6, "user" = $7, "sm_transition" = $8, "reason" = $9, "updated_at" = $10 where "id" = $11 returning *
3 min 0.2% 3 ms 71,966 postgres
select "tickets".* from "tickets" where "space_id" = $1 and "service_id" = $2 and "branch_session_id" = $3 and "state" = $4 order by "created_at" asc
3 min 0.2% 0 ms 6,302,199 postgres
select "branch_sessions".* from "branch_sessions" where "space_id" = $1 and "branch_id" = $2 and "current" = $3
Details
CREATE INDEX CONCURRENTLY ON branch_sessions (branch_id, space_id)
3 min 0.2% 0 ms 819,575 postgres
select "datasets".* from "datasets" where "dashboard_id" = $1 order by "created_at" asc
3 min 0.2% 0 ms 8,510,986 postgres
select "id", "name", "metadata", "created_at", "updated_at", "api_key", "enabled", "host", "email", "settings", "data", "dashboard_id" from "spaces" where "name" = $1
2 min 0.2% 3 ms 47,811 postgres
update "tickets" set "state" = $1, "transition" = $2, "session_id" = $3, "counter_id" = $4, "counter" = $5, "user_id" = $6, "user" = $7, "sm_transition" = $8, "reason" = $9, "updated_at" = $10, "available_actions" = jsonb_set(available_actions,  $12, $13), "stats" = jsonb_set(stats, $14, $15) || jsonb_set(stats,  $16, $17) where "id" = $11 returning *
2 min 0.2% 0 ms 977,767 postgres
select "tickets".* from "tickets" where "branch_session_id" = $1 and "user_id" = $2 and "counter_id" = $3 and "state" = $4 order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
2 min 0.2% 3 ms 52,359 postgres
insert into "tickets" ("available_actions", "branch", "branch_id", "branch_session_id", "channel", "code", "created_at", "label", "priority", "reason", "serial", "service", "service_id", "session_id", "sm_transition", "source", "space_id", "state", "stats", "tz", "updated_at", "user", "user_id") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23) returning *
2 min 0.2% 21 ms 6,585 postgres
SELECT schemaname AS schema, t.relname AS table, ix.relname AS name, regexp_replace(pg_get_indexdef(i.indexrelid), $1, $2) AS columns, regexp_replace(pg_get_indexdef(i.indexrelid), $3, $4) AS using, indisunique AS unique, indisprimary AS primary, indisvalid AS valid, indexprs::text, indpred::text, pg_get_indexdef(i.indexrelid) AS definition FROM pg_index i INNER JOIN pg_class t ON t.oid = i.indrelid INNER JOIN pg_class ix ON ix.oid = i.indexrelid LEFT JOIN pg_stat_user_indexes ui ON ui.indexrelid = i.indexrelid WHERE schemaname IS NOT NULL ORDER BY 1, 2 /*pghero*/
2 min 0.2% 0 ms 952,161 postgres
update "jobs" set "status" = $1, "error" = $2, "updated_at" = $3 where "id" = $4 returning *
2 min 0.1% 2 ms 71,932 postgres
update ticket_meetings
      set status = $2, updated_at = now()
      where ticket_id = $1 and status != $3
Details
CREATE INDEX CONCURRENTLY ON ticket_meetings (ticket_id)
2 min 0.1% 0 ms 952,154 postgres
update "jobs" set "status" = $1, "next_run" = $2, "last_run_ts" = $3, "last_run_duration" = $4, "attempts" = $5, "updated_at" = $6 where "id" = $7 returning *
2 min 0.1% 0 ms 362,920 postgres
insert into "incidents" ("branch_id", "created_at", "data", "slug", "space_id", "status", "type", "updated_at") values ($1, $2, $3, $4, $5, $6, $7, $8) returning *
2 min 0.1% 5 ms 21,177 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
1 min 0.1% 66 ms 1,196 postgres
with operators as (
      select
        distinct on (u.id) u.id as user_id,
        s.id as session_id,
        c.id as counter_id,
        (case when c.override_user_services then cs.priority else $5 end) as counter_service_priority,
        us.priority as user_service_priority
      from users u
      join sessions s on s.user_id = u.id
      join counters c on c.id = s.counter_id
      join branch_sessions bs on bs.id = s.branch_session_id
      join counters_services cs on cs.counter_id = c.id and cs.service_id = $1
      left join users_services us on us.user_id = u.id and us.service_id = $2
      where u.space_id = $3
      and u.id not in ($6)
      and s.status = $7
      and s.branch_id = $4
      and s.counter_id is not null
      and bs.current = $8
      and not exists (
        select $9
        from tickets t
        where t.space_id = u.space_id
        and t.user_id = u.id
        and t.branch_session_id = s.branch_session_id
        and t.state in ($10)
        limit $11
      )
      and (
        select us.status
        from user_statuses us
        where us.user_id = u.id
        and us.space_id = u.space_id
        order by us.created_at desc
        limit $12
      ) = $13
    )
    select user_id, session_id, counter_id
    from operators
    order by counter_service_priority desc nulls last, user_service_priority asc
    limit $14
1 min < 0.1% 4 ms 17,000 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7, $8, $9) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc
1 min < 0.1% 1 ms 87,577 postgres
delete from "tickets" where "tickets"."id" = $1 returning *
1 min < 0.1% 0 ms 527,549 postgres
select "ticket_archive".* from "ticket_archive" where "space_id" = $1 and "id" = $2
1 min < 0.1% 2 ms 44,859 postgres
select "ticket_meetings".* from "ticket_meetings" where "space_id" = $1 and "ticket_id" = $2 order by "created_at" asc
Details
CREATE INDEX CONCURRENTLY ON ticket_meetings (ticket_id)
1 min < 0.1% 0 ms 1,448,829 postgres
select "branches".* from "branches" where "id" in ($1) order by "created_at" asc
1 min < 0.1% 0 ms 1,846,413 postgres
select "users".* from "users" where "id" = $1
1 min < 0.1% 27 ms 2,185 postgres
select "incidents".* from "incidents" where "branch_id" = $1 and "space_id" = $2 order by "created_at" asc
1 min < 0.1% 8 ms 6,585 postgres
WITH query_stats AS ( SELECT LEFT(query, $1) AS query, queryid AS query_hash, rolname AS user, ((total_plan_time + total_exec_time) / $2 / $3) AS total_minutes, ((total_plan_time + total_exec_time) / calls) AS average_time, calls FROM pg_stat_statements INNER JOIN pg_database ON pg_database.oid = pg_stat_statements.dbid INNER JOIN pg_roles ON pg_roles.oid = pg_stat_statements.userid WHERE calls > $4 AND pg_database.datname = current_database() ) SELECT query, query AS explainable_query, query_hash, query_stats.user, total_minutes, average_time, calls, total_minutes * $5 / (SELECT SUM(total_minutes) FROM query_stats) AS total_percent, (SELECT SUM(total_minutes) FROM query_stats) AS all_queries_total_minutes FROM query_stats ORDER BY "total_minutes" DESC LIMIT $6 /*pghero*/
1 min < 0.1% 2 ms 24,246 postgres
update "tickets" set "sm_transition" = $1, "session_id" = $2, "updated_at" = $3, "stats" = jsonb_set(stats,  $5, $6), "available_actions" = jsonb_set(available_actions,  $7, $8) where "id" = $4 returning *
1 min < 0.1% 0 ms 658,723 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3) and ("evaluate_at" <= $4 or "evaluate_at" is null) order by "priority" desc, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
1 min < 0.1% 0 ms 1,046,655 postgres
select "applications".* from "applications" where "applications"."id" in ($1)
1 min < 0.1% 1 ms 48,259 postgres
select
      su.space_id,
      su.user_id,
      su.last_status->>$1 as last_status_at,
      us.idle_status,
      us.idle_timeout_in_seconds
    from spaces_users su
    join user_status_settings us on us.space_id = su.space_id
    where su.last_status->>$2 = $3
      and us.idle_detection_enabled = $4
1 min < 0.1% 0 ms 1,611,013 postgres
select "roles".* from "roles" where "id" in ($1)
1 min < 0.1% 0 ms 1,550,853 postgres
select "counters_services".* from "counters_services" where "counter_id" = $1
1 min < 0.1% 15 ms 3,029 postgres
select "sessions".* from "sessions" where "device_id" is null and "space_id" = $1 and "branch_session_id" is not null and "counter_id" is not null and "user_id" is not null and "user_id" = $2 and not "counter_id" = $3 and not "id" = $4
1 min < 0.1% 0 ms 1,609,459 postgres
select "counters".* from "counters" where "id" = $1
1 min < 0.1% 1 ms 75,911 postgres
select max("serial") from "ticket_history" where "space_id" = $1 and "branch_session_id" = $2 and "service_id" = $3
1 min < 0.1% 3 ms 14,366 postgres
insert into "tickets" ("available_actions", "branch", "branch_id", "branch_session_id", "channel", "code", "created_at", "form", "label", "priority", "reason", "serial", "service", "service_id", "session_id", "sm_transition", "source", "space_id", "state", "stats", "tz", "updated_at", "user", "user_id") values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24) returning *
1 min < 0.1% 6 ms 6,889 postgres
SELECT extract($2 from max(created_at)) as ts
        FROM action_log
        WHERE action_id = $1
Details
CREATE INDEX CONCURRENTLY ON action_log (action_id)
1 min < 0.1% 0 ms 609,309 postgres
select "devices".* from "devices" where "id" = $1 and "space_id" = $2
1 min < 0.1% 0 ms 131,684 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3) order by "priority" desc, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
1 min < 0.1% 0 ms 78,931 postgres
select "tickets".* from "tickets" left join "counters_services" on "counters_services"."service_id" = "tickets"."service_id" and "counters_services"."counter_id" = $1 where "state" = $2 and "branch_session_id" = $3 and (("tickets"."counter_id" is null or "tickets"."counter_id" = $4) and ("tickets"."user_id" is null or "tickets"."user_id" = $5)) and "tickets"."service_id" in ($6, $7, $8) and "queue_id" is null order by "priority" desc, "counters_services"."priority" desc nulls last, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
1 min < 0.1% 0 ms 404,672 postgres
delete from "incidents" where "incidents"."id" = $1 returning *
1 min < 0.1% 0 ms 329,130 postgres
select "tickets".* from "tickets" where "space_id" = $1 and "id" = $2
1 min < 0.1% 5 ms 6,538 postgres
SELECT "Session"."id" AS "Session_id", "Session"."created_at" AS "Session_created_at", "Session"."updated_at" AS "Session_updated_at", "Session"."uuid" AS "Session_uuid", "Session"."user_id" AS "Session_user_id", "Session"."device_id" AS "Session_device_id", "Session"."application_id" AS "Session_application_id", "Session"."user_agent" AS "Session_user_agent", "Session"."ip_address" AS "Session_ip_address", "Session"."expires_at" AS "Session_expires_at", "Session"."refresh_token" AS "Session_refresh_token", "Session"."last_refresh_at" AS "Session_last_refresh_at", "Session"."revoked_at" AS "Session_revoked_at" FROM "auth"."sessions" "Session" WHERE (("Session"."refresh_token" = $1)) LIMIT $2
1 min < 0.1% 0 ms 66,222 postgres
select "counters".* from "counters" where "space_id" = $1 and "branch_id" = $2 order by "created_at" asc
1 min < 0.1% 0 ms 894,259 postgres
select "devices_services".* from "devices_services" where "device_id" = $1 order by "created_at" asc
1 min < 0.1% 16 ms 2,014 postgres
select * from "ticket_history" where "state" = $1 and "branch_session_id" = $2 and ("service_id" in ($3, $4, $5) and "counter_id" in ($6, $7, $8)) and "service_id" is not null and "counter_id" is not null order by "created_at" desc
1 min < 0.1% 0 ms 104,924 postgres
select "tickets".* from "tickets" left join "counters_services" on "counters_services"."service_id" = "tickets"."service_id" and "counters_services"."counter_id" = $1 where "state" = $2 and "branch_session_id" = $3 and (("tickets"."counter_id" is null or "tickets"."counter_id" = $4) and ("tickets"."user_id" is null or "tickets"."user_id" = $5)) and "tickets"."service_id" in ($6, $7, $8, $9, $10) and "queue_id" is null order by "priority" desc, "counters_services"."priority" desc nulls last, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
1 min < 0.1% 0 ms 1,130,680 postgres
select "spaces_users".* from "spaces_users" where "space_id" = $1 and "user_id" = $2
1 min < 0.1% 232 ms 135 postgres
select u.*
    from users u
    cross join lateral (
        select max(cs.priority) as max_priority, count(*) as session_count
        from sessions s
        join branch_sessions bs on bs.id = s.branch_session_id
        join branches b on b.id = s.branch_id
        join counters c on c.id = s.counter_id
        join counters_services cs on cs.counter_id = c.id
        and cs.service_profile_id = $1
        where bs.current = $4
        and s.user_id = u.id
        and s.status = $5
        and s.counter_id is not null
        and (b.code is null or b.code != $6)
        
        
        and not exists (
          select $7
          from tickets t
          where t.space_id = $2
          and t.user_id = u.id
          and t.branch_session_id = s.branch_session_id 
          and t.state in ($8, $9)
          limit $10
        )
    ) priorities
    where u.space_id = $3
    
    and priorities.session_count > $11
    order by priorities.max_priority desc nulls last, random()
1 min < 0.1% 2 ms 15,186 postgres
select distinct on ("service_id") "ticket_history".* from "ticket_history" where "state" = $1 and "branch_session_id" = $2 and "service_id" in ($3, $4, $5) order by "service_id" DESC, "created_at" DESC
1 min < 0.1% 0 ms 167,379 postgres
select "ticket_history".* from "ticket_history" where "state" = $1 and "branch_session_id" = $2 and id > $3 order by "created_at" desc
Details
CREATE INDEX CONCURRENTLY ON ticket_history (branch_session_id, id)
0 min < 0.1% 0 ms 937,817 postgres
select "dashboards".* from "dashboards" where "id" = $1
0 min < 0.1% 0 ms 1,628,657 postgres
select "users_roles".* from "users_roles" where "user_id" = $1
0 min < 0.1% 0 ms 132,283 postgres
select "services".* from "services" where "space_id" = $1 and "branch_id" = $2 order by "created_at" asc
0 min < 0.1% 0 ms 333,862 postgres
-- lookup branch
      SELECT *
      FROM branches
      WHERE space_id = $1
      AND (id::text = $2::text OR code = $3::text OR name = $4::text)
      LIMIT $5
0 min < 0.1% 3 ms 10,488 postgres
update "tickets" set "state" = $1, "transition" = $2, "session_id" = $3, "counter_id" = $4, "counter" = $5, "user_id" = $6, "user" = $7, "sm_transition" = $8, "reason" = $9, "updated_at" = $10, "stats" = jsonb_set(stats,  $12, $13) where "id" = $11 returning *
0 min < 0.1% 0 ms 77,216 postgres
insert into "tickets_sessions" ("branch_session_id", "created_at", "session_id", "space_id", "ticket_id", "updated_at") values ($1, $2, $3, $4, $5, $6) returning *
0 min < 0.1% 0 ms 2,235,117 postgres
select "calendar_rules".* from "calendar_rules" where "type" = $1 and "branch_id" = $2 order by "created_at" asc
0 min < 0.1% 0 ms 74,710 postgres
select "tickets".* from "tickets" left join "counters_services" on "counters_services"."service_id" = "tickets"."service_id" and "counters_services"."counter_id" = $1 where "state" = $2 and "branch_session_id" = $3 and (("tickets"."counter_id" is null or "tickets"."counter_id" = $4) and ("tickets"."user_id" is null or "tickets"."user_id" = $5)) and "tickets"."service_id" in ($6, $7, $8, $9, $10, $11) and "queue_id" is null order by "priority" desc, "counters_services"."priority" desc nulls last, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
0 min < 0.1% 0 ms 202,674 postgres
SELECT * FROM event WHERE calendar_id = $1 AND type = $2
0 min < 0.1% 0 ms 149,146 postgres
select "id", "name", "metadata", "created_at", "updated_at", "api_key", "enabled", "host", "email", "settings", "data", "logotype", "dashboard_id" from "spaces" where "id" = $1
0 min < 0.1% 4 ms 6,371 postgres
WITH steps AS (
        SELECT  jsonb_array_elements(path) AS step
        FROM    tickets
        WHERE   space_id = $1
        AND     service_id = $2
        AND     branch_session_id = $3
      )
      SELECT  count($5) AS calls_after
      FROM    steps
      WHERE   step->>$6 = $7
      AND     step->>$8 = $9
      AND     (step->$10)::numeric > $4
0 min < 0.1% 0 ms 348,199 postgres
select "services".* from "services" where "id" in ($1, $2, $3, $4, $5) order by "created_at" asc
0 min < 0.1% 5 ms 4,198 postgres
select distinct on ("service_id") "ticket_history".* from "ticket_history" where "state" = $1 and "branch_session_id" = $2 and "service_id" in ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) order by "service_id" DESC, "created_at" DESC
0 min < 0.1% 0 ms 146,565 postgres
select "services".* from "services" where "id" in ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) order by "created_at" asc
0 min < 0.1% 0 ms 562,530 postgres
select "users".* from "users" where "id" = $1 and "space_id" = $2
0 min < 0.1% 0 ms 54,887 postgres
select "counters".* from "counters" where "branch_id" = $1 order by "created_at" asc
0 min < 0.1% 0 ms 47,633 postgres
select "tickets".* from "tickets" left join "counters_services" on "counters_services"."service_id" = "tickets"."service_id" and "counters_services"."counter_id" = $1 where "state" = $2 and "branch_session_id" = $3 and (("tickets"."counter_id" is null or "tickets"."counter_id" = $4) and ("tickets"."user_id" is null or "tickets"."user_id" = $5)) and "tickets"."service_id" in ($6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19) and "queue_id" is null order by "priority" desc, "counters_services"."priority" desc nulls last, (case when evaluate_at is null then now() else evaluate_at end) asc, "created_at" asc
0 min < 0.1% 3 ms 6,584 postgres
SELECT n.nspname AS table_schema, c.relname AS table, attname AS column, format_type(a.atttypid, a.atttypmod) AS column_type, pg_get_expr(d.adbin, d.adrelid) AS default_value FROM pg_catalog.pg_attribute a INNER JOIN pg_catalog.pg_class c ON c.oid = a.attrelid INNER JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace INNER JOIN pg_catalog.pg_attrdef d ON (a.attrelid, a.attnum) = (d.adrelid, d.adnum) WHERE NOT a.attisdropped AND a.attnum > $1 AND pg_get_expr(d.adbin, d.adrelid) LIKE $2 AND n.nspname NOT LIKE $3 /*pghero*/
0 min < 0.1% 5 ms 3,489 postgres
-- lookup ticket
      SELECT *
      FROM tickets
      WHERE space_id = $1
      AND (id::text = $2::text
          OR lower(code) = lower($3::text)
          OR lower(label) = lower($4::text)
      )
      LIMIT $5
0 min < 0.1% 1 ms 28,519 postgres
insert into "user_statuses" ("created_at", "reason", "space_id", "status", "updated_at", "user_id") values ($1, $2, $3, $4, $5, $6) returning *
0 min < 0.1% 0 ms 597,708 postgres
select "id", "name", "metadata", "created_at", "updated_at", "api_key", "enabled", "host", "email", "settings", "data", "dashboard_id" from "spaces" where "id" = $1
0 min < 0.1% 0 ms 51,511 postgres
select *
    from branch_sessions
    where space_id = $1
    and current = $2
0 min < 0.1% 0 ms 240,774 postgres
select "services".* from "services" where "id" in ($1, $2, $3, $4, $5, $6) order by "created_at" asc
0 min < 0.1% 0 ms 1,015,992 postgres
select "licenses".* from "licenses" where "licenses"."id" in ($1)
0 min < 0.1% 5 ms 3,675 postgres
select "tickets".* from "tickets" where "state" = $1 and "branch_session_id" = $2 and "tickets"."service_id" in ($3, $4, $5, $6, $7, $8, $9, $10, $11, $12) order by (case when evaluate_at is null then now() else evaluate_at end) asc, "priority" desc, "created_at" asc