dokuzbir

dokuzbir

Ecto Time Query Only For Hour

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] ?

Marked As Solved

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.

Also Liked

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

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.

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.

Last Post!

blatyo

blatyo

Conduit Core Team

Although, you might be fine with just an index on court_id.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

We're in Beta

About us Mission Statement