English 中文(简体)
Mysql trigger/events vs Cronjob
原标题:

I have an auction website which let my users place an unlimited amount of autobiddings.

To monitor these autobiddings something has to check the database every second.

My question is if it is better to use mysql trigger events or to user a cronjob every minute that executes a 60 sec looping php script.

If i use the mysql trigger events there will be hundreds of events stacks on eachother, and fired on different times. Is this even possible?? ANd isn t the server load goning to be enourmous. I heard somewhere that the database will be locked while there is a schedueled event. I am using innoDB tables btw.

I hope some one can shed some light on this toppic.

Regards!

最佳回答

You d best run a seperate script that runs eternally and watches your database. That way you won t need cron. Nor a massive amount of triggers.

But you might want to reconsider your entire question. It s not necessary to actually update the bids every second. You only need to fill in the past x minutes/hours when someone actually points his browser to an auction or makes a manual bid. If it s all autobids you can calculate forwards an backwards with ease.

问题回答

The database handles scheduled requests no different from other request. But as many scheduled requests contain database and table maintenance operations that do lock the database it is not uncommon for them to do so.

Having said that: as your systems has to react to actions by a user the technical preferable way of doing this is using triggers. In practice this might lead to performance problems when your site does have high loads - though using a scheduled event might cause the same trouble.

My advise is to put your logic in stored procedures and call these stored procedures from the triggers. When you find that the triggers don t keep up you can always remove the triggers and call the stored procedures from a cron job.

I would probably model the solution to your auto-bidding problem a bit differently:

How about an event-based approach? You store the auto-bid requests of your users and if someone actually bids on an object you process the previously queued auto-bids.

This has the following benefits:

  • load on the database is distributed organically
  • you only do lookups that are actually needed at the time.
  • it is real-time and not tick-based
  • it is easier to reason about the business/application logic because it is local instead of global

You can use bash & shell script to achieve this auto-bidding process work. Refer this link you will get an idea: Bash script that executes php file every 5 seconds





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签