MySQL 响应时间长
MySQL Long Response Time
我有一个有效的 MySQL 查询 select 来自每个社区的 table 的最新占用百分比进入我的数据库,但它似乎正在扫描整个数据库条目的查找时间大约需要 3-4 秒。
根据下面查询中提供的详细信息,有人可以为我提供一种 faster/better 方法来查找每个社区的最新时间戳字段吗? - 我需要查询 select 每个进入的社区,具有最新的时间戳,但每个社区 selected 的限制应该是 1(这意味着名为 "Test Community" 的社区可能有数百个提交但我需要最新输入的时间戳 selected,以及 table)
中输入的每个社区的相同 selection
SELECT t1.reportID, t1.communityID, t1.region, t1.percentOccupied,
t1.TIMESTAMP, Communities.fullName
FROM NightlyReports t1
INNER JOIN Communities On t1.communityID = Communities.communityID
WHERE t1.TIMESTAMP = ( SELECT MAX( TIMESTAMP ) FROM NightlyReports WHERE
t1.communityID = NightlyReports.communityID )
AND t1.region = 'GA' ORDER BY percentOccupied DESC
根据我的经验,相关子查询的性能通常很差;试试这个:
SELECT t1.reportID, t1.communityID, t1.region, t1.percentOccupied
, t1.TIMESTAMP, Communities.fullName
FROM NightlyReports AS t1
INNER JOIN Communities ON t1.communityID = Communities.communityID
INNER JOIN (
SELECT communityID, MAX( TIMESTAMP ) AS lastTimestamp
FROM NightlyReports
WHERE region = 'GA'
GROUP BY communityID
) AS lastReports ON t1.communityID = lastReports.communityID
AND t1.TIMESTAMP = lastReports.lastTimestamp
WHERE t1.region = 'GA'
ORDER BY percentOccupied DESC
您的查询没问题。对于此查询(仅重写了一点):
SELECT nr.reportID, nr.communityID, nr.region, nr.percentOccupied,
nr.TIMESTAMP, c.fullName
FROM NightlyReports nr INNER JOIN
Communities c
ON nr.communityID = c.communityID
WHERE nr.TIMESTAMP = (SELECT MAX(nr2.TIMESTAMP)
FROM NightlyReports nr2
WHERE nr.communityID = nr2.communityID
) AND
nr.region = 'GA'
ORDER BY percentOccupied DESC;
您需要索引:
NightlyReports(region, timestamp, communityid)
NightlyReports(communityid, timestamp)
Communities(communityID)
(这可能已经存在)
相关子查询本身不是问题。
我有一个有效的 MySQL 查询 select 来自每个社区的 table 的最新占用百分比进入我的数据库,但它似乎正在扫描整个数据库条目的查找时间大约需要 3-4 秒。
根据下面查询中提供的详细信息,有人可以为我提供一种 faster/better 方法来查找每个社区的最新时间戳字段吗? - 我需要查询 select 每个进入的社区,具有最新的时间戳,但每个社区 selected 的限制应该是 1(这意味着名为 "Test Community" 的社区可能有数百个提交但我需要最新输入的时间戳 selected,以及 table)
中输入的每个社区的相同 selectionSELECT t1.reportID, t1.communityID, t1.region, t1.percentOccupied,
t1.TIMESTAMP, Communities.fullName
FROM NightlyReports t1
INNER JOIN Communities On t1.communityID = Communities.communityID
WHERE t1.TIMESTAMP = ( SELECT MAX( TIMESTAMP ) FROM NightlyReports WHERE
t1.communityID = NightlyReports.communityID )
AND t1.region = 'GA' ORDER BY percentOccupied DESC
根据我的经验,相关子查询的性能通常很差;试试这个:
SELECT t1.reportID, t1.communityID, t1.region, t1.percentOccupied
, t1.TIMESTAMP, Communities.fullName
FROM NightlyReports AS t1
INNER JOIN Communities ON t1.communityID = Communities.communityID
INNER JOIN (
SELECT communityID, MAX( TIMESTAMP ) AS lastTimestamp
FROM NightlyReports
WHERE region = 'GA'
GROUP BY communityID
) AS lastReports ON t1.communityID = lastReports.communityID
AND t1.TIMESTAMP = lastReports.lastTimestamp
WHERE t1.region = 'GA'
ORDER BY percentOccupied DESC
您的查询没问题。对于此查询(仅重写了一点):
SELECT nr.reportID, nr.communityID, nr.region, nr.percentOccupied,
nr.TIMESTAMP, c.fullName
FROM NightlyReports nr INNER JOIN
Communities c
ON nr.communityID = c.communityID
WHERE nr.TIMESTAMP = (SELECT MAX(nr2.TIMESTAMP)
FROM NightlyReports nr2
WHERE nr.communityID = nr2.communityID
) AND
nr.region = 'GA'
ORDER BY percentOccupied DESC;
您需要索引:
NightlyReports(region, timestamp, communityid)
NightlyReports(communityid, timestamp)
Communities(communityID)
(这可能已经存在)
相关子查询本身不是问题。