Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query table every second for notification. Is it a good practice?

I have a project that has notification functionality and I came up with an idea on how to get a notification real time. My idea is to query the table every second. I have a notification table, so whenever the client is adding something it will insert to the notification table and then the other client gets the notification by querying it. I use ajax to do this.

Is it good to query the table every second or it's bad programming practice? I only use this in localhost and it's fine. Do this kind of idea affect negatively when the site goes live?

thanks in advance for your answers.

like image 703
Vincent815 Avatar asked Feb 12 '14 14:02

Vincent815


People also ask

How often should you query a database?

It just depends. You can query your database as often as you can, however you may meet efficiency issue. You can just write a script which just query your database and set a clock to make that script just run for sometime (30 seconds for example). You count the total query, and output the count into file.

Why using select * in an SQL query is a bad practice?

When you use SELECT * in views, then you create subtle bugs if a new column has been added and the old one is removed from the table. Why? Because your view will not break but start returning an incorrect result. To avoid that, you should always use WITHSCHEMABINDING with views in SQL Server database.

Why select * is not good?

By using SELECT * , you can return unnecessary data that will just be ignored. But fetching that data is not free of cost. This results in some wasteful IO cycles on the DB end since you will be reading all of that data off the pages. Perhaps you could have read the data from index pages.

Can you do select * in SQL?

So, SELECT just starts the statement and it's probably followed by a star (*) AKA “splat”. It basically means retrieve all the columns from a table. If there are multiple tables that we are selecting from, the star will select all columns from all tables e.g. when joining two or more tables.


1 Answers

Pinging the DB every second is not the ideal way to do this; you can implement a combination of DB triggers and HTML5 websockets/push notifications: http://html5hacks.com/blog/2013/04/21/push-notifications-to-the-browser-with-server-sent-events/

http://www.abrandao.com/2013/06/25/websockets-using-modern-html5-technology-for-true-server-push/

http://pusher.com/tutorials/html5_realtime_push_notifications

like image 62
Digital Chris Avatar answered Sep 22 '22 03:09

Digital Chris