How to create an index on a date column - mysql

I have a mysql table that has a date column. I have approx 800,000 rows in the table so I want to index. The most common search is going to be:
Select X,Y,Z from table where dateField='yyyy-mm-dd';
the date is in standard mysql yyyy-mm-dd format.
what type of index do i want to create?
CREATE INDEX 2013- ON customer (date(10));
or
CREATE INDEX 201 ON customer (date(10));
or am I way off?
I am new to indexing so any help would be great.

Just add an index on dateField and give it a simple name, such as the column name.
For example:
alter table your_table
add index dateField (dateField);

Related

Delete date column with varchar(255)

I want to delete data which are past 2years. filed name is Date and type is varchar(255)
delete from <table_name> where <Filed> like '%2022';
running very longtime but no deletion of data
I have check and tried the query, you can try with
DELETE From <datatable> WHERE <date> LIKE '%2022';
DELETE From post WHERE date LIKE '%2022'; #Example
May you provide the database or screenshot? I have tried the query and no issue https://www.db-fiddle.com/f/syhtgVyEcSPcHRXBXHLtor/0
If primary key(probably id) and the date column are correlated, meaning bigger id will result the later dates(in this case, it is a of type varchar, and thanks to P.Salmon for pointing this out),then
I think you can delete using primary key(normally it is column id), for example:
select id from table where date > '2020' order by id asc limit 1;
// assume this id = 123456789, and delete rows that created before this id was created
delete from table where id < 123456789;
if there is not correlation, I have some ideas like below:
create a new column called created_at of type year/date/datetime/timestamp(probably date or year will do), it will store the actual year or date or datetime, use it to replace the date column of type varchar, probably create an index on created_at, and delete with the new column
If there is a index on date(varchar), since the % sign in like clause will cause the server not using index, so it is a full table scan for sure, and can you like enumerate all date like '01-01-2020', '01-02-2020', and delete rows one date by one date, with a script, I think in this way at least you get to use the index
if there are too many rows, like 10 years or even more, is it possible just migrate data within 2 years to a new table, and just remove the old table?
write a script, fetch 10000 row each time from beginning of primary key, and delete those that are over 2 years, and fetch next 10000
last_id = 0
select * from table where id > last_id order by id asc limit 10000;
last_id = [last id of the query]
delete from table where id in (xxx);

SQL Migrating old table to new table with an additional column

So im trying to migrate an old table to a new while maintaining compatibility, so my first guess would be to just map everything over by from example_A to example_B while inserting an old date.
Anyways, my question is there anything else I should do in order to maintain backward compatibility such using relations?
Thank you!
CREATE TABLE example_A
(
id INT
price NUMERIC
primary key (id)
)
CREATE TABLE example_B
(
id INT
price NUMERIC
date DATE
primary key (id, date)
)
Most queries should work compatibly, they'll just ignore the new column. Here are some potential problems:
Queries that use SELECT *. If the query involves a JOIN with another table that has a date column, the date column in the result will be ambiguous as to whether it refers to this table or the other table.
If you join with a table that has a date column, and refer to that column without a table prefix, it will become a syntax error because of the ambiguous reference.
INSERT statements that don't list specific columns will break, because the number of values will no longer match the number of columns.
You should also specify a default value for the new column, so that INSERT queries that don't fill it in will work.

Create functions indexes MySQL

I have a table with [date] index.
[date] column:
'2015-01-05'
'2015-01-06'
and etc
Can I create new index for function index?
When i am trying to create it I get error, example:
create index date_y on table (year(date))
If I could we didn't recreate queries for program performance.
Yes and no. No, you cannot create an index on an expression in this fashion. However, if you happen to have mysql v5.7.8 or newer, then you can create generated columns and you can create a secondary index on them (secondary index means that a generated column cannot be part of a primary key).
So, create your expression as a generated column and then create an index on it - if you have mysql v5.7.8 or newer.
One moment, when I have:
date is created index on tableX
id is created index on tableX
id is created index on tableY
My query:
Select * from tableX as x left outer join tableY as y on x.id=y.id
where year(x.date)=2015 and month(x.date)=11
Should I recreate date to:
create index date on tableX (date,id)
or some else?

How do you achieve a physically sorted table file organization?

Do I just declare an Index on a column in the schema like ?
ALTER TABLE `table` ADD INDEX `index_name` (`colDate`)
I want the table records to be physically sorted not just when doing an orderBy query.
I believe it would be best practice to index on a date if I will be consistently query a range of dates or any query regarding a sequential range. Am I correct in believing this?

Multiple Column Duplicate Entry Check MySQL

I'm aware that you can create a unique column in your MySQL table, but I'm actually looking to compare TWO columns.
So if I had records like:
Time User Table
10:00pm Fred 29
11:00am Bob 33
I COULD insert a new record with the time 10:00pm and table 33 but not 10:00pm table 29.
I know I could run a query and then nullify my ability to insert a new record if I had results from that query based on comparing those two fields, but I'm wondering if there is a more elegant solution wherein I can get a duplicate entry error from MySQL on the INSERT and save myself a few lines of code.
You can create a unique index that incorporates both columns:
CREATE UNIQUE INDEX idx_time_and_table ON reservations (`Time`, `Table`);
This will block any inserts for the same pairing provided both values are not NULL. Having a NULL value side-steps the unique checking.
You're also using reserved SQL keywords for your column names which you might want to change.
Try using a composite unique constraint across both columns:
ALTER TABLE your_table ADD UNIQUE(`Time`, `Table`);
Now any rows attempting to be added that have matching values will force MySQL to throw an error, which you can test for in your app.
Create an unique index based on the columns you want to be unique:
CREATE UNIQUE INDEX index_name ON table_name ( column1, column2,...);