dokuzbir

dokuzbir

I want fetch time datas without specify minutes . So with one query, can i fetch these two times time: ~T[21:30:00] and time: ~T[21:15:00] ?

Showing Posts 1 to 10

Ankhers

Ankhers

start_time = ~T[21:00:00]
end_time = ~T[22:00:00]
MyApp.Repo.all(from f in Foo, 
  where: f.time >= ^start_time and f.time < ^end_time)

You can use Time.add/3 to get from your start time to the end time.

dokuzbir

dokuzbir OP

Thanks. What do you think about effiency of that code? Or should i splite time column to two different column :hour and :minute ?

dimitarvp

dimitarvp

The efficiency of that query depends on your database, not on your code. If you are so concerned, add a DB index to the column holding the timestamps.

Never do premature optimization.

Ankhers

Ankhers

I’m not really sure how efficient it is. But I don’t really see it being bad. You could also try using a fragment to search.

MyApp.Repo.all(from f in Foo,
  where: fragment("extract(hour from ?)", f.time) == ^"21")

Keep in mind I haven’t tested the above, but it should work, or something close to it anyways. Take a look here for functions to process dates and times within postgres.

blatyo

blatyo

Conduit Core Team

I tend to favor @Ankhers first approach as opposed to extracting the hour out in the query. That’s simply because the database must now run that operation on every row. If you have an index on the time column, you won’t be able to use it, unless the index is for the specific operation you’re performing.

In short, favor doing the work once, rather than many times.

blatyo

blatyo

Conduit Core Team

I don’t think it will make much difference. The only benefit I see is that it may make the work you do before the query simpler.

dokuzbir

dokuzbir OP

So if i understand right, @Ankhers’ s first approach better for indexes?

blatyo

blatyo

Conduit Core Team

The second approach could work fine if you had an expression index for it. But it would have to be a separate index from an index directly on the time column. So, you could end up with two potentially.

All of this is completely dependent on the situation though. If you have some other column you also always query on, it may reduce the result set, such that either solution is perfectly fine. I tend to avoid most indexes until it’s obvious they’re necessary. Before, I would try to pre-empt what indexes would be useful and later find most of them are unused because another one gave better performance, so the query planner always picked it. Having unused indexes affects write performance without any gain. Just watch your slow query log and you’ll be able to see what needs optimizing.

dokuzbir

dokuzbir OP

I actually had date:string, hour:integer, minute:integer columns. I started refactor my app to date:date , time:time columns. After comments i decided one date_time:naive_datetime and index. I dont know how postgresql works behind scene. My story is fetching booking slots depends on court_id and date. New problem is only court_id index works well ? or both court_id and date indexes? What do you think about having 2 indexes in same table ?

blatyo

blatyo

Conduit Core Team

If you’re going to always query two columns you should use a compound index, not one for each field. In most circumstances, a database can only use one index at a time.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley: Mutonex gamedesign was finish...
New
Stef-Verniers
Hello I’ve been coding in Elixir for almost two years now because of my current job where we implemented Elixir in our application. But ...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews