ycv005

ycv005

Optimize the Ecto.Query using Ecto.SubQuery

I want to combine two queries into 1 query to optimize my code. I tried my own by getting error.
Also, In doc, it is given that Subqueries are currently only supported in the from and join fields.. So, does following can be done or not. And, if not then how could I optimized them.

Check my implementation-

1st Query

    query = from(pipeline in CandidatePipeline,
        join: ap in assoc(pipeline, :applicant),
        where:
          pipeline.job_opening_id == ^job_id and pipeline.job_stage_id == ^stage_id and
            pipeline.status == ^status,
        preload: [applicant: ap],
        join: a in AppliedOpening,
        on: a.job_opening_id == pipeline.job_opening_id and a.applicant_id == pipeline.applicant_id,
        join: applicant in Applicant,
        on: applicant.id == pipeline.applicant_id,
        join: r in Result,
        on: r.applied_opening_id == a.id,
        where: r.state == ^result,
        order_by: [desc_nulls_last: r.percent, asc: applicant.name]
      )

2nd Query

    query = from(pipeline in CandidatePipeline,
        join: ap in assoc(pipeline, :applicant),
        where:
          pipeline.job_opening_id == ^job_id and pipeline.job_stage_id == ^stage_id and
            pipeline.status == ^status,
        preload: [applicant: ap],
        join: a in AppliedOpening,
        on: a.job_opening_id == pipeline.job_opening_id and a.applicant_id == pipeline.applicant_id,
        where: a.state == ^applied_state,
        join: applicant in Applicant,
        on: applicant.id == pipeline.applicant_id,
        order_by: [asc: applicant.name]
      )

Query that I tried, but not working

      sub_query =  from pipeline in CandidatePipeline,
      join: ap in assoc(pipeline, :applicant),
      where:
        pipeline.job_opening_id == ^job_id and pipeline.job_stage_id == ^stage_id and
          pipeline.status == ^status,
      preload: [applicant: ap]

      cond do
        result == "unattempted" || result == "skipped" ->
          query = Ecto.subquery(sub_query),
          join: a in AppliedOpening,
          on: a.job_opening_id == pipeline.job_opening_id and a.applicant_id == pipeline.applicant_id,
          where: a.state == ^applied_state,
          join: applicant in Applicant,
          on: applicant.id == pipeline.applicant_id,
          order_by: [asc: applicant.name]
        true ->
          query = Ecto.subquery(sub_query),
          join: a in AppliedOpening,
          on: a.job_opening_id == pipeline.job_opening_id and a.applicant_id == pipeline.applicant_id,
          join: applicant in Applicant,
          on: applicant.id == pipeline.applicant_id,
          join: r in Result,
          on: r.applied_opening_id == a.id,
          where: r.state == ^result,
          order_by: [desc_nulls_last: r.percent, asc: applicant.name]
      end

In my just above code, getting error on 2nd line just after cond do that is- syntax error before: ','
Your help will be appreciated. Also, if you know more better or optimized way let me know.

Most Liked

peerreynders

peerreynders

  • You can’t preload in a query you are going to use for a subquery
  • So your subquery focuses on the information that the rest of the query needs
  • You don’t actually have a usecase for a subquery
  • You need composition which was explained in your last topic

Again freehand, no guarantees but something along the lines of:

query =
  from(pipeline in CandidatePipeline,
    join: ap in assoc(pipeline, :applicant),
    where: pipeline.job_opening_id == ^job_id
      and pipeline.job_stage_id == ^stage_id
      and pipeline.status == ^status,
    join: a in AppliedOpening, as: :opening,
      on: a.job_opening_id == pipeline.job_opening_id
        and a.applicant_id == pipeline.applicant_id,
    join: applicant in Applicant, as: :applicant,
      on: applicant.id == pipeline.applicant_id,
    preload: [applicant: ap]
  )

query =
  if result == "unattempted" or result == "skipped" do
    from([opening: a, applicant: applicant] in query,
      where: a.state == ^applied_state,
      order_by: [asc: applicant.name]
    )

  else
    from([opening: a, applicant: applicant] in query,
      join: r in Result,
        on: r.applied_opening_id == a.id,
      where: r.state == ^result,
      order_by: [desc_nulls_last: r.percent, asc: applicant.name]
    )
  end

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement