select a.id,b.is_manager_big, b.is_manager_bcompany,a.one_shoukuan,a.two_shoukuan,b.one_income,b.two_income from (select a.id, SUM(CASE WHEN b.shoukuan_month = '2023-05' and b.is_del = 0 then b.netincome ELSE 0 end) as one_shoukuan, SUM(CASE WHEN b.shoukuan_month = '2023-04' AND b.is_del = 0 THEN b.netincome ELSE 0 END) as two_shoukuan from xw_kefu a left join xw_user_applyloan_checking b on b.kefuid = a.id where a.id in (1748,1749,1776,1834,1913,1921,1922,1778,1830,1829,1833,1835,1750,1753,1754,1756,1758,1761,1892,1905,1762,1764,1902,1915,1751,1773,1792,1774,1788,1917,1791,1793,1795,1909,1775,1787,1752,1757,1771,1772,1914,1768,1898,1760,1767,1769,1863,1770,1827) and a.is_del = 0 and a.is_lizhi = 0 group by a.id) as a,(select a.id,a.is_manager_big, a.is_manager_bcompany, SUM(CASE WHEN b.month = '2023-05' then b.create_income end) as one_income, SUM(CASE WHEN b.month = '2023-04' THEN b.create_income END) as two_income from xw_kefu a left join xw_kefu_performance_target b on b.kefuid = a.id where a.id in (1748,1749,1776,1834,1913,1921,1922,1778,1830,1829,1833,1835,1750,1753,1754,1756,1758,1761,1892,1905,1762,1764,1902,1915,1751,1773,1792,1774,1788,1917,1791,1793,1795,1909,1775,1787,1752,1757,1771,1772,1914,1768,1898,1760,1767,1769,1863,1770,1827) and a.is_del = 0 and a.is_lizhi = 0 group by a.id ) as b where b.id = a.id group by a.id优化一下这个查询统计
时间: 2023-10-02 15:08:38 浏览: 86
SQL Server 2016里的sys.dm_exec_input_buffer的问题
可以考虑以下优化:
1. 使用预编译语句:如果该查询需要频繁执行,可以使用预编译语句,避免每次执行都需要重新解析和编译查询语句。
2. 添加索引:根据查询条件,可以为 xw_kefu 和 xw_user_applyloan_checking 表的 id 列、shoukuan_month 列添加索引,为 xw_kefu_performance_target 表的 kefuid 列、month 列添加索引。
3. 使用 EXISTS 子查询代替 LEFT JOIN:可以使用 EXISTS 子查询代替 LEFT JOIN,因为 EXISTS 子查询能够在找到第一个匹配项时立即停止查询,从而提高效率。
优化后的查询语句如下:
SELECT a.id,
b.is_manager_big,
b.is_manager_bcompany,
a.one_shoukuan,
a.two_shoukuan,
b.one_income,
b.two_income
FROM (SELECT a.id,
SUM(CASE
WHEN b.shoukuan_month = '2023-05' AND b.is_del = 0 THEN b.netincome
ELSE 0
END) AS one_shoukuan,
SUM(CASE
WHEN b.shoukuan_month = '2023-04' AND b.is_del = 0 THEN b.netincome
ELSE 0
END) AS two_shoukuan
FROM xw_kefu a
WHERE a.id IN (1748, 1749, 1776, 1834, 1913, 1921, 1922, 1778, 1830, 1829, 1833, 1835, 1750, 1753, 1754, 1756,
1758, 1761, 1892, 1905, 1762, 1764, 1902, 1915, 1751, 1773, 1792, 1774, 1788, 1917, 1791, 1793,
1795, 1909, 1775, 1787, 1752, 1757, 1771, 1772, 1914, 1768, 1898, 1760, 1767, 1769, 1863, 1770,
1827)
AND a.is_del = 0
AND a.is_lizhi = 0
GROUP BY a.id) AS a,
(SELECT a.id,
a.is_manager_big,
a.is_manager_bcompany,
SUM(CASE
WHEN b.month = '2023-05' THEN b.create_income
ELSE 0
END) AS one_income,
SUM(CASE
WHEN b.month = '2023-04' THEN b.create_income
ELSE 0
END) AS two_income
FROM xw_kefu a
WHERE a.id IN (1748, 1749, 1776, 1834, 1913, 1921, 1922, 1778, 1830, 1829, 1833, 1835, 1750, 1753, 1754, 1756,
1758, 1761, 1892, 1905, 1762, 1764, 1902, 1915, 1751, 1773, 1792, 1774, 1788, 1917, 1791, 1793,
1795, 1909, 1775, 1787, 1752, 1757, 1771, 1772, 1914, 1768, 1898, 1760, 1767, 1769, 1863, 1770,
1827)
AND a.is_del = 0
AND a.is_lizhi = 0
AND EXISTS (SELECT 1
FROM xw_kefu_performance_target b
WHERE b.kefuid = a.id
AND b.is_del = 0)
GROUP BY a.id) AS b
WHERE b.id = a.id
GROUP BY a.id;
阅读全文