如何优化sql查询以提高性能

时间:2019-09-04 10:11:16

标签: sql tsql

我只有一个子查询,它只能检索数千行。这是我的查询:

str = "select * from ";
                        str += " (SELECT a.[ID] ,a.[strUserName], ";
                        str += " (select intCurrentBalance from accountmappings where intUserID = a.[ID] and intAccountType = 1) as Prm_Balance ";
                        str += " ,(select intCurrentBalance from accountmappings where intUserID = a.[ID] and intAccountType = 2 ) as Trn_Balance ";
                        str += " ,(select intCurrentBalance from accountmappings where intUserID = a.[ID] and intAccountType = 3 ) as Opt_Balance ";
                        str += " ,a.[strMobile] ";
                        str += " ,a.[strEmailID] ";
                        str += " ,a.[bIsApproved] ";
                        str += " ,a.[bIsActive] ";
                        str += " ,a.[dtlastrecharge] ";
                        str += " ,case when b.id is null then 0 else b.id end as createdbyId ";
                        str += " ,case when b.strusername is null then '' else b.strusername end as createdby ";
                        str += " FROM users a left join users b on a.intCreatedBy = b.ID ";
                        str += " ) as test ";
                        str += " where Prm_Balance < " + int.Parse(txt_balance.Text.Trim()) + " and Trn_Balance < " + int.Parse(txt_balance.Text.Trim()) + " and Opt_Balance < " + int.Parse(txt_balance.Text.Trim());

,但是此查询花费太多时间来显示数据。什么分辨率。

2 个答案:

答案 0 :(得分:1)

一个明显的改进是删除子查询,并在left joins中使用它们,如下所示:

str = "select * from ";
                        str += " (SELECT a.[ID] ,a.[strUserName], ";
                        str += " a1.intCurrentBalance as Prm_Balance ";
                        str += " ,a2.intCurrentBalance as Trn_Balance ";
                        str += " ,a3.intCurrentBalance as Opt_Balance ";
                        str += " ,a.[strMobile] ";
                        str += " ,a.[strEmailID] ";
                        str += " ,a.[bIsApproved] ";
                        str += " ,a.[bIsActive] ";
                        str += " ,a.[dtlastrecharge] ";
                        str += " ,case when b.id is null then 0 else b.id end as createdbyId ";
                        str += " ,case when b.strusername is null then '' else b.strusername end as createdby ";
                        str += " FROM users a left join users b on a.intCreatedBy = b.ID ";
                        str += " left join accountmappings a1 on (a1.intUserID = a.[ID] and a1.intAccountType = 1) ";
                        str += " left join accountmappings a2 on (a2.intUserID = a.[ID] and a2.intAccountType = 2) ";
                        str += " left join accountmappings a3 on (a3.intUserID = a.[ID] and a3.intAccountType = 3) ";
                        str += " ) as test ";
                        str += " where Prm_Balance < " + int.Parse(txt_balance.Text.Trim()) + " and Trn_Balance < " + int.Parse(txt_balance.Text.Trim()) + " and Opt_Balance < " + int.Parse(txt_balance.Text.Trim());

干杯!

答案 1 :(得分:1)

您可以将单一联接用于多种帐户类型,并在下面的列选择中使用CASE

str = "select * from ";
str += " (SELECT a.[ID] ,a.[strUserName], ";
str += " CASE WHEN am.intAccountType = 1 THEN am.intCurrentBalance ELSE 0 END as Prm_Balance ";
str += " ,CASE WHEN am.intAccountType = 2 THEN am.intCurrentBalance ELSE 0 END as Trn_Balance ";
str += " ,CASE WHEN am.intAccountType = 3 THEN am.intCurrentBalance ELSE 0 END as Opt_Balance ";
str += " ,a.[strMobile] ";
str += " ,a.[strEmailID] ";
str += " ,a.[bIsApproved] ";
str += " ,a.[bIsActive] ";
str += " ,a.[dtlastrecharge] ";
str += " ,case when b.id is null then 0 else b.id end as createdbyId ";
str += " ,case when b.strusername is null then '' else b.strusername end as createdby ";
str += " FROM users a left join users b on a.intCreatedBy = b.ID ";
str += " left join accountmappings am ON am.intUserID = a.id
            AND am.intAccountType in (1,2,3) ";
str += " ) as test ";
str += " where Prm_Balance < " + int.Parse(txt_balance.Text.Trim()) + " and Trn_Balance < " + int.Parse(txt_balance.Text.Trim()) + " and Opt_Balance < " + int.Parse(txt_balance.Text.Trim());
相关问题