2011-08-31 26 views
0

我使用一個存儲過程,FilterExpressionsASP.NET的SqlDataSource預計FilterExpression列在存儲過程

建築的搜索結果轉發只希望用戶3 SELECT語句或在我的中繼器4列但在我的where語句中可能有幾個不同的查詢。

我的存儲過程會是這個樣子

Select organisationRegulatory.ID, 
    organisationRegulatory.name As regname, 
    regAddr1, regAddr2, regAddr3, regAddr4, regAddr5, 
    CRONumber, 
    CHYNumber, 
    legalObjects, 
    charityActCategoryID, 
    isprimary, 
    organisationVoluntary.overviewURL As overviewURL, 
    min(targetCommunityId) as targetCommunityId 
    from organisationRegulatory 
     inner join organisationVoluntary 
     on organisationRegulatory.ID = organisationVoluntary.ID 
     left outer join targetCommunitiesToOrganisation 
     on organisationRegulatory.ID = targetCommunitiesToOrganisation.ID 
     left outer join charityActCategoriesToOrganisation 
     on organisationRegulatory.ID = charityActCategoriesToOrganisation.ID 
     left outer join financial 
     on organisationRegulatory.ID = financial.ID 
    where organisationVoluntary.publishStatusID = 2 
Group BY organisationRegulatory.ID, 
regAddr1,regAddr2,regAddr3,regAddr4,regAddr5, 
    CRONumber, 
    CHYNumber, 
    legalObjects, 
    organisationRegulatory.name, 
    organisationVoluntary.overviewURL, 
    nameCompSecretary, 
    charityActCategoryID, 
    isPrimary, 
    overviewURL 
    order by ID 

我的過濾器表達可能是這個樣子

AND (value > 50000 AND financialActivityTypeID = 31) AND (isPrimary = 1 AND charityActCategoryID = 1) 

如果我添加篩選表達式中的WHERE子句中的SQL通常我得到預期的結果,但如果我嘗試通過

SqlDataSource1.FilterExpression = "AND (value > 50000 AND financialActivityTypeID = 31) AND (isPrimary = 1 AND charityActCategoryID = 1)"; 
將篩選表達式添加到Stpred Procedure

我得到一個錯誤,

Exception Details: System.Data.EvaluateException: Cannot find column [financialActivityTypeId]. 

,直到我那列添加到SQL查詢的SELECT語句,我不想​​這樣做,因爲它會減慢查詢很大。有可能解決這個問題嗎?

這裏的中繼

<asp:Repeater 
           id="rptSearchResults" 
           runat="server" 
           DataSourceID="SqlDataSource1" 
           onitemdatabound="Repeater1_ItemDataBound"> 

和這裏的SQL數據源

<asp:SqlDataSource 
    ID="SqlDataSource1" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:DBConnectionString.ProviderName %>" 
    SelectCommand="spGetSearchResults" 
    SelectCommandType="StoredProcedure" 
    > 

</asp:SqlDataSource> 

任何幫助將非常感激。

感謝

回答

2

是否有可能解決這個問題?

您正在向select語句的結果集添加過濾器。
結果集僅包含您在select部分中指定的的列。

如果您未選擇列[financialActivityTypeId],則無法對其進行過濾。

這是沒有辦法的。

如何修復緩慢
我想加入另一列的緩慢與其說是在列本身,而是除了group by子句和額外的工作,這需要。
我的建議是增加

SELECT 
.... 
MIN([financialActivityTypeId]) AS financialActivityTypeId 
.... 

要選擇部分,看看是否能得到所需的輸出。
如果一個字段由索引唯一定義,那麼實際上不需要在group by子句中指定它。(只會有一個候選人),但是幾乎所有的DB ,MySQL的着名例外都堅持要鎖定聚合或group by子句中的所有行。
這增加了運行時間,這就是你所遭受的。

我的建議是要始終把所有的唯一定義排在MIN() *或* MAX()骨料和那些拉出來的group by條款。
您的查詢將運行得更快。

*)選擇哪一個並不重要,因爲應該(!)只能是一個值來選擇。

有關詳細信息,請參閱下面的鏈接,它涉及MySQL,但問題實際上是一個普遍問題,它解釋了爲什麼您應該儘可能縮短您的group by子句。
http://blog.mclaughlinsoftware.com/2010/03/10/mysql-standard-group-by/

+0

太好了,謝謝。 我以爲聚合函數是導致速度問題的原因......我現在就試試這個,謝謝。 –

相關問題