Select Timestamp By Time Ignoring Date - mysql

I am trying to "SELECT" a time range while ignoring the date portion of a "TIMESTAMP" field (09-09-11 10:23:03).
The query below works but has huge performance costs...
SELECT * FROM Table WHERE Time LIKE '10:09:13'
What is the fastest way to do this on a table with 1/2-1 million rows without using the "LIKE" command?

SELECT * FROM Table WHERE TIME(Time) = '10:09:13'
That should do it.

Related

How to filter string date column in MySQL?

I have a string date column in the format DD-MMM-YY (EX:29-Jul-18) which is same the filter value. I need to get records which are greater than the given date . But the filter is not working. It's retrieving all the records in the table. The table contains nearly 10M record. Can someone help ?
SELECT * FROM mytable WHERE `date_column` > '20-SEP-22'
Just try this
SELECT * FROM mytable WHERE `date_column` > STR_TO_DATE('20-SEP-22','%d-%M-%y')
For more info, please refer here
I don't know which programming language you are using, but for maximum query performance, it's better to parse to respective date format before it reaching it to the query that you are supposed to execute. As you said this have million records, so inspect the execution time and choose better solution wisely.

SQL SELECT - order dates with wrong format

I was tasked with ordering some entries in our web application. Current solution made by some other guy 10 years ago, is that there is a select on db and then it iterates and make table.
Problem is, that date is in dd-mm-yyyy format and in varchar data.
And not really sure, if I am brave enought to make changes to the database.
So is there some way to order it anyway within a select, some way to order it by the end meaby? Or only way without making some gruesome function in code is to change the db?
You can use the STR_TO_DATE() function for this. Try
ORDER BY STR_TO_DATE(varcharDateColumn, '%d-%m-%Y')
It converts your character-string dates to the DATE datatype where ordering works without trouble.
As of MySQL 5.7 or later, you can add a so-called generated column to your table without touching the other data.
ALTER TABLE tbl
ADD COLUMN goodDate
AS (STR_TO_DATE(varcharDateColumn, '%m-%d-%Y'))
STORED;
You can even put an index on that column if you need to use it for searrching.
ALTER TABLE t1 ADD INDEX goodDate(goodDate);
You can use STR_TO_DATE function, but this will work only for small tables(maybe thousands of records), on large data sets you will face performance problems:
SELECT *
FROM (
SELECT '01-5-2013' AS Date
UNION ALL
SELECT '02-6-2013' AS Date
UNION ALL
SELECT '01-6-2013' AS Date
) AS t1
ORDER BY STR_TO_DATE(Date,'%d-%m-%Y')
Long term solution should be conversion of that column to proper date type.

Generalize a sql query to accomodate full and incremental extract

I have a the following query :
select * from table where createddate>='03-Feb-2020' and createddate<'04-Feb-2020'
The above query will give me incremental count for a single day.
How do i generalize the above query so that i can get the entire historical data/full dump without changing the where clause.
For example:
select * from table where createddate>='VARIABLE1' and createddate<'VARIABLE2'
Is there a way that without changing the schema of the sql query i can just pass in different values for the createddate to get the full dump?
Is this what you want?
where createddate >= '1000-01-01' and createddate < '9999-12-31'
Note that dates in MySQL must be formated as YYYY-MM-DD.
Do you want group by?
select date(createddate), count(*)
from table
group by date(createddate);
This returns the count for each day.

MySQL select from table with unixtime row, but using datetime in query

I have a MySQL table, one of rows is filled using current unixtime (let's call it 'unixtime').
I need to select values, but using datetime format in the query.
For example:
SELECT * from test where unixtime>"2017-01-06 12:00" AND unixtime<"2017-01-08 12:14"
Note: Datetime range could be different.
Thank you very much!
As mentioned above, by using FROM_UNIXTIME() in your query. The following query selects from a table called 'testValues' and a Unix time field in the table, which is called 'timevalue' (stored as an int). It selects between two date ranges with the ranges in DateTime format in the query.
SELECT * FROM testValues
where FROM_UNIXTIME(timevalue) > "2017-01-07 21:00:48"
AND FROM_UNIXTIME(timevalue) < "2017-01-07 21:02:09"
Hope that helps :)

Calculate no of rows created on daily basis from a huge table in mysql

I need to calculate the num of rows created on a daily basis for a huge Table in mysql. I'm currently using
select count(1) from table_name group by Date
THe query is taking more 2000sec and counting. I was wondering if there's any optimized query or a way to optimize my query.
If you're only interested in items that were created on those dates, you could calculate the count at end-of-day and store it another table.
That lets you run the COUNT query on a much smaller data set (Use WHERE DATE(NOW()) = Date and drop the GROUP BY)
Then then query the new table when you need the data.
Make sure that "date" field is of "date" type, not datetime nor timestamp
Index that column
If you need it for one day, add a "where" statement. i.e. WHERE date="2013-07-10"
Add an index on the Date column, there's no other way to optimize this query that I can think of.
CREATE INDEX ix_date
ON table_name (Date);