简体   繁体   English

PHP数据库值更改监听器,有没有更好的方法?

[英]PHP Database Value Change Listener, is there a better way?

Our company deals with sales. 我们公司处理销售。 We receive orders and our PHP application allows our CSRs to process these orders. 我们收到订单,并且我们的PHP应用程序允许我们的CSR处理这些订单。

There is a record in the database that is constantly changing depending on which order is currently being processed by a specific CSR - there is one of these fields for every CSR. 数据库中有一条记录会不断更改,具体取决于特定CSR当前正在处理的顺序-每个CSR都有这些字段之一。

Currently, a completely separate page polls the database every second using an xmlhhtp request and receives the response. 当前,一个完全独立的页面使用xmlhhtp请求每秒对数据库进行一次轮询,并接收响应。 If the response is not blank (only when the value has changed on the database) it performs an action. 如果响应不是空白(仅当数据库上的值已更改时),它将执行操作。

As you can imagine, this amounts to one databse query per second as well as a http request every second. 可以想象,这相当于每秒一个数据库查询和每秒一个http请求。

My question is, is there a better way to do this? 我的问题是,有更好的方法吗? Possibly a listener using sockets? 可能是使用套接字的侦听器? Something that would ping my script when a change has been performed without forcing me to poll the database and/or send an http request. 当执行更改时,可以在不强制我轮询数据库和/或发送http请求的情况下对我的脚本执行ping操作。

Thanks in advance 提前致谢

First off, 1 query/second, and 1 request/second really isn't much. 首先,每秒1个查询和1个请求/秒确实不多。 Especially since this number wont change as you get more CSRs or sales. 尤其是因为您获得更多的CSR或销售量后,这个数字不会改变。 If you were executing 1 query/order/second or something you might have to worry, but as it stands, if it works well I probably wouldn't change it. 如果您要执行1个查询/命令/秒或其他操作,则可能要担心,但就目前而言,如果运行良好,我可能不会更改它。 It may be worth running some metrics on the query to ensure that it runs quickly, selecting on an indexed column and the like. 可能有必要对查询运行一些指标以确保其快速运行(在索引列中进行选择),等等。 Most databases offer a way to check how a query is executing, like the EXPLAIN syntax in MySQL. 大多数数据库都提供一种检查查询执行方式的方法,例如MySQL中的EXPLAIN语法。

That said, there are a few options. 也就是说,有一些选择。

  • Use database triggers to either perform the required updates when an edit is made, or to call an external script. 使用数据库触发器可以在进行编辑时执行所需的更新,或者调用外部脚本。 Some reference materials for MySQL: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html MySQL的一些参考资料: http : //dev.mysql.com/doc/refman/5.0/en/create-trigger.html
  • Have whatever software the CSRs are using call a second script directly when making an update. 进行更新时,具有CSR使用的任何软件都可以直接调用第二个脚本。
  • Reduce polling frequency. 降低轮询频率。

You could use an asynchronous architecture based on a message queue. 您可以使用基于消息队列的异步体系结构。 When a CSR starts to handle an order, and the record in the database is changed, a message is added to the queue. 当CSR开始处理订单,并且数据库中的记录被更改时,一条消息将添加到队列中。 Your script can either block on requests for the latest queue item or you could implement a queue that will automatically notify your script on the addition of messages. 您的脚本可以阻止对最新队列项目的请求,也可以实现一个队列,该队列将在添加消息时自动通知您的脚本。

Unless you have millions of these events happening simultaneously, this kind of setup will cause the action to be executed within milliseconds of the event occuring, and you won't be constantly making useless polling requests to your database. 除非您同时发生数百万个此类事件,否则这种设置将使操作在事件发生后的几毫秒内执行,并且您将不会不断向数据库发出无用的轮询请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM