优化查询,从许多表中选择

时间:2015-08-11 06:42:49

标签: mysql sql requirejs-optimizer

我有一个连接四个表的SQL脚本。我正在使用许多AND个操作。有没有解决方法如何优化这个脚本?

SELECT s.ACCESS_CODE, a.ACCESS_CODE, MIN(b.ID), b.NAME, a.USER_ID, b.PARENT_ID,b.UPDATE_TIME
FROM b_disk_simple_right s, b_user_access a, b_disk_object b, b_file f
WHERE s.ACCESS_CODE = a.ACCESS_CODE 
  AND a.USER_ID = '".$userID."' 
  AND s.OBJECT_ID=b.ID 
  AND f.ID = b.FILE_ID
  AND b.DELETED_BY=0
  AND f.MODULE_ID = 'disk' 
GROUP BY b.ID

2 个答案:

答案 0 :(得分:0)

尝试使用内部联接,如下所示:

SELECT        s.ACCESS_CODE, a.ACCESS_CODE AS Expr1, MIN(b.ID) AS Expr2, b.NAME, a.USER_ID, b.PARENT_ID, b.UPDATE_TIME
FROM          b_disk_simple_right AS s INNER JOIN
              b_user_access AS a ON s.ACCESS_CODE = a.ACCESS_CODE CROSS JOIN
              b_disk_object AS b CROSS JOIN
              b_file AS f
WHERE        (a.USER_ID = '".$userID."') AND (s.OBJECT_ID = b.ID) AND (f.ID = b.FILE_ID) AND (b.DELETED_BY = 0) AND (f.MODULE_ID = 'disk')
GROUP BY b.ID

答案 1 :(得分:0)

首先,您应该了解此操作是否实际上限制了应用程序的速度。如果不是,那么它可能是not worth worrying about。最后,在不改变模式或索引的情况下,您不会从中获得更多的东西(一个像样的数据库系统会在运行之前为您优化它)。