I'm facing currently very, very strange behaviour.
I have an enterprise application, running on JBoss, the ORM is as you might expect hibernate and I'm doing some bulk operations.
I have a table and in that table there are all kind of fields and among the others there is a field of type DATETIME. In this field I'm saving user info, I have another field of the same type, configured with an trigger, so every time when the row is being updated the second DATETIME field is also updated.
I have a case where the first field should be increased with one second, so I'm doing bulk copy something like this:
UPDATE <TABLE_NAME> SET customerDATE = DATE_ADD(customerDATE, INTERVAL 1 SECOND)
My problem is that the query does not always work as expected.
What I see is that the second DATETIME field(the onne with the trigger) is being updated, together with some other fields, but the auto-increment with one second does not alway works(sometimes is also increased, sometimes not).
I tried searching for some known issues, but without success.
If anybody knows some problem in this direction I would really appreciate any help!
I'm testing now if there will be problems with more than an extra second.
Thanks in advance!
Related
I have the field named hits in my table which records user interaction with objects on my website. For example: if user views object preview field hits would be updated and increased by 1, if user enters object's page it would be increased by 3 and etc.
Everything works like a charm on my local development server. But on production server (online > 50) sometimes field hits increases by right value and then within several seconds it could be decreased by some random small value (1,2). This bug doesn't always occur. I think the solution can be related with MyISAM engine I'm currently using for this table.
Below is a code implementing table update query (codeigniter)
$this->db->set('hits', 'hits+' . (int) $count, FALSE);
$this->db->where('id', $id);
$this->db->update('gallery');
So I have 2 questions:
How to fix this bug?
How can I perform multiple queries to my table to duplicate this situation on my local development server?
Thanks to all for responses. Anyway I got useful information from your answers. The problem was at the crontask. I had a function running every minute every third month. It cutted these hits. I fixed that crontask and everything works now.
Check your crontask twice. Thanks you all for your help.
I'm creating a database table where rows need to be removed after a set time. That time is defined in minutes by the valid_time cell in that row. I found this answer though I am not sure how I can implement what I need to into it.
Is someone able to tell me how I can implement this time (as minutes) into the event in the previous answer or if it's not possible, another way to do so. Thanks.
Clarification, I have two columns in the table. One is created which is a TIMESTAMP of when the row is created, and the second is valid_time, an integer in minutes of how long the row is valid for.
DELETE FROM table WHERE created < DATE_SUB(NOW(), INTERVAL `valid_time` MINUTE)
You can try to use the MySQL event scheduler and attach a DELETE query to it. That DELETE will be a simple query that will delete all records where current_time is greater that the valid_time/valid_until fields.
You can configure the scheduler to run in a minute/hourly/daily/... basis as you wish to erase the registers.
Check here and here for more information. M0rtiis offered the query example.
Good evening,
I've been searching around about the mysql event scheduler. Info and examples found on the web are very simple (and I have to admit my mysql experience is quite poor).
I need to do a mysql event scheduler that runs every hour. The event by itself isn't the problem. The thing is, I need the event to read a value from a field in each of the rows of the table, and then sum that value to another field of the row. Notice that this has to be done in all of the rows of the table, in a dinamic way, since the database keeps growing and I don't know how many rows I've got.
In PHP this would easy be to do with some for loops, a few arrays, then some UPDATE to mysql, and we would be done. The problem is that mysql event scheduler only works with mysql syntax, so this makes things a bit dificult.
I've got a table called users, it has some fields:
id, name, economicalincome, credits, etc...
So.. For each id, the event should some the value from the economicalincome to the existent credits.
Thanks in advance.
I have a doubt regarding the sqlite command that should be written for getting the name of MIN(count) and MAX(timestamp) and after retrieving, timestamp should be set for current timestamp amd and count should be incremented by 1.
For the first time, the table will be updates sa follows -
and for the next time, i have to update the timestamp for lil to current timestamp. Then for the third time, jil's timestamp must be updated.
I have tried to work with this, but I am getting logical errors, the table is not getting updated as per the requirement. How can I implement this?
I have solved the problem by making changes in the database. Initially, I have set the timestamp in descending order. then I wrote command to to display name with min(count)and min(timestamp) and updated in the same order. This worked for me.
I'm working on an app that is partly an employee time clock. It's not too complex but I want to make sure I head in the right direction the first time. I currently have this table structure:
id - int
employee_id - int (fk)
timestamp - mysql timestamp
event_code - int (1 for clock in, 0 for clock out)
I've got everything working where if their last event was a "clock in" they only see the "clock out" button and visa-versa.
My problem is that we will need to run a report that shows how many hours an employee has worked in a month and also total hours during the current fiscal year (Since June 1 of the current year).
Seems like I could store clock in and outs in the same record and maybe even calculate minutes worked between the two events and store that in a column called "worked". Then I would just need to get the sum of all that column for that employee to know how much time total.
Should I keep the structure I have, move to all on one row per pair of clock in and out events, or is there a better way that I'm totally missing?
I know human error is also a big issue for time clocks since people often forget to clock in or out and I'm not sure which structure can handle that easier.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Thanks for any advise/direction.
Rich
I would go with two tables:
One table should be simple log of what events occurred, like your existing design.
The second table contains the calculated working hours. There are columns for the logged in and logged out times and perhaps also a third column with the time difference between them precalculated.
The point is that the calculation of how many hours an employee has worked is complicated, as you mention. Employees may complain that they worked longer hours than your program reports. In this case you want to have access to the original log of all events with no information loss so that you can see and debug exactly what happened. But this raw format is slow and difficult to work with in SQL so for reporting purposes you also want the second table so that you can quickly generate reports with weekly, monthly or yearly sums.
Is MySQL Timestamp a good option or should I use UNIX Timestamp?
Timestamp is good because there are lots of MySQL functions that work well with timestamp. You might also want to consider using datetime which is very similar to timestamp.
Related
Should I use field 'datetime' or 'timestamp'?