SELECT
id,
UserID,
fieldID,
(SELECT MAX(date1) FROM mytable WHERE fieldID=mt.fieldID) As DateOne,
(SELECT MAX(date2) FROM mytable WHERE fieldID=mt.fieldID) As DateTwo,
FROM mytable mt
WHERE UserID=1
MySQL, getting rows from table with latest date from 2 different columns
Вопрос
I have a mysql db where I import data every morning from an oracle db. The table is about 30million records. This takes about 5-6 hours. At night then I duplicate the table so I can access it from a website that shows a grid with the table content.
the table looks like this
id userID fieldID date1 date2
1 1 1 2014 2014
2 1 1 2014 2016
3 1 1 2013 2017
4 1 2 2014 2016
5 2 3 2014 2016
Basically on the website the user inputs userID and I have to show the records of each fieldID that have the latest date1, and then the latest date2 so the output for userID=1 should be:
id userID fieldID date1 date2
2 1 1 2014 2016
4 1 2 2014 2016
The query I'm using so far is this:
SELECT id,UserID,fieldID,date1,date2 FROM mytable WHERE id =
( SELECT id from mytable AS lookup
WHERE lookup.fieldID = mytable.fieldID
ORDER BY date1 DESC, date2 DESC
LIMIT 1 ) AND UserID=1
This works but it takes too long to query the server and load the page, while if I select everything for a specific userID it takes just a second to fetch around 2000 rows
I was wondering if you could suggest a better query or maybe a workaround where instead of duplicating the table, I create the new one with just the records I need (the website isn't used at night).
thanks
Решение
Другие советы
How about something like:
SELECT id, mytable.UserID, mytable.fieldID, A.date1, B.date2
FROM mytable AS outer,
(
SELECT MAX(date1) AS date1
FROM mytable AS innerone
WHERE innerone.id=outer.id
WHERE innerone.UserID=outer.UserID
AND innerone.fieldID=outer.fieldID
GROUP BY innerone.id,
innerone.UserID,
innerone.fieldID
) AS A,
(
SELECT MAX(date2) AS date2
FROM mytable AS innertwo
WHERE innertwo.id=outer.id
WHERE innertwo.UserID=outer.UserID
AND innertwo.fieldID=outer.fieldID
GROUP BY innertwo.id,
innertwo.UserID,
innertwo.fieldID
) AS B
WHERE UserID=1