4
I have a space problem and there are many useless things being stored, but they are useful during the same day (you will understand).
Real problem:
There are more than 2 million "lines" and this is occupying more than 1.1GB, I am using a high availability server, anyway, this does not allow to change the capacity easily, besides having costs
Solution to the problem:
Erase data that is not the smallest based on the day, but the date is DATETIME and I need to match DATE. This process would occur every day, to delete the data of the previous day (but, as there are already data, would have to delete all old ones too).
Explanation of how it works:
The site saves several product data several times a day. Imagine the product with the id "999".
These data ("duplicated") are useful during the same day (that is, all data today should exist), so I can not simply check if there is already a larger today and prevent the insertion, as it should have this "duplication".
There is a:
id | Prod | Preco | Data
(id auto) | 999 | 99.99 | 2015-01-25 12:31:00
(id auto) | 999 | 89.99 | 2015-01-25 11:00:00
(id auto) | 999 | 78.99 | 2015-01-25 03:37:00
(id auto) | 999 | 98.99 | 2015-01-24 11:30:00
(id auto) | 999 | 74.99 | 2015-01-24 17:37:00
(id auto) | 999 | 79.99 | 2015-01-23 02:39:00
I wish there was only:
id | Prod | Preco | Data
(id auto) | 999 | 78.99 | 2015-01-25 03:37:00
(id auto) | 999 | 74.99 | 2015-01-24 17:37:00
(id auto) | 999 | 79.99 | 2015-01-23 02:39:00
What I tried to think:
I need something that selects everything that is not the smallest and that is on the same day.
This is extremely strange, but says the real idea:
$PegaIDMenor = SELECT id FROM tabela WHERE Preco = (SELECT MIN(Preco) FROM tabela WHERE Data LIKE '2015-01-25%' AND Prod = '999') AND Data LIKE '2015-01-25%' AND Prod = '999' LIMIT 1
// Exemplo apenas, logico que terá que existir funções de query!
$EliminaMaiores = DELETE FROM tabela WHERE id != ' $id ' AND Data LIKE '2015-01-25%' AND Prod = '999'
I thought the date go changing in loop and checking, but I believe there must be some better function, only using Mysql to perform the same task, and better performance.
I think it’s clear what I need. Remember, I don’t need SELECT but DELETE!
use http:/sqlfiddle.com/ to create a test
– Gabriel Rodrigues
You want to delete everything that is not the lowest price of the specific day?
– Gabriel Rodrigues
Yes, but not for a specific day, but always, every day. Per day each product should remain only one data, the one with the lowest price.
– Inkeliz
The first problem is that you’re in
DATETIME
and it is necessary to select byDATE
(function ofLIKE
is extremely slow, even with index). The other problem is to locate which is the smallest per day and per product, so that it is not excluded. Another idea I had, but I can’t build it would be to take theID
ofMIN()
forGROUP BY prod
, so would have theID
of all the lowest values per product (adding theWHERE Data LIKE
would have a specific day). ThenDELETE
usingNOT IN
of these earlier Ids using WHERE Data Like. Using PHP to create a data loop, deleting date by date.– Inkeliz
If you achieve something of the specific day would already be valid, because only create a loop to "change the specific day".
– Inkeliz
Why don’t you use
DAY(Data)
? The functionDAY
returns the specified day, you could compare for example,WHERE DAY(Data) = 26
– Guilherme Lopes
This can also be used, had read about it. I don’t know which would be better (in a matter of speed), because DAY would pick up and "convert" every day.
– Inkeliz