Query performance

  • Hi, Im new in sql server and I´l like to know another way to write this query, trying to improve performance.

    SELECT TOP 10 CUST_ID

    FROM CUSTOMERS

    WHERE CUST_UNI= @1 AND

       TYPE =@2 AND

       SUBTYPE =@3 AND

       STATUS = 'TRD' AND

       CUST_ID IN

             (Select OPERID

             FROM CUSTOMER_BAL

             WHERE CUST_UNI= @1 AND

             TYPE =@2 AND

             SUBTYPE =@3 AND

             CAS_ID =@4

    GROUP BY CUST_ID 

    ORDER BY COUNT(I_ID)

    Thanks for any advice.

  • As far as performance goes, its hard to say what kind of gain, if any, you will get with this query without knowing more about your database/environment.  But here is another way to write your query:

    SELECT TOP 10 Cust_ID

    FROM Customers C

        INNER JOIN Customer_Bal CB

        ON C.Cust_ID = CB.OperID

            and C.Cust_Uni = CB.Cust_Uni

            and C.Type = CB.Type

            and C.SubType = CB.SubType

    WHERE C.Cust_Uni = @1 and C.Type = @2 and C.SubType = @3 and CB.Cas_ID = @4

    John Rowan

    ======================================================
    ======================================================
    Forum Etiquette: How to post data/code on a forum to get the best help[/url] - by Jeff Moden

  • I would suggest to read a little about data normalisation.

    It dramatically improves performance for ALL queries, not just this one.

    _____________
    Code for TallyGenerator

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply