web services - How to get and order rows from different tables with a limit and offset? -


i'm creating web services return list of chats , notifications. user can send input page number , number of items display per page, 2 types of objects can returned (from newest latest) , must displayed in same list.

i have 2 tables chat , notification

create table chat (   idchat serial not null,   idinterest integer not null,   idowner integer not null,   iduser integer not null,   creationdate,   editdate,   constraint pk_chat primary key (idchat) )  create table notification (   idnotification serial not null,   message character varying(255) not null,   creationdate date not null,   datefinvalidite date not null,   idcompte integer not null,   idtypenotification integer not null,   sender integer not null default 0,   constraint pk_notification primary key (idnotification) ) 

i create view groups chats , notifications, composed of id (idchat or idnotification), date (creationdate), , boolean ischat

but don't know if right solution.

problematic if have return 20 rows of ordered messages (notifications , chats) can either :

  • get 10 last notifications, 10 last chats, 9th chat might older 11th notification

  • check date of newest chat, if 20th newest notification older, notifications, otherwise ... don't know

  • get 20 newest notifications, 20 newest chats order them , send hem client heavy task server serves many requests @ same time.

select idchat id, creationdate, true ischat chat  union  select idnotification id, creationdate, false ischat notification  order creationdate desc limit 20 

this version faster:

select *     (         (             select idchat id, creationdate, true ischat             chat             order creationdate desc             limit 20         )           union          (             select idnotification id, creationdate, false ischat             notification             order creationdate desc             limit 20         )      ) s  order creationdate desc limit 20 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -