shahryarjb
Hi, I have a notification part on my project which has 2 schema tables:
notifs= notifications is sent by system or moderator usersuser_notif_status=readorskippednotifications of user.
Imagine in this forum you have a profile picture in the top of the right side that shows you how many notifications you have 3 or 20, it counts them for you. If you clicked on it, you can see your last 10 notifications with status, you have read or not!
Then, if I make a query for loading last 10 queries, I do like this:
from(n in Notifs, left_join: s in assoc(n, :user_notif_status))
It is okay for now, but I have a problem when I want to count all the unread notification. I think these are separate queries and different. I could not use subquery because it loads my counter every record and I don’t want it, I just want to load counter one time.
Furthermore, I have 2 solutions on my mind:
- use elixir
taskto send 2 queries and show it to my user. - load 2 different queries.
Please suggest me what to do that is better for load time when I have a big database on my projects.
Thanks
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
APB9785
If your first query is fetching both read and unread notifications, you could pass the results into
Enum.count/2shahryarjb
I am using paginate and do not load all the records parameters, I just load last 10
see these images
APB9785
Ah. I think it’s fine to use
Taskfor multiple async queries, since Ecto does this under the hood also. But since thecountquery must check the entire table, there is potentially a faster way: adding anunread_countcolumn for each user, which gets updated during transactions where a notification is created, read, or skipped. Since it removes work from each read, but adds work to each write, this would be optimal for situations where page loads are more common than new notifications.