Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL查询提供了所需的输出,但加载输出需要很长时间。我如何优化它?_Mysql_Sql_Optimization - Fatal编程技术网

Mysql SQL查询提供了所需的输出,但加载输出需要很长时间。我如何优化它?

Mysql SQL查询提供了所需的输出,但加载输出需要很长时间。我如何优化它?,mysql,sql,optimization,Mysql,Sql,Optimization,我有三个表,users(包括用户详细信息)、territory_categories(具有不同的territory名称和ID)、user_territory(两者之间的连接表) 我想根据列表中选定的地区筛选用户。 查询是否为-- 输出: usersCount: 136 attendanceId: 0 punchInDate: 1111-11-11 punchIntime: 0 punchOutDate: 1111-11-11

我有三个表,users(包括用户详细信息)、territory_categories(具有不同的territory名称和ID)、user_territory(两者之间的连接表)

我想根据列表中选定的地区筛选用户。

查询是否为--

输出:

usersCount: 136
        attendanceId: 0
        punchInDate: 1111-11-11
        punchIntime: 0
        punchOutDate: 1111-11-11
        punchOutTime: 0
    10 rows in set (2.06 sec)
如果没有where子句,则在加载时显示数据几乎需要1分钟

不带where子句的输出:

usersCount: 144
        attendanceId: 0
        punchInDate: 1111-11-11
        punchIntime: 0
        punchOutDate: 1111-11-11
        punchOutTime: 0
    10 rows in set (54.45 sec)

我正在获得所需的输出,但是查询加载需要将近1分钟的时间,我希望对其进行优化。我该怎么做?

试试这个。我刚刚缩小了表
user\u territory
上的内部联接,因此它不会联接整个
user\u territory
表,而是只联接不同的用户ID。希望能缩短演出时间

SELECT u.id,
 account_id_fk,
 first_name,
 last_name,
 email_address,
 PASSWORD,
 mobile_number,
 gender,
 user_accuracy,
 check_in_radius,
 report_to,
 role,
 allow_timeout,
 active,
 last_logged_on,
 last_known_location_time,
 last_known_location,
 u.created_on,
 u.created_by,
 (
    SELECT
        COUNT(u2.id)
    FROM
        fieldsense.users u2
    INNER JOIN (SELECT DISTINCT user_id_fk as id, territory_id FROM user_territory ) t1 ON u2.id = t1.id
    INNER JOIN territory_categories c ON c.id = t1.teritory_id 
    WHERE
        c.category_name LIKE "Gujarat"
    AND (
        u2.account_id_fk = 1
        AND u2.role != 0
    )
) AS usersCount,
 IFNULL(a.id, 0) attendanceId,
 IFNULL(a.punch_date, '1111-11-11') punchInDate,
 IFNULL(a.punch_in, 0) punchIntime,
 IFNULL(
    a.punch_out_date,
    '1111-11-11'
) punchOutDate,
 IFNULL(a.punch_out, 0) punchOutTime
FROM
    fieldsense.users AS u
LEFT OUTER JOIN attendances AS a ON u.id = a.user_id_fk
AND a.id = (
    SELECT
        max(id)
    FROM
        attendances att
    WHERE
        att.user_id_fk = u.id
)
INNER JOIN (SELECT DISTINCT user_id_fk as id, territory_id FROM user_territory ) t1 ON u2.id = t1.id
INNER JOIN territory_categories c ON c.id = t1.teritory_id 
WHERE
    c.category_name LIKE "Gujarat" 
GROUP BY
    u.id 
LIMIT 10

这不是一个Java问题。。。为了你所珍爱的一切,请格式化你的代码。请提供好的解决方案。迫切需要它的解决办法
SELECT u.id,
 account_id_fk,
 first_name,
 last_name,
 email_address,
 PASSWORD,
 mobile_number,
 gender,
 user_accuracy,
 check_in_radius,
 report_to,
 role,
 allow_timeout,
 active,
 last_logged_on,
 last_known_location_time,
 last_known_location,
 u.created_on,
 u.created_by,
 (
    SELECT
        COUNT(u2.id)
    FROM
        fieldsense.users u2
    INNER JOIN (SELECT DISTINCT user_id_fk as id, territory_id FROM user_territory ) t1 ON u2.id = t1.id
    INNER JOIN territory_categories c ON c.id = t1.teritory_id 
    WHERE
        c.category_name LIKE "Gujarat"
    AND (
        u2.account_id_fk = 1
        AND u2.role != 0
    )
) AS usersCount,
 IFNULL(a.id, 0) attendanceId,
 IFNULL(a.punch_date, '1111-11-11') punchInDate,
 IFNULL(a.punch_in, 0) punchIntime,
 IFNULL(
    a.punch_out_date,
    '1111-11-11'
) punchOutDate,
 IFNULL(a.punch_out, 0) punchOutTime
FROM
    fieldsense.users AS u
LEFT OUTER JOIN attendances AS a ON u.id = a.user_id_fk
AND a.id = (
    SELECT
        max(id)
    FROM
        attendances att
    WHERE
        att.user_id_fk = u.id
)
INNER JOIN (SELECT DISTINCT user_id_fk as id, territory_id FROM user_territory ) t1 ON u2.id = t1.id
INNER JOIN territory_categories c ON c.id = t1.teritory_id 
WHERE
    c.category_name LIKE "Gujarat" 
GROUP BY
    u.id 
LIMIT 10