How can I optimize the follow query ?
select
hash,page,timestamp, count(*) as total
from behaviour
group by hash, page
having total > 2 AND timestamp >= Now() - interval 5 minute
Thank you
This query it's not wrong.
Maybe with a bigger volume, you could have a problem with performance.
Try use "WHERE CONDITIONS" always. (In your context, i don't know if is possible).
Another tip, is EXPLAIN on your query!
Use "explain" before your query. (explain select hash, timestamp, count(*)........).
In this way, you can discover how many results your query is returned.
Related
I am working on a school project where I have to make a todo web app. now i have a little problem. I need to get the records that are running out of time (think 20% of the whole task left). now i'm looking for a solution in php or a sql statement with which i can retrieve only those records.
I tried many statements but i cant get one to work.
SELECT * FROM tasks
WHERE user_id='$user_id'
AND '$currentDate' BETWEEN start_date AND end_date
The above one is working with the date but not with time.
So now I need to have a statement or function that only retrieves the tasks that are almost finished. I've added a screenshot of the database and the application to clarify it a bit.
i hope someone could help me. (this is my first time using stackoverflow so sorry if i do something wrong)
First, you should not be munging your query with constants, date or otherwise. So, use now().
Second, combine the date/time into a single column
Third, you seem to want and:
WHERE user_id = ? AND
NOW() >= start_datetime AND
NOW() < end_datetime
If you want to store the date/time in separate columns, then you can combine them:
WHERE user_id = ? AND
NOW() >= ADDTIME(CAST(start_date as DATETIME), start_time) AND
NOW() < ADDTIME(CAST(end_date as DATETIME), end_time)
SELECT SUM(UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)) as TIMESTAMPDIFF FROM job_timing_logs
This is Mysqli Query
I want to this query as A laravel query
actually i am working on time count like start time, end time, pause time
my table like id ,start_time , end_time.
Thank you for help.
I am not sure how laravelish your query needs to be, but here is my take
DB::table('job_timing_logs')
->select(DB::raw("SUM(UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)) as TIMESTAMPDIFF"));
Just try this code :
$q = JobTimingLogs::sum('UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)');
after a little bit search in stackoverflow I came up with this query which I wanted, and its like this:
SELECT * FROM workers where TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60)
each record is consist of these fields:
id worker_name last_activity_time per_hour
so each worker has a per_hour field that will determined as actions number per hour.
last activity is the last time worker was doing an action.
It will get records that are qualified to run at current time.
so it will determine time interval with 60/per_hour in seconds and selects the records which time passed from their last_activity_time till now is more than this calculated interval.
this works okay, But I want to know two things.
1: is this query a good approach for this problem or its slow?
2: how can I do this query in laravel 5.5 active records or query builder? and also it should return one record at a time.
thanks in advance
i think your query is fine because there were no joins and no subquerires just only condition is there. You can fire raw queries on laravel to -
$workers = DB::select('SELECT * FROM workers where TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60)');
// or you can make use of query builder as follows
$workers = DB::table('workers')->whereRaw('TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60')->first();
this should work
\DB::table('workers')->whereRaw(...)->first();
https://laravel.com/docs/5.6/queries
1: is this query a good approach for this problem or its slow?
It will depend on how many workers you have, you should try and see
Please tell me, if I specify an interval like such: subtime(now(), INTERVAL 1 day) inside a MySQL SELECT query while having a proper datetime column to use as reference - will this prevent from the query to look through the entire table (over 100,000 records in my case) each time it runs but rather look through records made only the past 24 hours? Is DESC order needed for the datetime table or such? Also, if I have SUM(column) in the query, will it also run only for the interval specified?
Edit: If I just would like to use the above mentioned SUM to sum a column where there only are integers of value "1" - would it be better to simply check how many rows the SELECT query returns with mysql_num_rows - is it more efficient in combination with the time interval setting?
Thank you!
It will in fact prevent MySQL to go through the whole table but not if you just subtime() in the SELECT-part. Instead you have to do something like this:
SELECT * FROM myTable
WHERE myDateCol BETWEEN DATE_SUB(now(), INTERVAL 1 day) AND now()
The query will now select only rows one day old. Add a B-TREE index on myDateCol to speed things up:
ALTER TABLE myTable ADD INDEX myIdx USING BTREE (myDateCol)
See MySQL doc on that topic
I once read in a performance blog that it is better to use PHP's date functions to set dates in a MySQL query instead of using mysql date functions like curdate() because mysql can then cache the query or the result or something like that. Does anyone have any insight into this? Does it hold any water or is it baseless?
example:
$query = 'SELECT id FROM table WHERE publish_date = \''.date('Y-m-d').'\'';
vs
$query = 'SELECT id FROM table WHERE publish_date = CURDATE()';
Any function containing CURDATE() will not be cached. Source
Hardcoding the date should still be cached as far as I can tell. Though you might want to consider using the prepare functionality instead of splicing strings into your query (for sanity and security sake).
It's quite simple actually. The MySQL server does not see your PHP code so it'll receive one of these:
SELECT id FROM table WHERE publish_date = '2010-01-18';
SELECT id FROM table WHERE publish_date = CURDATE();
It will not read your intentions either. For MySQL, '2010-01-18' is a string and is deterministic: its value is always '2010-01-18'. However, CURDATE() is not deterministic: its value varies depending on the date when you run it. Thus the first one is cacheable and the second one is not.
I personally preffer first way, because it give clear head about server time (time zone), my mysql server happend to be 10h earlier when promissed :)
localtime in your PHP script will apply in SQL