Join 使用内部联接从多个表执行查询需要很长时间

Join 使用内部联接从多个表执行查询需要很长时间,join,count,Join,Count,我认为是内部连接导致查询执行时间过长,导致系统速度变慢尝试将WHERE d

我认为是内部连接导致查询执行时间过长,导致系统速度变慢

尝试将WHERE d<'$dayed'包含到子查询a中。删除字段并放置计数*。如果表没有根据用于条件和关系的值编制索引,请尝试编制索引

I have an sql query below that is taking too long to execute. kindly check the query and optimise it for me, i need to count number of files from a file_Actions table but combining it three other tables using inner join

SELECT count(*) as total 
FROM (SELECT t1.cfid as cfid,MAX(t1.timestamp) d 
FROM file_actions t1 
INNER JOIN case_files t2 ON t2.cfid=t1.cfid 
INNER JOIN case_file_allocations t3 ON t1.cfid=t3.cfid
INNER JOIN cbeta_user t4 
WHERE t4.id=t1.user_id 
AND t4.team_leader='$user' and t2.closed<>'yes' AND
t2.deleted<>1 AND
t3.reallocated<>'yes' GROUP BY t1.cfid) a 
WHERE d < '$yesterday'

建议阅读:

我只需要文件操作表中具有最新时间戳的文件数<$Yesterday可能不需要按cfid分组。如果它是一个ID,则不会重复。在您的情况下,d列将是未知的。我看到了…复制粘贴问题。
SELECT count(*) as total
  FROM file_actions t1 
  INNER JOIN case_files t2 
               ON t2.cfid=t1.cfid 
  INNER JOIN case_file_allocations t3 
               ON t1.cfid=t3.cfid
  INNER JOIN cbeta_user t4 
WHERE t4.id=t1.user_id 
 AND t4.team_leader='$user' and t2.closed<>'yes' 
 AND t2.deleted<>1 
 AND t3.reallocated<>'yes' 
 AND d < '$yesterday'
GROUP BY t1.cfid