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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With