select IFNULL(surplus_score,0) from tab_pb_psnl_score where tenant_id= '1478277449404907520' and area_code= '330282109204' and personnel_id = '1656243505334321152' and grid_id = '1610113030392119296' order by creat_time desc limit 1
时间: 2024-04-26 09:20:10 浏览: 54
这是一个 SQL 查询语句,作用是从名为 `tab_pb_psnl_score` 的表中查询出某个租户、区域、人员和网格的最近一次得分,如果没有得分则返回0。具体的查询条件是:`tenant_id` 等于 `'1478277449404907520'`,`area_code` 等于 `'330282109204'`,`personnel_id` 等于 `'1656243505334321152'`,`grid_id` 等于 `'1610113030392119296'`。最后使用 `IFNULL` 函数来判断是否有得分,有则返回得分,没有则返回0。
相关问题
优化这段SQL:SELECT CONCAT( HOUR ( mail_send_time ) DIV 1 * 1, ':00-', HOUR ( mail_send_time ) DIV 1 * 1, ':59' ) AS timeInterval, IFNULL( COUNT( * ), 0 ) AS total, IFNULL( SUM( contact_account_type in (SELECT code_description FROM dwd_t_code_value WHERE code_value = '01') ), 0 ) AS wxTotal, IFNULL( SUM( contact_account_type IN (SELECT code_description FROM dwd_t_code_value WHERE code_value = '02') ), 0 ) AS qqTotal, IFNULL( SUM( local_action = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '05') AND contact_account_type = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '01') ), 0 ) AS wxSend, IFNULL( SUM( local_action = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '06') AND contact_account_type = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '01')), 0 ) AS wxReceive , IFNULL( SUM( local_action = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '05') AND contact_account_type = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '02')), 0 ) AS qqSend, IFNULL( SUM( local_action = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '06') AND contact_account_type = (SELECT code_description FROM dwd_t_code_value WHERE code_value = '02')), 0 ) AS qqReceive FROM dwd_t_friend_talk, dwd_t_code_value WHERE person_no = '653129200105072624' AND contact_account_type IN ( '微信', 'QQ' ) AND friend_account <> '' AND contact_account_type <> '' AND mail_send_time IS NOT NULL AND mail_send_time <> '' GROUP BY HOUR ( mail_send_time ) DIV 1
可以尝试以下优化措施:
1. 将子查询转化为 JOIN 操作:将子查询(SELECT code_description FROM dwd_t_code_value WHERE code_value = '01') 转化为 JOIN dwd_t_code_value on dwd_t_code_value.code_value = '01',避免子查询的重复执行。
2. 将 IFNULL 函数替换为 COALESCE 函数,COALESCE 函数可以同时处理多个参数,效率更高。
3. 使用具有更好性能的索引:可以考虑在 dwd_t_friend_talk 表的 person_no 和 contact_account_type 字段上建立联合索引,提高查询效率。
4. 避免在 WHERE 子句中使用函数:可以将 HOUR(mail_send_time) 的计算移到 SELECT 子句中,避免在 WHERE 子句中使用函数,提高查询效率。
优化后的 SQL 语句如下:
SELECT
CONCAT(HOUR(mail_send_time) DIV 1 * 1, ':00-', HOUR(mail_send_time) DIV 1 * 1, ':59') AS timeInterval,
COALESCE(COUNT(*), 0) AS total,
COALESCE(SUM(contact_account_type = '微信'), 0) AS wxTotal,
COALESCE(SUM(contact_account_type = 'QQ'), 0) AS qqTotal,
COALESCE(SUM(local_action = '05' AND contact_account_type = '微信'), 0) AS wxSend,
COALESCE(SUM(local_action = '06' AND contact_account_type = '微信'), 0) AS wxReceive,
COALESCE(SUM(local_action = '05' AND contact_account_type = 'QQ'), 0) AS qqSend,
COALESCE(SUM(local_action = '06' AND contact_account_type = 'QQ'), 0) AS qqReceive
FROM
dwd_t_friend_talk
JOIN dwd_t_code_value ON dwd_t_code_value.code_value = '01' OR dwd_t_code_value.code_value = '02'
WHERE
person_no = '653129200105072624'
AND contact_account_type IN ('微信', 'QQ')
AND friend_account <> ''
AND contact_account_type <> ''
AND mail_send_time IS NOT NULL
AND mail_send_time <> ''
GROUP BY
HOUR(mail_send_time) DIV 1
SELECT DISTINCT ( A.DATA_TYPE ) AS DATA_TYPE, A.DATA_VALUE AS TSL, IFNULL(( SELECT B.DATA_VALUE FROM YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA B WHERE B.IS_DELETED = 0 AND B.DATA_DIMENSION = '期末' AND B.LEVEL = 2 AND B.DATA_NAME = '举报' AND B.DATA_AREA = '市级' AND B.DATA_TYPE = A.DATA_TYPE AND B.DATA_TIME = ( SELECT MAX( DATA_TIME ) FROM YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA WHERE IS_DELETED = 0 AND DATA_DIMENSION = '期末' AND DATA_NAME = '举报' AND LEVEL = 2 AND DATA_AREA = '市级' ) ), 0 ) AS JBL, CURRENT_ID FROM YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA A WHERE A.IS_DELETED = 0 AND A.DATA_DIMENSION = '期末' AND A.LEVEL = 2 AND A.DATA_NAME = '投诉' AND A.DATA_AREA = '市级' AND A.DATA_TIME = ( SELECT MAX( DATA_TIME ) FROM YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA WHERE IS_DELETED = 0 AND DATA_DIMENSION = '期末' AND DATA_NAME = '投诉' AND LEVEL = 2 AND DATA_AREA = '市级')这个sql 优化
对于这个SQL查询语句的优化,可以考虑以下几点:
1. 使用JOIN语句代替子查询:将子查询转换为JOIN语句,可以提高查询性能。
2. 添加适当的索引:根据查询条件和连接条件,为相关表添加合适的索引,以加快查询速度。
3. 减少重复的条件判断:在子查询和主查询中,有一些条件判断是重复的,可以将它们提取出来,避免重复计算。
4. 使用表别名:为表和字段使用简短的别名,提高查询语句的可读性。
下面是对原始SQL查询语句进行优化的示例:
SELECT DISTINCT
A.DATA_TYPE AS DATA_TYPE,
A.DATA_VALUE AS TSL,
IFNULL(B.DATA_VALUE, 0) AS JBL,
A.CURRENT_ID
FROM
YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA A
LEFT JOIN
(
SELECT
DATA_TYPE, DATA_VALUE
FROM
YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA
WHERE
IS_DELETED = 0
AND DATA_DIMENSION = '期末'
AND DATA_NAME = '举报'
AND LEVEL = 2
AND DATA_AREA = '市级'
AND DATA_TIME = (
SELECT MAX(DATA_TIME)
FROM YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA
WHERE
IS_DELETED = 0
AND DATA_DIMENSION = '期末'
AND DATA_NAME = '举报'
AND LEVEL = 2
AND DATA_AREA = '市级'
)
) B ON A.DATA_TYPE = B.DATA_TYPE
WHERE
A.IS_DELETED = 0
AND A.DATA_DIMENSION = '期末'
AND A.LEVEL = 2
AND A.DATA_NAME = '投诉'
AND A.DATA_AREA = '市级'
AND A.DATA_TIME = (
SELECT MAX(DATA_TIME)
FROM YXDDZH_MIDDLE.GZ_SCREEN_JGSY_SDHJ_SPFW_CITY_AREA_DATA
WHERE
IS_DELETED = 0
AND DATA_DIMENSION = '期末'
AND DATA_NAME = '投诉'
AND LEVEL = 2
AND DATA_AREA = '市级'
);
请注意,具体的优化策略可能需要根据实际情况进行调整和测试,以达到最佳的查询性能。
阅读全文